Learn to code

ARDUINO & CODES

Previous Post
Create a Touch Button with copper /aluminum foil
Next Post
Detecting distance with HC-SR04 sensor

Detecting motion with PIR sensor

in Arduino 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 pins

Hardware required:

  • Arduino Uno
  • Breadboard
  • PIR sensor
  • hook-up wires
  • LED
  • 220-ohm resistor

Circuit Diagram

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 4.1 Control a LED with a button. 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.

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

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() {
 pinMode(ledPin, OUTPUT); // declare LED as output
 pinMode(inputPin, INPUT); // declare pushbutton as 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(){
 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);
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu