//Belajar LCD menampilkan data Sensor PIR dengan ESP32
#include <LiquidCrystal_I2C.h>
int KolomLCD = 16;
int BarisLCD = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, KolomLCD, BarisLCD);
const int PIRSensor = 14;
const int LED1 = 15;
int Pirstate = 0;
int val = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Memulai LCD
lcd.init();
// Menyalakan LCD backlight
lcd.backlight();
pinMode(PIRSensor, INPUT);
pinMode(LED1, OUTPUT);
}
void loop() {
val = digitalRead(PIRSensor); // Baca nilai Input
if (val == HIGH) { // Cek apakah nilai input HIGH
digitalWrite(LED1, HIGH); // Lampu Menyala
if (Pirstate == LOW) {
lcd.setCursor(0, 0);
// Menampilkan Text
lcd.print("Ada Gerakan!!");
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
Pirstate = HIGH;
}
}
else
{
digitalWrite(LED1, LOW); // Lampu Mati
if (Pirstate == HIGH){
lcd.setCursor(0, 0);
// Menampilkan Text
lcd.print("Tidak ada Gerakan");
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
Pirstate = LOW;
lcd.clear();
}
}
}