Technology

Off-The-Shelf Hacker: Create an Early Warning Detector with Passive Infrared Sensors

8 Oct 2016 6:14am, by

For years I’ve wanted to build a constellation of passive infrared sensors (PIR) in my yard, giving me early warning of an intruder and perhaps activate the appropriate force field, on demand. Alarm systems are great when somebody tries to break into your house. What about lunatics from the local funny farm, the occasional snarling rabid dog or maybe even zombies? That extra 10-15 seconds of notice might be the difference between life and … well, you know.

Will a PIR sensor even pick up a zombie, since they’re sort of dead already and likely not emitting any infrared energy? We can’t know the answer to that unless we build a device and try it out. In a true Off-The-Shelf Hacker fashion, let’s hack a prototype.

PIR 101

Passive infrared sensors are digital devices that detect heat. Mammals emit tiny amounts of infrared heat by the very fact that they are living. Electronics monitor two halves of the sensor chip inside the device, and the signals cancel each other out when there’s no difference between them, which would otherwise indicate infrared light falling on the chip. If one-half senses a different level of IR light, the PIR sensor fires a pulse to the output pin indicating movement. There’s a lens system in front of the sensor that helps focus the infrared energy on the chip itself.

There are two types of PIR sensors and the output pin will go either high or low to indicate an object. The device used in this project is known as an “open collector,” meaning that the output is held high with a pull-up resistor until an object passes in front of the detector. The output is then pulled low, thus signaling detection. The open collector signaling method makes it easy to daisy chain multiple sensors on the same 3-wire bus.

I purchased my PIR sensor from Sparkfun and it’s of the open collector persuasion. The output pin goes low, with detection. Arduino marketplace Adafruit sells a newer, more adjustable sensor. Its output is normally low and goes high on detection. So, if you pirate code from the Internet, make sure you look for the appropriate high or low output. It’s a simple matter to reverse the “LOW” and “HIGH” values when you read the microcontroller input pin to properly interpret your detection events.

Wire It Up

One cool characteristic of microcontrollers is that you can take basic circuits and just switch input devices, to get different behaviors. For example, to build this project, I re-purposed the wireless temperature sensor, from a past OTSH project and just swapped the DS18B20 digital thermometer for the PIR sensor. Both sensors are digital and adjustments for the different devices are easily made in the code.

Take a look at the breadboard:

ESP8266-PIR Breadboard

ESP8266-PIR Breadboard

Notice the wire colors from the PIR sensor to the breadboard. My device has red, white and black wires. Red is for VCC (positive). This particular sensor is rated for from 5 to 12 volts. Oddly, the white wire goes to ground (negative). Lastly, the black wire is output. The output of the PIR sensor connects to the general purpose digital input/output (GPIO) pin number 2 on the ESP8266 micro-controller. You’d think the black would be ground and the white would be the output. Nope. Don’t worry, I’ve hooked mine up wrong without ill effects.

Adafruit has a slightly more advanced PIR sensor with a little header and adjustable delay and sensitivity. The header has the ground pin on the left, output in the middle and VCC on the right, so wire color is your choice. The Banggood sensor looks to be the same as the Adafruit offering.

Other sensors might use different pin configurations or wire colors. Don’t forget that if you buy an Adafruit device, you’ll need to swap your logic around to respond to a HIGH output, upon detection, instead of a LOW output.

My PIR sensor was also a bit sensitive to input voltage. I ran it from the 5 volts supplied by the USB connection and it gave a lot of false positives. Increasing the voltage (to the PIR sensor) to 9 volts gave very reliable operation. At that voltage, I could detect objects to about eight feet.

Remember that the module detects the difference between the two halves of the sensor, so the object has to be moving for the output to change. You can connect the positive side of a 9-volt battery to the positive PIR sensor lead while removing the link that goes to +5 volts. Connect the negative side of the battery to the common ground. Connect the negative lead of the sensor to the common ground as well.

Bending Code

Much of the code I used was from the previous ESP8266 temperature sensor project. As before, you can initially join the ESP8266-PIR sensor to your local LAN using the on-board access point and web configuration page, using your smartphone.

Also, notice that I used the built-in standard “pin 13” LED to tell when the PIR sensor fired. Pin 13 is typically connected to a LED on Arduino projects for basic operation and testing.

Further down in the code, I also added a little counter that prints out to the serial monitor, showing how many times the PIR sensor pings the input pin. It will reset to zero during a reboot. I used the sequential numbers for testing and diagnostics.

It took a while to get the PIR sensor to work with the ESP8266, illustrating one of the challenges of hacking physical computing devices.

While firmware-based microcontrollers, like the Arduino and ESP8266, are considered very fast, they have their limitations and you need to take those into account when building your projects. Add in a WiFi stack, as in the case of the ESP8266 and your real-time operation will likely have to make a few compromises.

I wanted to pick up all movement in front of the sensor and send a message to another computer on my LAN for logging and alarm purposes. The monitor machine could be a Linux laptop, a Raspberry Pi or even another ESP8266 device.

So, when the 8266 is sitting there in the main loop watching the PIR sensor pin for a state change, you have to allow the WiFi stack to occasionally take control, otherwise, the built-in watchdog timer (WDT) will trip and cause the microcontroller to reset. It’s a safety feature to prevent endless loops and other such problems.

It turned out that I had to add in a few delays to give the WiFi radio time to do its communication thing between sensor readings. A 200 ms delay between PIR readings worked well. There’s little chance that the PIR detector will miss seeing a moving object in that short time span. You’ll usually get multiple state changes anyway, in quick succession, when an object moves in front of the sensor.

Put in the delays, boost the PIR supply voltage to at least 9 volts and the sensor becomes rock solid.

At the other end of the network connection on my Linux notebook, is the netcat command, listening and printing out the “motion” text, whenever the PIR sensor fires. As spelled out in the other article, piping the output of netcat (we’re using Linux after all) to a file and adding in time/date information, using AWK, lets you log data without having to implement time/date routines on the 8266. Keeping it simple makes it easy to troubleshoot and maintain.

Next Steps

I used an ESP8266 model 07 for this project. The 07 has 9 GPIO pins. All we need for this sensor is one pin. Why not see if we can get the same functionality with an ESP8266 model 01?

I’ll explore what it takes to move from the “overkill” 07 model to the bare-bones 01 device, in a future OTSH article.

In the meantime, I’ll go round up test zombies.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.