18. Chapter Infrared Motion Sensor

In this chapter, we will learn a widely used sensor, Infrared Motion Sensor.

18.1. Project 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.

18.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

Jumper Wires x5

jumper-wire

HC SR501 x1

HC_SR501

LED x1

red-led

Resistor 220Ω x1

res-220R

18.1.2. Component Knowledge

HC_SR501

HC_SR501_bottom

HC_SR501_Schematic

Description:

  1. Working voltage: 5v-20v(DC) Static current: 65uA.

  2. 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.

  3. 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

  4. 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.

  5. 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.

18.1.3. Circuit

Schematic diagram

HC_SR501_Sc

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

HC_SR501_Fr

18.1.4. Sketch

18.1.4.1. Sketch SenseLED

First, observe the result after running the sketch, and then learn about the code in detail.

  1. Use Processing to open the file Sketch_18_1_1_SenseLED.

$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_18_1_1_SenseLED/Sketch_18_1_1_SenseLED.pde
  1. Click on “RUN” to run the code.

After the program is executed, Display Window shows the following interface, where the middle circle indicates the LED. Color of the circle is white when LED is off, green when LED is on.

../../../_images/sense.png

The following is program code:

 1/*****************************************************
 2 * Filename    : Sketch_18_1_1_SenseLED
 3 * Description : Control the led by Infare motion sensor
 4 * auther      : www.freenove.com
 5 * modification: 2024/09/04
 6 *****************************************************/
 7import freenove.processing.io.*;
 8
 9final int sensorPin = 17;  //connect to sensor pin
10final int ledPin = 18;    //connect to led pin
11void setup() {
12  size(640,360);    //window size
13  GPIO.pinMode(sensorPin, GPIO.INPUT); 
14  GPIO.pinMode(ledPin, GPIO.OUTPUT);
15}
16
17void draw() {
18  background(102);
19  titleAndSiteInfo();
20  //if read sensor for high level
21  if (GPIO.digitalRead(sensorPin) == GPIO.HIGH) {
22    GPIO.digitalWrite(ledPin, GPIO.HIGH);  //led on
23    fill(64,255,64);    //fill in green
24  } else {
25    GPIO.digitalWrite(ledPin, GPIO.LOW);  //led off
26    fill(255);        //fill in white
27  }
28  ellipse(width/2,height/2,height/2,height/2);
29}
30
31void titleAndSiteInfo() {
32  fill(0);
33  textAlign(CENTER);    //set the text centered
34  textSize(40);        //set text size
35  text("SENSE LED", width / 2, 40);    //title
36  textSize(16);
37  text("www.freenove.com", width / 2, height - 20);    //site
38}

In this project, the code is relatively simple. In the function draw(), read level of sensor pin. When it is a high level, LED is turned on. At the same time the filled color will be changed to green. When the level is low, LED turns off and the filled color turns white. Finally, it draws a circle.

 1void draw() {
 2  background(102);
 3  titleAndSiteInfo();
 4  //if read sensor for high level
 5  if (GPIO.digitalRead(sensorPin) == GPIO.HIGH) {
 6    GPIO.digitalWrite(ledPin, GPIO.HIGH);  //led on
 7    fill(64,255,64);    //fill in green
 8  } else {
 9    GPIO.digitalWrite(ledPin, GPIO.LOW);  //led off
10    fill(255);        //fill in white
11  }
12  ellipse(width/2,height/2,height/2,height/2);
13}