PIR sensors: are they reliable?

PIR sensor
25 Sep
2017

Presence detection is often a difficult problem to solve for a home automator. One of the most common solutions is the PIR sensor, famous for its low price and infamous for its bad accuracy. In today’s article I’m going to try and answer this question: “Is the PIR sensor unreliable? Can I tweak it to make it better?” But before that, a little explaining is in order.

Human body radiation and PIR detection

PIR is an acronym for Passive InfraRed and name itself gives a hint about its mode of operation. The idea behind the sensor is based on a physical principle known as black body radiation: every object at a temperature greater than the absolute zero (-273,15°C) emits a radiation signature that depends indeed on its temperature. Out of curiosity, this phenomenon was explained by Max Planck in 1901 and his theory marked the first step towards the discovery of quantum physics. The human body is an object with a temperature, so it emits a peculiar signature. But how can the sensor detect a specific signature in the IR spectrum? If every object emits some radiation, the noise would be way too high. The answer is that the PIR does not try to identify the human signature, but it simply detects motion.

PIR PCB topThe PIR has a simple sensor that measures the intensity of the waves in the infrared spectrum. The sensor is composed of two identical receptors with different areas of detection. When a body intercepts or leaves the area of one half, the sensor detects a differential in intensity for that half and signals a motion. There is no frequency recognition going on, only intensity comparison. You may be wondering why there are two modules: the answer is environmental noise. For example, the sun is an infrared radiation emitter and when it sets it takes away some IR intensity. This change, however, has nothing to do with human motion. If the delta is detected in both halves of the sensor it is ignored.

PCB overview

PIR PCB bottom

Now that we know the physics behind the device, let’s have a closer look at its structure. The top side of the PIR sensor hosts the lens, which looks like a white dome. This kind of lens is called “Fresnel lens” and it’s engineered so that it focuses the radiation onto the center of the PCB. Once you remove the lens, you will be able to see the double receptor, the component that does the hard work. This is the side that should face the area we want to detect motion in. The bottom side, as shown in the picture, has two orange potentiometers, three regular pins (VCC – output – GND), three retrigger pins, a voltage regulator that outputs 3.3V and the motion detection embedded IC. Therefore, in our attempt to make the sensor better, we will only tweak the potentiometers and the retrigger pins.

 

Wiring and mode of operation

The PIR sensor is very simple to use with an Arduino. You just need to connect VCC to either 5V or 3.3V (the regulator does the work for you), GND to GND and the sensor’s output to a GPIO pin. I recommend to connect a LED and continuously set it to the status of the PIR. You can use this code for testing.

#define LED_PIN 3
#define PIR_PIN 4

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(PIR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
 digitalWrite(LED_PIN, digitalRead(PIR_PIN));
}

The sensor will not work properly as soon as you power it, because it needs some time to adapt to the environment. After a short warm-up period, which is usually less than a minute, the sensor will use two time intervals: the “output-high” interval and the “sleep” interval. When motion is detected, the sensor sets the output to HIGH and then it waits the “output-high” interval before setting it back to LOW. As soon as the output becomes low, the “sleep” period kicks in, to give the sensor some time to recalibrate itself. The PIR won’t detect any motion during this short time span.

The retrigger pins

The detection IC has two modes of operation: retrigger and non-retrigger. The non-retrigger mode works with full “output-high” – “sleep” cycles every time the sensor is triggered. Instead, the other mode keeps the output HIGH and resets the “output-high” timer if motion is detected again while in this state. To activate the first mode the middle retrigger pin has to be shorted to the L pin, while to activate the retrigger it needs to be shorted to the H pin instead. The retrigger mode is usually preferred because the programmer doesn’t need to implement the logic to ignore the “sleep” interval. On the other hand, if the output recipient is sensitive to signal edges, the non-retrigger mode is required (this is usually the case of hardware-oriented projects).

The potentiometers

The orange knobs of the sensor are the most important controls. If badly calibrated they can cause a lot of trouble. To analyse the situation, let’s assume that the PIR sensor directly controls the light of a room.

The right-hand (in the previous picture) potentiometer controls the length of the “output-high” interval, which needs accurate tuning. An excessively short interval can cause the output to become low when someone is standing still for a while (false negative), while a long interval kills the real time features of the system. In our assumed use case, too short means that the lights turn off when someone is still in the room, while too long implies that the light stays on for that entire time span, effectively wasting electricity.

The left potentiometer rules the sensitivity of the device, that is the entity of the differential needed to detect motion. If the sensitivity is too low the sensor may not be able to pick up motion happening far from it (causing a false negative); on the other hand, if it is too high, the sensor might go off when it shouldn’t, causing false positives. If you are in the first condition, you need to wave your hand to be detected (but this defeats the purpose of home automation) and in the second condition the light turns on randomly when the room is empty, again wasting power.

PIR calibration tutorial

This is the algorithm that I like to use to calibrate the sensor. First off, I set the potentiometer in non-retrigger mode and I turn the time delay knob all the way to the left: this way I can control the delay more precisely via software timer (check out my timer library). Then I carefully adjust the sensitivity knob. This can be quite a long winded process, because it involves testing over long time periods.

I recommend connecting the sensor to an ESP8266 to be able to log the status via MQTT. You can use my code template as a starting point to write your testing sketch. To log the MQTT entries to a file, just use this script on your linux machine (remember to change user and password and to give the file execution permission). The script won’t work on Mac OSX. Run the command in a screen session to test over long periods.

