#include <LiquidCrystal_I2C.h>
#define ldrPin 2
const float gama = 0.7;
const float rl10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
int iLDR = analogRead(ldrPin);
iLDR = map(iLDR, 4095, 0, 1024, 0);
float voltage = iLDR / 1024.*5;
float resistanse = 2000 * voltage / (1-voltage/5);
float lux = pow(rl10*1e3*pow(10,gama)/resistanse,(1/gama));
Serial.print("lux = ");
Serial.println(lux);
lcd.setCursor(0, 0);
lcd.print("Room: ");
if (lux > 500) {
lcd.print("Light!");
} else {
lcd.print("Dark ");
}
lcd.setCursor(0, 2);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
}