PIR Sensor & LCD Shield

Components Used:

  • 1x Arduino Uno R3
  • 1x PIR Sensor
  • 1x LCD Shield

 

hc-sr501-arduino_bb.png

Program:

// Author : Ankit Modi
// Date : 28-09-2017

/* Arduini PIR Motion Sensor Tutorial
 ********************************************
 */
 #include <LiquidCrystal.h> // includes the LiquidCrystal Library
 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
 int pirSensor = 1;
 int relayInput = 2;
 void setup() {
 pinMode(pirSensor, INPUT);
 pinMode(relayInput, OUTPUT);
 lcd.begin(16,2); 
 }
 void loop() {
 int sensorValue = digitalRead(pirSensor);
 if (sensorValue == 1) {
 lcd.display();
 digitalWrite(relayInput, HIGH); // The Relay Input works Inversly
 lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
 lcd.print("Object is "); // Prints string "Object" on the LCD
 delay(10);
 lcd.setCursor(0,1);
 lcd.print("Detected. "); // Prints string "Detected" on the LCD
 delay(10);
 // scroll 13 positions (string length) to the left
 // to move it offscreen left:
 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
 // scroll one position left:
 lcd.scrollDisplayLeft();
 // wait a bit:
 delay(200);
 }
 // scroll 29 positions (string length + display length) to the right
 // to move it offscreen right:
 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
 // scroll one position right:
 lcd.scrollDisplayRight();
 // wait a bit:
 delay(200);
 }
 }
 else {
 lcd.display();
 digitalWrite(relayInput, LOW); // The Relay Input works Inversly
 lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
 lcd.print("Object is "); // Prints string "Object" on the LCD
 delay(10);
 lcd.setCursor(0,1);
 lcd.print("Not Detected."); // Prints string "Not Detected" on the LCD
 delay(10);
 // scroll 13 positions (string length) to the left
 // to move it offscreen left:
 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
 // scroll one position left:
 lcd.scrollDisplayLeft();
 // wait a bit:
 delay(200);
 }
 // scroll 29 positions (string length + display length) to the right
 // to move it offscreen right:
 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
 // scroll one position right:
 lcd.scrollDisplayRight();
 // wait a bit:
 delay(200); 
 }
 }
 }

 

Advertisements
%d bloggers like this: