// import LiquidCrystal head file
#include <LiquidCrystal.h>
//LiquidCrystal(rs, rw(optional), enable, d4, d5, d6, d7);
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
// put your setup code here, to run once:
//LCD initialization
//set columns and rows
lcd.begin(16,2);
//Title: LUX
lcd.print("Luminance (Lux):");
//serial port initialization
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//NOTE:The photoresistor sensor module includes a LDR in series with a 10K resistor.
//The AO pin is connected between the LDR and the 10K resistor.
//luminance calculation given by wokwi doc
//Step 1: These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
//Step 2: Convert the analog value into lux value:
int analogValue = analogRead(A5);//LDR AO is connected to Pin A5
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
//print luminance value on LCD
lcd.setCursor(0,1);
lcd.print(lux);
//print luminace value by serail port
Serial.println(lux);
delay(1000);
}