How to control light on movement detection
What you will learn here about controlling room lights on movement detection
- How to control light on movement detection
Sometimes we want to control room lights on human movement detection and turn off room lights when movement is not detected So here we will see how to control room lights based on human movement detection.
Hardware requirements:
- PIR sensor
- ESP8266
- USB to power ESP8266
- Relay
- Light or blub
- Connecting wires
How to control light on movement detection
Hardware connection
- Connect VCC pin of PIR to 3.3v or Vin of ESP8266
- Connect the output of PIR to GPIO14 i.e D5 of ESp8266
- Connect GND of PIR to GND of ESP8266
- Connect GPIO13 i.e D7 to relay input (primary winding)
- Connect GND of ESP8266 to GND of the relay (primary winding GND)
ESP8266 Code
Please upload the following code in ESP8266.
int value = 0; int past=0; void setup() { // Motion sensor is connected to D5 pin of ESP8266 pinMode(14,INPUT); //COnfigured as input //relay is connected to D7 pin of ESP8266 pinMode(13,OUTPUT); //Configured as ouput Serial.begin(115200); Serial.println("please connect sensor to D5 pin of ESP8266"); Serial.println("please connect D7 pin of ESP8266 to relay input winding"); } void loop() { value=digitalRead(02); //reads movement data from pir sensor if(value==1 && past==0) { Serial.println("present"); digitalWrite(13,HIGH); //Turn on light blub } if(value==0 && past==1) { Serial.println("Absent"); digitalWrite(13,LOW); //Turn off light blub } past=value; }
How this works:
When PIR sensor detects human movement ESP8266 Sends logic 1 signal to GPIO 13 and relay activates and blub turn ON which is shown below.
When PIR sensor does not detect human movement ESP8266 Sends logic 0 signal to GPIO 13 and relay deactivates and blub turn OFF which is shown below.