This circuit consists of an Arduino UNO R4 WiFi microcontroller interfaced with an I2C LCD 16x2 screen and a photocell (LDR) sensor. The photocell is used to measure the ambient light level, and the LCD screen displays the light intensity in human-readable form. A resistor is connected in series with the photocell to form a voltage divider, which allows the Arduino to read the light levels as an analog voltage on one of its analog input pins.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the I2C address for the 16x2 LCD (often 0x27 or 0x3F for I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);
int sensorPin = A0; // Pin where LDR is connected
int sensorValue = 0; // Variable to store the value coming from the sensor
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Start serial communication
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Light Level: ");
}
void loop() {
// Read the value from the photoresistor
sensorValue = analogRead(sensorPin);
// Print the sensor value to the Serial Monitor
Serial.print("Photoresistor Value: ");
Serial.println(sensorValue);
// Determine the light level based on the sensor value
String lightLevel;
if (sensorValue < 200) {
lightLevel = "Dark";
} else if (sensorValue < 400) {
lightLevel = "Partially Dark";
} else if (sensorValue < 600) {
lightLevel = "Medium";
} else if (sensorValue < 800) {
lightLevel = "Fully Lit";
} else {
lightLevel = "Brightly Lit";
}
// Display the light level on the LCD
lcd.setCursor(0, 1); // Set the cursor to the second line
lcd.print(" "); // Clear the second line
lcd.setCursor(0, 1); // Reset cursor
lcd.print(lightLevel); // Display the light level
// Wait 1 second before the next reading (for ease of testing)
delay(1000);
}
This code initializes the LCD and sets up the Arduino to read the analog value from the photocell (LDR). It then prints the raw sensor value to the Serial Monitor and displays a qualitative light level on the LCD based on the sensor reading. The light level is updated every second.