(DKHK100200)Lesson15-HC-SR501 infrared sensor module for arduino

Content

  1. Introduction
  2. Preparations
  3. About the PIR Motion sensor
  4. Examples

Introduction

A passive infrared sensor (PIR Motion sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors. So, it can detect motion based on changes in infrared light in the environment. It is ideal to detect if a human has moved in or out of the sensor range. In this lesson we will learn how a PIR Sensor works and how to use it with the Arduino Board for detecting motion.

Preparations

Hardware

Software

About PIR Motion sensor

Overview

PIR Motion Sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors.

Use this Arduino motion sensor to build burglar alarm systems, home automation systems, or any simple gadget that prevents people from getting into your room!

Specifications

PIRs are basically made of a pyroelectric sensor (which you can see above as the round metal can with a rectangular crystal in the center), which can detect levels of infrared radiation. Everything emits some low level radiation, and the hotter something is, the more radiation is emitted. The sensor in a motion detector is actually split in two halves. The reason for that is that we are looking to detect motion (change) not average IR levels. The two halves are wired up so that they cancel each other out. If one half sees more or less IR radiation than the other, the output will swing high or low.

This sensor is then placed behind a multifaceted lens (a Fresnel lens) that “chops up” the view of the world into smaller cones of heightened visibility and intervening areas of lessened visibility thus widening the useful viewing /detection angle dramatically.

PIR Sensing Angle diagram:

Along with the pyroelectic sensor is a bunch of supporting circuitry, resistors and capacitors. It seems that most small hobbyist sensors use the BISS0001 (“Micro Power PIR Motion Detector IC”), undoubtedly a very inexpensive chip. This chip takes the output of the sensor and does some minor processing on it to emit a digital output pulse from the analog sensor.

Our PIR Motion Sensors looked like this:

Pin or Control Function
Delay Time Adjust Sets how long the output remains high after detecting motion…. Anywhere from 5 seconds to 5 minutes.
Sensitivity Adjust Sets the detection range…. from 3 meters to 7 meters
Ground pin Ground input
Digital Output Pin Low when no motion is detected.. High when motion is detected. High is 3.3V
Power Pin 4.5 to 20 VDC Supply input

How Does it Work?

Here, we are using a PIR motion sensor. PIR stands for Passive InfraRed. This motion sensor consists of a fresnel lens, an infrared detector, and supporting detection circuitry. The lens on the sensor focuses any infrared radiation present around it towards the infrared detector. Our bodies generate infrared heat and as a result, this gets picked up by the motion sensor. The sensor outputs a 5V signal for a period of one minute as soon as it detects the presence of a person. It offers a tentative range of detection of about 6-7 m and is highly sensitive.

When the PIR motion sensor detects a person, it outputs a 5V signal to the Arduino. Thus, an interrupt on Arduino is triggered. We define what the Arduino should do as it detects an intruder.

Sensitivity Adjustment

As mentioned, the adjustable range is from approximately 3 to 7 meters. The illustration below shows this adjustment. You may click to enlarge the illustration.

Delay Time Adjustment

The time delay adjustment determines how long the output of the PIR sensor module will remain high after detection motion. The range is from about 5 seconds to five minutes. The illustration below shows this adjustment.

Trigger Mode Selection Part

The trigger mode selection jumper allows you to select between single and repeatable triggers. The affect of this jumper setting is to determine when the time delay begins.

5 Seconds Off After Time Delay Completes – IMPORTANT

The output of this device will go LOW (or Off) for approximately 5 seconds after the time delay completes. In other words, all motion detection is blocked during this three second period.

For Example:

Examples

PIR Motion Sensor Control LED

In this project you’re going to create a simple circuit with an Arduino and PIR motion sensor that can detect movement. An LED will light up when movement is detected.

Connection

Build the circuit as below:

Connecting PIR sensors to a microcontroller is really simple. The PIR acts as a digital output so all you need to do is listen for the pin to flip high (detected) or low (not detected).

Power the PIR with 5V and connect ground to ground. Then connect the output to a digital pin. In this example we’ll use pin 2.

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

int ledPin = 13; // choose the pin for the LED int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input Serial.begin(9600); } void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { digitalWrite(ledPin, LOW); // turn LED OFF if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } }

This code just keeps track of whether the input to pin 2 is high or low. It also tracks the state of the pin, so that it prints out a message when motion has started and stopped.

Running Result

A few seconds after the upload finishes, have a look at your Arduino’s pin 13 LED. You can also open your serial monitor, and set the baud rate to 9600 bps, you may see the following:

The PIR sensor requires a couple seconds of motion-free activity, while it gets a “snapshot” of it’s viewing area. Try not to move until the pin 13 LED turns off, then wave your hands, jump in the air, go crazy!

You will also notice that there is a delay associated with the motion sensor after each detection. Depending on the sensor, you may be able to adjust this delay.

PIR Motion Sensor Control Relay

As an example for this lesson I will make a circuit that will turn on a relay to control some high voltage things when the sensor will detect an object.

Connection

Build the circuit as below:

The output pin of the sensor will be connected to pin number 2 on the Arduino Board and when an object will be detected the pin number 3 will activate the relay module and the high voltage lamp will turn on. For more details how the relay module works, you can check the sketch below.

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.

int ledPin = 13; // choose the pin for the LED int relayInput = 3; // choose the pin for the relay int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input pinMode(relayInput, OUTPUT); // declare relay as output digitalWrite(relayInput, HIGH);//assuming relay is off Serial.begin(9600); } void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; digitalWrite(relayInput, LOW); // The Relay Input works Inversly Serial.println("Turn on the Lamp!"); Serial.println("Wait for 30 seconds"); delay(30000); // delay for 30 seconds digitalWrite(relayInput, HIGH);// Relay input operation is positive Serial.println("Turn off the Lamp!"); } } else { digitalWrite(ledPin, LOW); // turn LED OFF if (pirState == HIGH) { // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } }

Here’s the Arduino Code for this example. It’s quite simple. We just need to define the PIR Sensor pin as input and the relay pin as output. Using the digitalRead() function we will read the output of the sensor and if its high or if an object is detected it will activate the relay. For activating the relay module we will send a logic low as the relay input pin works inversely.

Running Result

A few seconds after the upload finishes, you will see as below:

Note that after powering the sensor module it needs about 20 – 60 seconds to “warm-up” in order to function properly. Now when you will put your hand in front of the sensor the relay will activate the lamp. But note that even if you move your hand constantly the lamp will turn off after the adjusted delay time is over because the PIR sensor is in “non-repeatable trigger” mode. If you change the sensor with the jumper to the “repeatable trigger” mode and you constantly move the hand, the lamp will be constantly on as well and it will turn off after the movement is gone and the set delay time is over.

Keep in minds that if you are using high voltage in this example, so you should be very caution.