$ nano mosquitto-log

#!/bin/bash
# usage: ./mosquitto-log topic1 topic2 > logfile.txt
mosquitto_sub -v -h 192.168.1.4 -u youruser -P yourpassword -t $1 -t $2 | xargs -d$'\n' -L1 sh -c 'date "+[%d/%m/%Y - %T] $0"'

Example of logfile entries:

[24/09/2017 - 20:52:12] test/pir1 OFF
[24/09/2017 - 20:52:17] test/pir1 ON
[24/09/2017 - 20:52:20] test/pir1 OFF
[24/09/2017 - 20:52:26] test/pir1 ON
[24/09/2017 - 20:52:29] test/pir1 OFF
[24/09/2017 - 21:32:40] test/pir1 ON
[24/09/2017 - 21:32:42] test/pir1 OFF
[24/09/2017 - 23:22:11] test/pir1 ON
[24/09/2017 - 23:22:13] test/pir1 OFF
[24/09/2017 - 23:22:19] test/pir1 ON
[24/09/2017 - 23:22:21] test/pir1 OFF
[24/09/2017 - 23:22:26] test/pir1 ON
[24/09/2017 - 23:22:29] test/pir1 OFF

Set the sensitivity to the maximum value (turn clockwise) and start the logging right before you leave the room. Ensure that nobody goes inside the room for at least a couple of hours and then look for false positives in the logfile. It’s better to run this tests during daytime because the sun is a source of disturbance. If there are too many false positives, repeat the test with a lower sensitivity value until you are satisfied with the result. If the sensor goes haywire that is probably due to a bad initial calibration (were you inside the room during the sensor startup?) or a defective device.

Once the sensitivity is set, it is time to define the correct time interval in the ESP software. I used an AsyncTimer object with a thirty-second timeout and implemented a very simple logic. Reset the timer if the PIR output is HIGH and check for timer expiration if it is LOW. When motion is first detected, the virtual presence sensor we created sends a MQTT  “on” message, while it sends an “off” payload upon timer expiration. To carry out the tests you just need to stay inside the detection area and act like the sensor was not there. If the timer expires when you are still in the room (false negative), increase the time and repeat the test. Keep testing until you reach a satisfactory result.

The sensor might fail to detect you in this second phase. If this happens, you are too far away from the sensor with respect to the sensitivity you set earlier: the farther you are, the lower the differential. In this specific situation, you have to carefully trade off time and sensitivity.

Test results

The first test that I ran involved a short range detection area. I set up the sensor 1 m away from my computer seat and I settled the sensitivity knob at 45° to the left. I found that the sensor had no trouble at all detecting me despite the low sensitivity setting. Moreover, the optimal time interval was around 2 minutes, which is not quite as long as I was expecting. The sensor did not detect any false negatives and the low sensitivity prevented false positives: only two in 8 hours.

The second test had an area that was 4m away from the sensor. This time the sensor struggled to detect me and I had to set the time to 5 minutes, which is definitely too long. The increased sensitivity lead to false positives, I had a hard time trying to find the perfect balance between time and sensitivity. Whenever I raised the sensitivity value, the sensor could see me better and the minimum time was shorter, but the number of false positives kept increasing.

Conclusions

The PIR is a good way to detect human presence, but it is far from perfect. I could trace the roots of every flaw I noticed to the same issue: the sensor can identify motion as opposed to mere human presence. Here is a list of all the issues that I found.

  1. The sensor needs some time to assume that the room is not empty. It can only show that nobody has moved for a couple of minutes, but this is as far as it goes. If the sensor controls a light, the bulb turns off with a considerable delay.
  2. The sensor tends to miss small movements. For instance, if a person is chilling on a couch he is less likely to make big movements in a short range of time.
  3. The sensor detects false positives in daylight conditions, no matter what kind of sensitivity setting it has. The false positives can be decreased in number, but it’s impossible to completely suppress them.

I would not recommend this type of sensor for applications that require absolute precision. There are some other sensors out there that use different technologies (such as thermal analysis) and that are exponentially more expensive. For every other use case, the key to success is optimal placement. Every room has a purpose, and every purpose requires some kind of movement. We already stated that the performance of our sensor is better with reduced sensitivity and short range, especially where the purpose requires little motion. Also, the presence of sunlight is another important factor to consider. And most certainly, a single PIR is certainly not enough to cover an entire room. Let’s analyse some common use cases.

The PIR sensor is perfect for hallways. A hallway is usually a long, narrow and dark room with no sunlight access. An array of sensors placed every other meter on the long side is very likely to solve your problems. However, any bigger room that has access to sunlight needs more attention. As a general rule, place a sensor wherever there is an object related to relax, like a chair or a bed. These are the places where people are less likely to move. If you place enough sensors, chances are that the room will have full coverage. Pay attention not to place the PIRs directly in front of a sunlight source.

In conclusion, PIR sensors aren’t the most reliable detection system in the world. However, with a smart placing policy and a precise tweaking, they can be a powerful instrument to create functional home automation projects. If you want to use them in your projects, be sure to check out the Home Assistant category of my website!

 

 

 

Search bar

Subscribe

Did you miss my latest post? Subscribe to be notified whenever I pusblish a new article!

Archive

Social links

%d bloggers like this: