Devices & Components
16x2 LCD display with I²C interface
Breadboard - 830 contacts
Arduino Uno Rev3
HC-SR501
Breadboard Jumper Wire Pack (200mm&100mm)
Resistor 220 Ohm
Software & Tools
Arduino IDE
Project description
Code
Interfacing HC-SR501 PIR Sensor to an Arduino
cpp
This code indicates the detection of motion.
1#include <LiquidCrystal.h> // Include the LiquidCrystal library for LCD display 2LiquidCrystal lcd(12, 11, 6, 7, 8, 9); // Initialize the LCD object with pin numbers 3 4int sensorInput = 2; // PIR sensor input pin 5int sensorReturn = 0; // Variable to store PIR sensor output 6 7void setup() { 8 pinMode(sensorInput, INPUT); // Set sensor pin as input 9 // Set up the LCD's number of columns and rows 10 lcd.begin(16, 2); 11 // Print initial message on the LCD 12 lcd.setCursor(0, 0); 13 lcd.print("PIR Sensor Says:"); 14 lcd.setCursor(0, 1); 15} 16 17void loop() { 18 sensorReturn = digitalRead(sensorInput); // Read input value from PIR sensor 19 20 // Check if motion is detected 21 if (sensorReturn == HIGH) { 22 // Set cursor to the second row and print motion detection message 23 lcd.setCursor(0, 1); 24 lcd.print("Motion Occurs "); 25 } else { 26 // Set cursor to the second row and print motion stopped message 27 lcd.setCursor(0, 1); 28 lcd.print("Motion Stops "); 29 } 30}
Comments
Only logged in users can leave comments