#include <LiquidCrystal_I2C.h>
#define LAMPU 8
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(LAMPU,OUTPUT);
}
void loop() {
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println("analogValue : " + String(analogValue));
Serial.println("voltage : " + String(voltage));
Serial.println("resistance : " + String(resistance));
Serial.println("lux : " + String(lux));
Serial.println("----------------------");
lcd.setCursor(0, 0);
lcd.print("Ruangan: ");
if (lux > 50) {
lcd.print("Terang");
digitalWrite(LAMPU,LOW); // LAMPU MATI
} else {
lcd.print("Gelap ");
digitalWrite(LAMPU,HIGH); // LAMPU MENYALA
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(1000);
}