How to Connect PIR Sensor With Arduino Microcontroller
by Lisleapex Blog in Circuits > Arduino
247 Views, 1 Favorites, 0 Comments
How to Connect PIR Sensor With Arduino Microcontroller
Detecting motion or movement has always been important in most projects. With the help of PIR sensor, detecting human/animal movement becomes very easy. In this project, we will learn how to interface a PIR sensor with a microcontroller like Arduino. We connected the Arduino with the PIR module and made it flash the LED and make a buzzer beep when movement is detected. The following components are required to build this project.
Supplies
- Infrared sensor module
- Arduino UNO (any version)
- led
- buzzer
- Breadboard
- Connecting line
- 330 ohm resistor
Learn About Infrared Sensor
PIR sensor stands for Passive Infrared Sensor. It is a low-cost sensor that can detect the presence of humans or animals. There are two important materials in the sensor, one is a pyroelectric crystal that can detect the heat signal of an organism (human/animal), and the other is a Fresnel lens that can expand the range of the sensor. In addition, the PIR sensor module also provides us with some options to adjust the sensor operation, as shown in the figure below.
Two potentiometers (orange) control the sensor's sensitivity and triggering time. Basically, the Dout pin of the sensor is between the Vcc and Gnd pins. The module operates at 3.3V, but can also be powered by 5V. In the upper left corner it also has trigger pin settings that can be used to make the module work in two different modes. One is "H" mode and the other is "I" mode.
In "H" mode, when someone is detected within range, the output pin Dout will go high (3.3V) and go low after a specific time (time set by potentiometer). In this mode, the output pin will go high regardless of whether the person is still in range or has left the area. We use our module in "H" mode in our project.
In "I" mode, when a person is detected to be within the range, the output pin Dout will go high (3.3V) and remain active as long as he/she remains within the limits of the sensor range. will remain high. Once the person leaves the area, the pin will go low after a specific time that can be set using a potentiometer.
Note: The location of the potentiometer or pin may vary depending on the PIR sensor supplier. Follow the silk screen to determine your pinout
Circuit Diagram and Description
The image below shows the circuit diagram of arduino motion detector project to interface Arduino with PIR module and blink LED/buzzer.
We use the 5V power rail from the Arduino to power the PIR sensor. The output pin of the PIR sensor is connected to the 2 digital pins of the Arduino. This pin will be the input pin of the Arduino. Then there are 3^RD^ pins of the Arduino connected to the LED and buzzer. This pin will act as the output pin of the Arduino. We will program the Arduino to trigger the output on the 3^RD^ pin if an input of 2 is detected on the ^DEĀ·^ pin. The complete procedure is explained below.
Program the Arduino
Arduino programming is very simple and straightforward. To connect the Arduino PIR sensor , we have to assign pin number 2 as input and pin number 3 as output. Then, we have to generate a discontinuous flip-flop every time pin 2 goes high. Each line is explained below.
In the blank setup function shown below, we have to declare that pin 2 connected to the PIR output will be used as input and pin 3 connected to the LED/buzzer will be used as input.
void setup() {
pinMode(2, INPUT); //Pin 2 as INPUT
pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}
Then we continue using the loop() function. As we all know, as long as the MCU is powered on, the code here will be executed. So we always use the following lines in the loop() function to check if Pin 2 goes high.
if (digitalRead(2) == HIGH)
If we find that a particular pin has gone high, it means that the PIR module has been triggered. So, now we have made our output pin (pin 3) high. We turn this pin on and off with a 100ms delay so that we can get a blinking or buzzing output. The code to do the same is shown below.
void setup() {
pinMode(2, INPUT); //Pin 2 as INPUT
pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}
void loop() {
if (digitalRead(2) == HIGH) // check if PIR is triggered.
{
digitalWrite(3, HIGH); // turn the LED/Buzz ON
delay(100); // wait for 100 msecond
digitalWrite(3, LOW); // turn the LED/Buzz OFF
delay(100); // wait for 100 msecond
}
}
Processing
The circuit and program of this arduino motion detector has been discussed above. Now you can build this circuit on the breadboard as per the schematic given above and upload the program at the end of this tutorial. Once connected, your settings should look like below.
Now, power on the Arduino and wait for about 50-60 seconds for the PIR sensor to calibrate. Don't be frustrated by the output you get during this period. After that, try moving in front of the PIR sensor and your LED/buzzer should be triggered.
The beeping/flashing should stop after a while; you can now bypass the output by changing the potentiometer to change the module's sensitivity or low time.
void setup() {
pinMode(2, INPUT); //Pin 2 as INPUT
pinMode(3, OUTPUT); //PIN 3 as OUTPUT
}
void loop() {
if (digitalRead(2) == HIGH)
{
digitalWrite(3, HIGH); // turn the LED/Buzz ON
delay(100); // wait for 100 msecond
digitalWrite(3, LOW); // turn the LED/Buzz OFF
delay(100); // wait for 100 msecond
}
}