#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int ledPin=16;
// choose the pin for the LED
int buzzerPin=-17;
// choose the pin for the buzzer
int inputPin =4;
// choose the input pin (for PIR senson)
int pirState = LOW; // we start, assuming no motion detected
int val=0;
// variable for reading the pin status
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(ledPin, OUTPUT);
// declare LED as output
pinMode(buzzerPin, OUTPUT); //declare LED as output
pinMode(inputPin, INPUT); //declare sensor as input
lcd.init();// initialize the lcd
lcd.backlight();
lcd.setCursor(6, 0);// move cursor to (6, 0)
lcd.print("IOT");
// print message at (6, 0)
lcd.setCursor(4,1); // move cursor to (0, 1)
lcd.print("PIR Sensor"); // print message at (0,1)
delay(2000);
// display the above for two seconds
lcd.clear();
}
// clear display
void loop(){
// put your main code here, to run repeatedly:
val = digitalRead(inputPin); // read input value
if (val == HIGH){
// check if the input is HIGH
digitalWrite(ledPin, HIGH); //turn LED ON
if (pirState == LOW){
// we have just turned on
analogWrite(buzzerPin, 123); // turn buzzer ON
Serial.println("Motion detected!");
lcd.setCursor(0, 0);
lcd.print("Motion detected!");
lcd.setCursor(0, 1);
lcd.print("LED= ON ");
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
analogWrite(buzzerPin, 0); // turn buzzer OFF
if (pirState = HIGH){
// we have just turned of
Serial.println("Motion ended!");
lcd.setCursor(0, 0);
lcd.print("Motion ended! ");
lcd.setCursor(0, 1);
lcd.print("LED = OFF");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}