Added new file
This commit is contained in:
100
pir_light_relay_sleep_light_voltage.ino
Normal file
100
pir_light_relay_sleep_light_voltage.ino
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* ESP32 PIR Motion Light Controller with Deep Sleep
|
||||||
|
*
|
||||||
|
* This sketch uses a PIR motion sensor to wake the ESP32 from deep sleep,
|
||||||
|
* turns on a light via relay for a configurable duration, then returns to
|
||||||
|
* deep sleep after a delay to save power.
|
||||||
|
*
|
||||||
|
* Hardware:
|
||||||
|
* - ESP32 Dev Module
|
||||||
|
* - PIR Motion Sensor
|
||||||
|
* - Relay Module
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "esp_sleep.h"
|
||||||
|
#include "driver/rtc_io.h"
|
||||||
|
|
||||||
|
// Pin definitions
|
||||||
|
const int PIR_PIN = 13; // GPIO pin connected to PIR sensor output
|
||||||
|
const int RELAY_PIN = 12; // GPIO pin connected to relay control
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
//const int LIGHT_ON_DURATION = 30000; // Duration to keep light on (30 seconds)
|
||||||
|
const int SLEEP_DELAY = 10000; // Delay before going to sleep (10 seconds)
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
unsigned long motionDetectedTime = 0;
|
||||||
|
bool lightOn = false;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(1000); // Allow serial to initialize
|
||||||
|
|
||||||
|
Serial.println("ESP32 PIR Motion Light Controller");
|
||||||
|
|
||||||
|
// Configure pins
|
||||||
|
pinMode(PIR_PIN, INPUT);
|
||||||
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
|
|
||||||
|
// Turn off relay initially
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
|
||||||
|
// Check wake-up reason
|
||||||
|
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||||
|
|
||||||
|
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0) {
|
||||||
|
Serial.println("Woken up by external signal (PIR sensor)");
|
||||||
|
// Motion detected, turn on light
|
||||||
|
turnLightOn();
|
||||||
|
} else {
|
||||||
|
Serial.println("First boot or reset");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Check if motion is detected (for initial boot or if already awake)
|
||||||
|
if (digitalRead(PIR_PIN) == HIGH && !lightOn) {
|
||||||
|
Serial.println("Motion detected!");
|
||||||
|
turnLightOn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's time to turn off the light
|
||||||
|
if (lightOn && (millis() - motionDetectedTime > LIGHT_ON_DURATION)) {
|
||||||
|
Serial.println("Light timer expired, turning off");
|
||||||
|
turnLightOff();
|
||||||
|
|
||||||
|
// Wait a bit before going to sleep
|
||||||
|
Serial.println("Waiting before sleep...");
|
||||||
|
delay(SLEEP_DELAY);
|
||||||
|
|
||||||
|
// Go to deep sleep
|
||||||
|
goToSleep();
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(100); // Small delay to prevent CPU hogging
|
||||||
|
}
|
||||||
|
|
||||||
|
void turnLightOn() {
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
lightOn = true;
|
||||||
|
motionDetectedTime = millis();
|
||||||
|
Serial.println("Light turned ON");
|
||||||
|
}
|
||||||
|
|
||||||
|
void turnLightOff() {
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
lightOn = false;
|
||||||
|
Serial.println("Light turned OFF");
|
||||||
|
}
|
||||||
|
|
||||||
|
void goToSleep() {
|
||||||
|
Serial.println("Going to deep sleep now");
|
||||||
|
Serial.flush();
|
||||||
|
|
||||||
|
// Configure wake-up source (PIR sensor connected to GPIO)
|
||||||
|
esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, HIGH); // Use RTC GPIO for wake-up
|
||||||
|
|
||||||
|
// Enter deep sleep
|
||||||
|
esp_deep_sleep_start();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user