This circuit is designed to measure light intensity using a photocell (Light Dependent Resistor, LDR) and adjust the brightness of an LED accordingly. The photocell forms part of a voltage divider network connected to the Arduino UNO's analog input. The LED's brightness is inversely proportional to the light level detected by the photocell, meaning the LED gets brighter as the light level decreases. The Arduino UNO controls the LED brightness through PWM on a digital pin.
/*
Photocell Light Sensitivity Tester
This sketch is designed to read the light level from a photocell (also known as a photoresistor or LDR) and adjust
the brightness of an LED accordingly.
The photocell is part of a voltage divider connected to analog input A0, with a 10K resistor connecting to ground.
The LED is connected to digital pin 11 through a current-limiting resistor.
As the light level decreases, the LED brightness increases.
Circuit connections:
- Photocell one end connected to 5V
- Photocell other end connected to A0
- 10K resistor from A0 to GND
- LED anode connected to pin 11 through a resistor
- LED cathode connected to GND
*/
int photocellPin = A0; // the cell and 10K pulldown are connected to A0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; // variable to store the brightness value
void setup() {
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
photocellReading = analogRead(photocellPin); // read the value from the sensor
Serial.print("Analog reading = ");
Serial.println(photocellReading); // print the raw analog reading
// Invert the reading since the LED gets brighter in darkness
photocellReading = 1023 - photocellReading;
// Map the reading to the PWM range (0-255) and set LED brightness
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);
delay(100); // Wait for 100 milliseconds before the next loop
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the serial communication for debugging purposes, reads the analog value from the photocell, inverts the reading to increase LED brightness as it gets darker, maps the reading to a PWM value, and writes this value to the LED pin to adjust its brightness.