pir-motion-sensor

Rust library to interact mainly with PIR motion sensors. This lib was tested on HC-SR501 on Raspberry Pi 400 and Raspberry Pi 4B and it's widely used at my appartment and at my familys house - for smart alarm purposes (code will be published) and some in-house activies like turning on/off various devices based on motion detection.

 

Tested devices

Raspberry Pis: - Raspberry Pi 4B 4 and 8 GB RAM - Raspbian GNU/Linux 11 (bullseye) - Raspbbery Pi 400 4 GB RAM - Raspbian GNU/Linux 11 (bullseye)

Sensors:

 

Why this library?

HC-SR501 PIR is very cheap and widely available an infrared motion sensor and it's very capable to various project based on arduino/raspberry/stm32, etc.

However, because it's very cheap, it can detects "noise" from time to time, which means detection happens when there are no real motion within it's detection range - this is a false positive detection.

To eliminate this problem I created this simple library which allows to initialize this sensor with various parameters that they changes it's detection characteristic. The library, based on sensor configuration, can "ignore" these false detections and help make this sensors very reliable.

HC-SR501 PIR is not probably the only one infrared sensor which can be supported by the library - if you tested with another motion sensor (or even with microwave) please let me know.

 

Prerequsities

Use this reference manual for HC-SR501 PIR: https://lastminuteengineers.com/pir-sensor-arduino-tutorial/

Based on this manual, you should:

Allow of above settings can be programmatically changed by this library, so you won't need to touch jumper and screws again anymore after this operation.

If you are not sure if you did it correctly - attach your sensor to VCC (5V), GND of your raspberry, and simple LED diode with resistor (betwen 1-10 kΩ) to OUT signal from the sensor and based on LED check how this sensor work after these adjustments. Be aware, that sensors sets ~5V on it's OUT PIN, when it detects motion, which is at high state.

 

How it works

In this instruction, there is a term valid detection - this is a detection which is classified as valid by this library - which may be not the same as OUT pin state of the sensor - everything depends on your configuration.

In other words: depends on sensor configuration, there can be many detections made by sensor (here defined as setting it's OUT pin at high state), but it does not mean, there will be any valid detection classified.

 

Configuration

To init your sensor, consider the following parameters:

To short sum up these parameters: based on sensor refresh rate time it periodically reads state of sensor OUT pin. If there is a detection (or high state), the library will try to count up these states to minimal triggering number within motion time period. If minimal triggering number within motion time period is reached, then it means valid detection (from library standpoint) just happened.

Setting these parameters allow you to decide how sensitive and accurate is your sensor.

 

Using in your project

 

Contributions

Contributions highly welcomed.

 

TODO