#include <LiquidCrystal.h>
// LCD pin configuration
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// Constants for the LDR
const float GAMMA = 0.7;
const float RL10 = 50;
const int ldrPin = A0; // Analog pin connected to AO of the LDR module
const int backlightPin = 6; // PWM pin for backlight control
void setup() {
pinMode(backlightPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Ambient Light:");
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(ldrPin);
float voltage = analogValue / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
int backlightBrightness = map(lux, 0, 1000, 255, 0); // Map lux to brightness (0-255)
backlightBrightness = constrain(backlightBrightness, 0, 255);
analogWrite(backlightPin, backlightBrightness); // Set backlight brightness
lcd.setCursor(0, 1); // Set cursor to the second row
lcd.print("Lux: ");
lcd.print((int)lux); // Print the lux value
delay(100);
}