Chapter 22 Infrared Motion Sensor
In this chapter, we will learn a widely used sensor, Infrared Motion Sensor.
Project 22.1 PIR Infrared Motion Detector with LED Indicator
In this project, we will make a Motion Detector, with the human body infrared pyroelectric sensors.
When someone is in close proximity to the Motion Detector, it will automatically light up and when there is no one close by, it will be out.
This Infrared Motion Sensor can detect the infrared spectrum (heat signatures) emitted by living humans and animals.
Component Knowledge
The following is the diagram of the Infrared Motion Sensor(HC SR-501)a PIR Sensor:
Top |
Bottom |
Schematic |
|
|
|
Description:
Working voltage: 5v-20v(DC) Static current: 65uA.
Automatic Trigger. When a living body enters into the active area of sensor, the module will output high level (3.3V). When the body leaves the sensor’s active detection area, it will output high level lasting for time period T, then output low level(0V). Delay time T can be adjusted by the potentiometer R1.
According to the position of Fresnel lenses dome, you can choose non-repeatable trigger modes or repeatable modes.
L: non-repeatable trigger mode. The module outputs high level after sensing a body, then when the delay time is over, the module will output low level. During high level time, the sensor no longer actively senses bodies.
H: repeatable trigger mode. The distinction from the L mode is that it can sense a body until that body leaves during the period of high level output. After this, it starts to time and output low level after delaying T time.
Induction block time: the induction will stay in block condition and does not induce external signal at lesser time intervals (less than delay time) after outputting high level or low level
Initialization time: the module needs about 1 minute to initialize after being powered ON. During this period, it will alternately output high or low level.
One characteristic of this sensor is when a body moves close to or moves away from the sensor’s dome edge, the sensor will work at high sensitively. When a body moves close to or moves away from the sensor’s dome in a vertical direction (perpendicular to the dome), the sensor cannot detect well (please take note of this deficiency). Actually this makes sense when you consider that this sensor is usually placed on a celling as part of a security product. Note: The Sensing Range (distance before a body is detected) is adjusted by the potentiometer.
We can regard this sensor as a simple inductive switch when in use.
Component List
Freenove Projects Board for Raspberry Pi |
|
Raspberry Pi |
GPIO Ribbon Cable |
Jumper Wire |
HC SR501 |
Circuit
Schematic diagram |
|
Hardware connection: |
|
Note
If you have any concerns, please send an email to: support@freenove.com
How to use this sensor?
Top |
Bottom |
|
|
Description:
You can choose non-repeatable trigger modes or repeatable modes.
L: non-repeatable trigger mode. The module outputs high level after sensing a body, then when the delay time is over, the module will output low level. During high level time, the sensor no longer actively senses bodies.
H: repeatable trigger mode. The distinction from the L mode is that it can sense a body until that body leaves. After this, it starts to time and output low level after delaying T time.
R1 is used to adjust HIGH level lasting time when sensor detects human motion, 1.2s-320s.
R2 is used to adjust the maxmum distance the sensor can detect, 3~5m.
Here we connect L and adjust R1 and R2 like below to do this project.
Put you hand close and away from the sensor slowly. Obsever the LED in previous circuit.
It need some time between two detections.
Note
If you have any concerns, please send an email to: support@freenove.com
Code
In this project, we will use the Infrared Motion Sensor to trigger an LED, essentially making the Infrared Motion sensor act as a Motion Switch. Therefore, the code is very similar to the earlier project “Push Button Switch and LED”. The difference is that, when Infrared Motion Sensor detects change, it will output high level; when button is pressed, it will output low level. When the sensor output high level, the LED turns ON, or it will turn OFF.
C Code SenseLED
First, observe the project result, and then learn about the code in detail.
Note
If you have any concerns, please send an email to: support@freenove.com
Use cd command to enter 22_1_InfraredSensor directory of C code.
$ cd ~/Freenove_Kit/Code/C_Code/22_1_InfraredSensor
Use following command to compile “SenseLED.c” and generate executable file “SenseLED”.
$ gcc SenseLED.c -o SenseLED -lwiringPi
Run the generated file “SenseLED”.
$ ./SenseLED
After the program runs, wait 1 minute for initialization. Then move away from or move closer to the Infrared Motion Sensor and observe whether the LED turns ON or OFF. The Terminal window will continuously display the state of LED. As is shown below:
The following is the program code:
1/**********************************************************************
2* Filename : SenseLED.c
3* Description : Control led with infrared Motion sensor
4* Author : www.freenove.com
5* modification: 2024/07/29
6**********************************************************************/
7#include <wiringPi.h>
8#include <stdio.h>
9
10#define ledPin 17 //define the ledPin
11#define sensorPin 24 //define the sensorPin
12
13int main(void)
14{
15 printf("Program is starting ... \n");
16
17 wiringPiSetupGpio();//Initialize wiringPi. Use BCM Number.
18
19 pinMode(ledPin, OUTPUT);
20 pinMode(sensorPin, INPUT);
21
22 while(1){
23
24 if(digitalRead(sensorPin) == HIGH){ //if read value of sensor is HIGH level
25 digitalWrite(ledPin, HIGH); //make led on
26 printf("led turned on >>> \n");
27 }
28 else {
29 digitalWrite(ledPin, LOW); //make led off
30 printf("led turned off <<< \n");
31 }
32 }
33
34 return 0;
35}
36
Python Code 22.1 SenseLED
First, observe the project result, and then learn about the code in detail.
If you have any concerns, please send an email to: support@freenove.com
Use cd command to enter 22_InfraredSensor directory of Python code.
$ cd ~/Freenove_Kit/Code/Python_GPIOZero_Code/22_InfraredSensor
Use Python command to execute code “SenseLED.py”.
$ python SenseLED.py
After the program runs, wait 1 minute for initialization. Then move away from or move closer to the Infrared Motion Sensor and observe whether the LED turns ON or OFF. The Terminal window will continuously display the state of LED. As is shown below:
The following is the program code:
1#!/usr/bin/env python3
2########################################################################
3# Filename : SenseLED.py
4# Description : Control led with infrared Motion sensor.
5# auther : www.freenove.com
6# modification: 2024/07/29
7########################################################################
8from gpiozero import LED, Button
9
10led = LED(17)
11button = Button(24)
12
13def loop():
14 key_state = 0
15 while True:
16 if button.is_pressed==True and key_state == 0: # if button is pressed
17 led.on() # turn on led
18 key_state = 1
19 print("Button is pressed, led turned on >>>") # print information on terminal
20 elif button.is_pressed==False and key_state == 1:
21 led.off() # turn off led
22 key_state = 0
23 print("Button is released, led turned off <<<")
24
25if __name__ == '__main__': # Program entrance
26 print ('Program is starting...')
27 try:
28 loop()
29 except KeyboardInterrupt: # Press ctrl-c to end the program.
30 print("Ending program")
31 finally:
32 led.close()
33 button.close()








