How PIR Sensor Works and How To Use It with Arduino

How PIR Sensor Works and How To Use It with Arduino

PIR sensors are widely used for motion detection in various applications, from security systems to automatic lighting. Understanding how PIR sensors work and how to integrate them with Arduino opens up a world of possibilities for creating interactive projects. In this guide, we will learn about PIR sensors, explore their working principles, and provide step-by-step instructions for interfacing them with Arduino.?

Understanding PIR Sensor

Let's first understand how a PIR sensor works. PIR sensor detects changes in infrared radiation emitted by objects within its field of view. This radiation is emitted by all objects with a temperature above absolute zero, including humans and animals. The sensor consists of a pyroelectric sensor, which generates an electrical signal in response to changes in infrared radiation, and a circuit that processes and amplifies this signal to detect motion.

HCSR04 Ultrasonic Sensor Pinout

The Pinout is given as below:

VCC is the power pin of the module.

GND is the GND pin of the module.

Out is the data output pin of the module.

Interfacing PIR Sensor with Arduino

Now that we have a basic understanding of how PIR sensors work, let's proceed to interface the PIR sensor with Arduino.

Components Used

Arduino UNO R3

16 x 2 LCD

1 Potentiometer 10 k?

1 Resistance 220 ?

Breadboard

HC-SR501 PIR Sensor

20 Connective Wires

Connections between Arduino Uno and PIR Sensor are as follows:

  1. Connect the VCC pin of the PIR sensor to the 5V pin on the Arduino board.
  2. Connect the GND pin of the PIR sensor to the GND pin on the Arduino board.
  3. Connect the OUT pin of the PIR sensor to a digital pin e.g., pin 2 on the Arduino board.

With the hardware connections in place, let's move on to writing the Arduino code to interface with the PIR sensor.

#include <LiquidCrystal.h> // Include the LiquidCrystal library for LCD display
LiquidCrystal lcd(12, 11, 6, 7, 8, 9); // Initialize the LCD object with pin numbers

int sensorInput = 2;   // PIR sensor input pin
int sensorReturn = 0;  // Variable to store PIR sensor output

void setup() {
  pinMode(sensorInput, INPUT); // Set sensor pin as input
  
  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  
  // Print initial message on the LCD
  lcd.setCursor(0, 0);
  lcd.print("PIR Sensor Says:");
  lcd.setCursor(0, 1);
}

void loop() {
  sensorReturn = digitalRead(sensorInput); // Read input value from PIR sensor
  
  // Check if motion is detected
  if (sensorReturn == HIGH) {
    // Set cursor to the second row and print motion detection message
    lcd.setCursor(0, 1);
    lcd.print("Motion Occurs   ");
  } else {
    // Set cursor to the second row and print motion stopped message
    lcd.setCursor(0, 1);
    lcd.print("Motion Stops    ");
  }
}        

Explanation of Arduino Code

This line includes the LiquidCrystal library, which allows interfacing with LCD displays.

#include <LiquidCrystal.h> // Include the LiquidCrystal library for LCD display        

This line initializes an instance of the LiquidCrystal class named lcd, specifying the pin numbers to which the LCD display is connected. The parameters represent (RS, EN, D4, D5, D6, D7) pins respectively.

LiquidCrystal lcd(12, 11, 6, 7, 8, 9); // Initialize the LCD object with pin numbers        

These lines declare two integer variables: sensorInput, which represents the pin number connected to the PIR sensor, and sensorReturn, which will store the output of the PIR sensor.

int sensorInput = 2;   // PIR sensor input pin

int sensorReturn = 0;  // Variable to store PIR sensor output        

The setup() function is called once when the Arduino board starts. Now we set the sensorInput pin as an input pin, indicating that it will be used to read data from the PIR sensor. Then initialize the LCD display with 16 columns and 2 rows, indicating the display's dimensions. Initial messages are printed on the LCD display, positioning the cursor at the beginning of the second row.

void setup() {
pinMode(sensorInput, INPUT); // Set sensor pin as input

// Set up the LCD's number of columns and rows

lcd.begin(16, 2);

 // Print initial message on the LCD

  lcd.setCursor(0, 0);

  lcd.print("PIR Sensor Says:");

  lcd.setCursor(0, 1);

}        

In the loop() function, the value of the PIR sensor output is read using digitalRead(sensorInput).

If motion is detected (sensor output is HIGH), a message indicating motion occurrence is displayed on the LCD. If no motion is detected (sensor output is LOW), a message indicating motion stops is displayed on the LCD.

void loop() {
sensorReturn = digitalRead(sensorInput); // Read input value from PIR sensor

  
  // Check if motion is detected

  if (sensorReturn == HIGH) {

    // Set cursor to the second row and print motion detection message

    lcd.setCursor(0, 1);

    lcd.print("Motion Occurs   ");

  } else {

    // Set cursor to the second row and print motion stopped message

    lcd.setCursor(0, 1);

    lcd.print("Motion Stops    ");

  }

}        

Demonstration

Image and Code Source: https://playwithcircuit.com/how-hc-sr501-pir-sensor-works-how-to-interface-it-with-arduino/

Ayham Amrous

I am a freelancer on Fiverr, I am open to any work outside Fiverr as well just please e-mail me or contact me on LinkedIn or my telegram: @AmrousAyhamBachir or Discord: ayham_dz

10 个月

Amazing work and explanation, I was definitely inspired by your content, keep up the good work!

回复
Thenathayalan P

An electronics hobbyist

10 个月

Well explained ??

回复
archana deiva

--Embedded System Corporate trainer/ C C++ trainer/ardunio trainer /Robotics trainer

10 个月

Good

回复

要查看或添加评论,请登录

Rachana Jain的更多文章

社区洞察

其他会员也浏览了