#include <LiquidCrystal.h>
/* General constants */
const int VSUPPLY = 5;
/* Display */
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
/* LDR */
const float GAMMA = 0.7;
const float RL10 = 50;
const int REF_RESISTANCE = 10000;
const int LDR_PIN = A0;
float readLDRSensor() {
int analogValue = analogRead(LDR_PIN);
const float refVoltage = analogValue / 1024. * 5;
const float ldrResistance = REF_RESISTANCE * (refVoltage / (VSUPPLY - refVoltage));
return pow(RL10 * 1000 * pow(10, GAMMA) / ldrResistance, (1 / GAMMA));
}
void showLuxValue(float lux)
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Light level:");
lcd.setCursor(3, 1);
lcd.print(String(lux, 2) + " lux");
}
void setup() {
lcd.begin(16, 2);
lcd.print("This is a test!");
}
void loop() {
float roomLux = readLDRSensor();
showLuxValue(roomLux);
delay(1000);
}