Motion Sensor (PIR) with ESP8266 and MQTT

LED and PIR in one ESP8266

This setup can be implemented with Arduino MCU unless you want to have the wireless communication for an optional control such as by a Control Terminal or using the MQTT to collect data.

I have posted detailed steps on how to build the limiting current for LEDs. Here is the link again. I would not repeat the required parts here, the only difference is the main resistor, which is 1R 2W or 3W versions.

schematics

Another important component of the build is the software. If you using the larger PIR sensor, you have a rough estimation of the HIGH status of the Pinout (the middle pin in the PIR sensor) between 5 seconds, to a mid-way turn by the trim pot gives you about 2 minutes of HIGH signal out after the last movement was detected. The smaller PIR only relays the HIGH status about 5 seconds, so it can be inconvenient that you have to move continuously in front of the sensor to keep the LEDs on. If you just moved out of the detecting region or stood still for a few seconds, the DATA pin status is LOW, and the LEDs is OFF.

To add a defined delay by the software, I implemented a reset loop inside the main loop. During the last movement was detected (or digitalRead(pir) == 1), the counter is incremented to the maximum time range called onRetain. During that time, if a movement was detected, the counter is set to zero, and the loop starts over.

The snippet of the code is below:

unsigned int onRetain = 30;  //set a 30 seconds delay after the last movement is detected
if (lightState == 1){
    for (int i=0; i < onRetain; i++){
        pirRead = digitalRead(pirPin);
        if (pirRead == 1) {
            i= 0;
        };
        delay(1000); 
    ...

Here is the entire code to upload to the ESP8266 hosted on GitHub . I used 1M-flash version of ESP8266. I made a kitchen light using this approach.

completed wiring A working Kitchen Light