// Light Sensitivity Sensor Library
#include <BH1750.h>
// I2C Liquid Crystal Library
#include <LiquidCrystal_I2C.h>
// I2C Communication Library
#include <Wire.h>
// Required Declarations to use the libraries
LiquidCrystal_I2C lcd(0x27, 16, 2);
BH1750 lightMeter;
// Pin Configurations
const int redPin = 13;
const int yellowPin = 11;
const int greenPin = 12;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
// Initialization for the libraries to start
lcd.begin(16, 2);
Wire.begin();
lightMeter.begin();
lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Code for reading the measurements by the Light Sensitivity Sensor
float lux = lightMeter.readLightLevel();
// Print lux value to Serial Monitor for debugging
Serial.print("Lux: ");
Serial.println(lux);
// Code for the LCD to display the measured values
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(lux);
lcd.print(" lux ");
lcd.setCursor(0, 1);
lcd.print("Level: ");
int high = 0;
// Parameters to label according to the measurements by the Light Sensor.
if (lux <= 50) {
lcd.print("Dim ");
high = yellowPin;
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
} else if (lux > 50 && lux <= 70) {
lcd.print("Normal ");
high = greenPin;
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
} else if (lux > 70) {
lcd.print("Too Much");
high = redPin;
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}
// Debug print to Serial Monitor
Serial.print("LED Pin: ");
Serial.println(high);
digitalWrite(high, HIGH);
// A time for the LCD to update the measurements real-time (1 Second)
delay(1000);
}