21. Chapter Infrared Motion Sensor

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

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

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

21.1.2. Component Knowledge

HC_SR5012

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.

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

How to use this sensor?

HC_SR501_T_B

Description:

  1. You can choose non-repeatable trigger modes or repeatable modes.

L: non-repeatable trigger mode. The module output 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.

  1. R1 is used to adjust HIGH level lasting time when sensor detects human motion, 1.2s-320s.

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

HC_SR501_1

21.1.4. Sketch

In this section, we will utilize an infrared motion sensor to control an LED, where the sensor will function as a motion-activated switch. The code is quite analogous to our previous project “Push Button Switch and LED.” However, unlike the button which outputs a low signal when pressed, the infrared motion sensor outputs a high signal upon detecting motion. Consequently, when the sensor outputs a high level, the LED will illuminate or turn off.

21.1.4.1. Sketch_21_1_InfraredSensor

First, enter where the project is located:

$ cd ~/Freenove_Kit/Pi4j/Sketches/Sketch_21_1_InfraredSensor
../../../_images/java_infrared.png

Enter the command to run the code.

$ jbang SenseLED.java
../../../_images/java_infrared_run.png

Once the code is running, you can observe whether the LED turns on or off by moving away from or closer to the infrared motion sensor.

The terminal will continuously display the LED status, as shown below:

../../../_images/java_infrared_exit.png

Press Ctrl+C to exit the program.

You can run the following command to open the code with Geany to view and edit it.

$ geany SenseLED.java

Click the icon to run the code.

../../../_images/java_infrared_code.png

If the code fails to run, please check Geany Configuration.

The following is program code:

 1///usr/bin/env jbang "$0" "$@" ; exit $?
 2
 3//DEPS org.slf4j:slf4j-api:2.0.12
 4//DEPS org.slf4j:slf4j-simple:2.0.12
 5//DEPS com.pi4j:pi4j-core:2.6.0
 6//DEPS com.pi4j:pi4j-plugin-raspberrypi:2.6.0
 7//DEPS com.pi4j:pi4j-plugin-gpiod:2.6.0
 8
 9import com.pi4j.Pi4J;
10import com.pi4j.io.gpio.digital.DigitalState;
11import com.pi4j.util.Console;
12
13public class SenseLED {
14    private static final int PIN_IR_SENSOR = 17;
15    private static final int PIN_LED = 18;
16
17    public static void myPrintln(String format, Object... args) {
18        Console console = new Console();
19        console.println(String.format("\u001B[32m" + format + "\u001B[0m", args));
20    }
21
22    public static void main(String[] args) throws Exception {
23        final var console = new Console();
24        var pi4j = Pi4J.newAutoContext();
25
26        var led = pi4j.dout().create(PIN_LED);
27        var ir_sensor = pi4j.din().create(PIN_IR_SENSOR);
28
29        ir_sensor.addListener(e -> {
30            if (e.state() == DigitalState.LOW) {
31                myPrintln("Blue LED on. >>> ");
32                led.high();
33            } else if (e.state() == DigitalState.HIGH) {
34                myPrintln("Blue LED off. <<< ");
35                led.low();
36            }
37        });
38
39        try {
40            while (true) {
41                Thread.sleep(500);
42            }
43        } finally {
44            pi4j.shutdown();
45        }
46    }
47}