#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// set the LCD addres to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int ldrPin = A0; // LDR pin
int ldrVal = 0; // Value of LDR
int ledPin = 2; // Build in LED pin
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
float lux;
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Welcome to T4T");
lcd.setCursor(0,1);
lcd.print("WORKSHOP!");
delay(2000);
Serial.begin(9600); // Initialise the serial monitor
pinMode(2, OUTPUT);
}
void loop() {
lcd.clear();
ldrVal = analogRead(ldrPin); // Read the analog value of the LDR
float voltage = ldrVal / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (lux < 200) { // If the LDR value is lower than 200
digitalWrite(ledPin, HIGH); // Turn buildin LED on
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light dey ON");
lcd.setCursor(0,1);
lcd.print("Lux data:");
lcd.print(lux);
} else {
digitalWrite(ledPin, LOW); // Turn buildin LED off
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light done OFF");
lcd.setCursor(0,1);
lcd.print("Lux data:");
lcd.print(lux);
}
Serial.println(lux); // Show the value in the serial monitor
delay(1000); // Pause 1000ms
}