Public post

8.2 Detecting Motion with the infrared motion sensor

· 8. Sensors

PIR sensors allow you to detect movement/motion. They are used to detect whether a human has moved in or out of the sensor’s range. PIR sensors are commonly found in appliances and gadgets used at home or for businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors. A simple 3-pin (power, ground, and signal) output ensures that you detect everything that passes the sensor.

Introduction

The PIR motion sensor is ideal to detect movement. PIR stands for “Passive Infrared”. Basically, the PIR motion sensor measures infrared light from objects in its field of view.

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.

The module has three more pins with a jumper between two of them. These pins are for selecting the trigger modes. Firstly, there is one called “non-repeatable trigger” and works like this: when the sensor output is high and the delay time is over, the output will automatically change from high to a low level. The other mode called “repeatable trigger” will keep the output high all the time until the detected object is present in the sensor’s range.

PIR-sensor

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 7x
Breadboard 400 points 1x
220Ω resistor 1x
LED 1x
PIR Sensor 1x

Breadboard layout

PIR sensor circuit diagram

Code Example 1 : PIR sensor

/*
 PIR sketch
 a Passive Infrared motion sensor connected to pin 2
 lights the LED on pin 13
*/
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for the PIR sensor)
void setup() {
 pinMode(ledPin, OUTPUT); // declare LED as output
 pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
 int val = digitalRead(inputPin); // read input value
 if (val == HIGH) // check if the input is HIGH
 {
 digitalWrite(ledPin, HIGH); // turn LED on if motion detected
 
 }
 else digitalWrite(ledPin, LOW);
}

Code Explanation

The code that we are using is similar to the pushbutton example shown in controlling a  While you do not have to push anything with the PIR sensor, the sensor acts as a switch when motion is detected. To be able to detect motion we will declare our variable first.

// choose the pin for the LED
const int ledPin = 13;  
 // choose the input pin (for the PIR sensor)
const int inputPin = 2;

In the void setup() part of our sketch, we set the pin of the PIR sensor as input and the pin connected to the LED as output.

void setup() { 
// declare LED as output 
pinMode(ledPin, OUTPUT); 
// declare pushbutton as input }
pinMode(inputPin, INPUT); 

The void loop() part we are executing the real code. Firstly, we define a variable called val with an integer data type. If the condition is met that motion is detected we will turn the LED on.

void loop(){ 
// read input value 
int val = digitalRead(inputPin); 
// check if the input is HIGH 
if (val == HIGH) 
// turn LED on if motion detected 
{ digitalWrite(ledPin, HIGH); 
} 
else digitalWrite(ledPin, LOW); 
}

PIR sensors come in a variety of styles and are sensitive over different distances and angles. The code example above is a way to use the PIR Sensor. Note, that after powering the sensor it may need a little time to function properly.

Now when you will put your hand in front of the sensor the LED will turn on. You can see in the above diagram that there is a “non-repeatable trigger” mode and a “repeatable trigger” mode. If you change the sensor with the jumper to the “repeatable trigger” mode and constantly move your hand in front of the sensor, the LED will be constantly on as long as you move your hand in front of the PIR sensor. After you remove your hand the LED will turn off after the set delay at the sensor.

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.