Motion Sensor (PIR) with ESP8266 and MQTT

Basics on Motion Sensor (PIR)

This simple chart below is the best I found to explain how PIR works:

PIR Principal
A working principle of motion (PIR) sensor. A heatsource moves accross the PIR sensor send a HIGH signal

This step covers the basics about part/form of PIR sensor, Arduino code to learn about the PIR, and a dedicated video to show what L setting looks like on the larger PIR version.

There are two (physical) forms of PIR sensors. One is smaller and looks simpler that costs about $1.28 on Aliexpress. The larger version (HC SR505) has two trimmer pots for the time delay and sensitivity setting, a jumper to select auto-trigger, and costs about $0.82 on Aliexpress. Both forms has an identical voltage regular (HT7133) and can accept the same input DC (max 24V, min ~5.0V). The typical current HT7133 output at 5.5V input is about 30mA, so soldering VCC wire to ESP8266 could overheat the IC. The ESP8266 consumes 100mA on average

PIR sensors
A look up of a larger PIR version
PIR readouts
A readouts from Serial Monitor

The link to Adafruit has a detailed calculation of the time delay for the larger PIR version. For the larger version, the lowest (shortest) time delay setting relays HIGH status in 4-5 seconds after the last motion is detected. A half-way setting relays about 117- 120 seconds in HIGH status.

I was confused about H-L jumper on the larger version. If you are, watch the YouTube video about the L setup - or not auto trigger. With a jumper on L, the HIGH on Pinout only if there was a LOW period in between HIGH period. The smaller version does not come with this setup and has the only AUTO-TRIGGERING mode.

Below is the code I used to upload to an Arduino Nano:

void setup() {
    pinMode(4, INPUT);  //Pin 4 <> the larger PIR
    pinMode(5, INPUT);	//Pin 5 <> the smaller PIR
    pinMode(2, OUTPUT);	//Pin 2 <> blue LED (indicates the status of the larger PIR)
    pinMode(11, OUTPUT);  //Pin 11 <> red LED (indicates the status of the smaller PIR)

    int id = 0;
void loop() {
    int lPIR = digitalRead(4);
    int sPIR = digitalRead(5);
    digitalWrite(2, lPIR);
    digitalWrite(11, sPIR);
    Serial.print("\tLarge PIR:  ");
    Serial.print("\tSmall PIR:  ");
  1. Adatafruit has a detailed writing about PIR sensor from the basic, wiring, connect with Arduino and with Python-enabled devices.
  2. This online listing has a detail specification of the larger PIR version.