Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Motion Control
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-14 03:13:38
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Generate an ESP32 code to control a 4-channel */
- /* relay and two 12V DC LEDs based on PIR motion */
- /* detection, including timers for inactivity and */
- /* motion detection, using only connected components. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* generate a code for ESP32 microcontroller light */
- /* automation using esp32,4channel relay,12v dc */
- /* led,pir sensor such that: the led turns on or off */
- /* ehen motion is detected */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // No external libraries are required for this implementation.
- /****** PIN DEFINITIONS *****/
- const int relayPins[4] = {14, 27, 26, 25}; // GPIO pins connected to relay channels
- const int ledPins[2] = {16, 17}; // GPIO pins connected to the 12V LEDs (via transistor or driver)
- const int pirSensorPin = 13; // GPIO pin connected to PIR sensor
- /****** SYSTEM TIMERS AND STATES *****/
- unsigned long lastMotionTime = 0;
- const unsigned long inactivityTimeout = 300000; // 5 minutes in milliseconds
- bool motionDetected = false;
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializePins(void);
- void handleMotionDetection();
- void turnOnRelaysAndLEDs();
- void turnOffRelaysAndLEDs();
- void setup(void)
- {
- initializePins();
- // Initialize relay and LED states to OFF
- turnOffRelaysAndLEDs();
- // Initialize PIR sensor pin
- pinMode(pirSensorPin, INPUT);
- }
- void loop(void)
- {
- handleMotionDetection();
- // Check for inactivity timeout
- if (motionDetected && (millis() - lastMotionTime > inactivityTimeout))
- {
- // No motion detected for timeout period, turn off relays and LEDs
- turnOffRelaysAndLEDs();
- motionDetected = false;
- }
- }
- void initializePins(void)
- {
- // Set relay pins as OUTPUT
- for (int i = 0; i < 4; i++)
- {
- pinMode(relayPins[i], OUTPUT);
- }
- // Set LED pins as OUTPUT
- for (int i = 0; i < 2; i++)
- {
- pinMode(ledPins[i], OUTPUT);
- }
- }
- void handleMotionDetection()
- {
- int pirState = digitalRead(pirSensorPin);
- if (pirState == HIGH)
- {
- // Motion detected
- turnOnRelaysAndLEDs();
- lastMotionTime = millis();
- motionDetected = true;
- }
- }
- void turnOnRelaysAndLEDs()
- {
- // Turn on all relay channels
- for (int i = 0; i < 4; i++)
- {
- digitalWrite(relayPins[i], HIGH);
- }
- // Turn on LEDs
- for (int i = 0; i < 2; i++)
- {
- digitalWrite(ledPins[i], HIGH);
- }
- }
- void turnOffRelaysAndLEDs()
- {
- // Turn off all relay channels
- for (int i = 0; i < 4; i++)
- {
- digitalWrite(relayPins[i], LOW);
- }
- // Turn off LEDs
- for (int i = 0; i < 2; i++)
- {
- digitalWrite(ledPins[i], LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment