/*
Photoresistor (LDR) Analog Demo
Copyright (C) 2021 Uri Shaked.
https://wokwi.com/arduino/projects/305193627138654786
*/
int LED1 = 25;
int LED2 = 18;
int LED3 = 19;
#include <LiquidCrystal_I2C.h>
#define LDR_PIN 12
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
int analogValue = analogRead(12);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
analogWrite(LED1, lux);
lcd.setCursor(2, 0);
lcd.print("Room: ");
if (lux > 50) {
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
lcd.print("Light!");
} else {
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
lcd.print("Dark ");
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
}