// Inisialisasi Komponen LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);

// Inisialisasi Variable untuk kebutuhan 
// sensor LDR
const float GAMMA = 0.7;
const float RL10 = 50;

// Inisialisasi pin-pin yang terkoneksi dengan kontroler
#define Led 3
int inputPin = 2;
int pirState = LOW;
int buzzer = 10;
int val = 0;

void setup() {
  Serial.begin(9600);
  pinMode (Led, OUTPUT);
  pinMode (inputPin, INPUT);
  pinMode (buzzer, OUTPUT);
  // Mengaktifkan LCD
  lcd.init();
  lcd.backlight();
}

void loop() {
  delay(5000);
  // Bagian Pembacaan sensor LDR
  int analogValue = analogRead (A0);
  float voltage = analogValue/1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  int lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  
  Serial.println("Analog Value = "+String(analogValue));
  Serial.println("voltage Value = "+String(voltage));
  Serial.println("resistance Value = "+String(resistance));

  lcd.setCursor (2, 0);
  lcd.print ("Cahaya :");
  // Bagian Pendeteksi Intensitas Cahaya dari Sensor LDR
  if (lux > 50){
    lcd.print ("Terang!");
    digitalWrite (Led, LOW);
    lcd.setCursor (0, 1);
    lcd.print("LUX :");
    lcd.print(lux);
    lcd.setCursor (10, 1);
    lcd.print("     ");
  }
  else{
    lcd.print ("Gelap  ");
    digitalWrite (Led, HIGH);

    // Bagian mendeteksi pergerakan dari sensor PIR
    val = digitalRead (inputPin);    
  if (val == HIGH)
  {
    if (pirState == LOW)
    {
      lcd.setCursor (0, 1);
      lcd.print ("Ada Gerakan     ");
      pirState = HIGH;
      tone(buzzer, 120);
      delay (1000);
      tone(buzzer, 300);
      delay (1000);
    }
  }
    else
    {
      if (pirState == HIGH)
      {
        lcd.setCursor (0, 1);
        lcd.print ("Tidak ada Gerak ");
        pirState = LOW;
        noTone(buzzer);
      }
    }
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module