#11Beginner

Measuring Light Intensity with an LDR

4.6(78)
119 completed
Measuring Light Intensity with an LDR
Project that measures ambient light intensity with LDR sensor and displays it on Serial Monitor. Teaches the basics of analog sensor reading and serial communication.

Video

Circuit Diagram

Measuring Light Intensity with an LDR - Circuit

Source Code

Arduino (C++)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   Measuring Light Intensity with an LDR
4// ─────────────────────────────────────────
5// Reads the ambient light on A0 through a voltage divider made of an LDR
6// and a 10k resistor, and prints the 0-1023 value to the Serial Monitor
7// every 0.5 s. The value rises as the light gets brighter.
8//
9// Pins:
10//   A0 -> Junction of the LDR and the 10k resistor
11//   LDR other leg to 5V, 10k other leg to GND
12
13int value;   // Light reading (0-1023)
14
15void setup() {
16  Serial.begin(9600);   // Serial at 9600 baud
17}
18
19void loop() {
20  value = analogRead(A0);   // Read the LDR voltage
21  Serial.print("Value: ");
22  Serial.println(value);
23  delay(500);               // Wait 0.5 s
24}
25

FAQ

How does LDR work?▾

As the amount of light falling on the LDR (light-dependent resistor) increases, its resistance decreases. When a voltage divider is created using a 10 kΩ resistor, changes in light are converted into changes in voltage and can be read as a value between 0 and 1023 using `analogRead`.

What should the readings be in light and dark conditions?▾

It depends on the connection type; generally, high values are observed in bright conditions and low values in dark conditions (or vice versa). Note the bright and dark values in its natural environment using the Serial Monitor; these will be used to select threshold values in future projects.

I'm seeing garbled characters in the Serial Monitor. Why is that?▾

The baud rate in the Serial Monitor must match the value specified in the `Serial.begin()` function in the code (e.g., 9600). If they differ, garbled characters will appear on the screen.

Is there a more sensitive light sensor than an LDR?▾

Yes, I2C light sensors like the BH1750 measure light directly in lux (lx) and are much more consistent than LDRs. An LDR is sufficient for simple night lights and darkness detection.

Measuring Light Intensity with an LDR | BlueGrays