LDR Sensor Project using Arduino
The LDR (Light Dependent Resistor) sensor project helps you measure ambient light levels. This tutorial walks you through connecting the sensor, reading values with Arduino, and displaying or acting on the data.
BeginnerComponents Required
- Arduino Uno (or compatible board)
- LDR (Light Dependent Resistor) sensor
- 10 kΩ resistor
- Breadboard
- Jumper wires
- USB cable
- LED (optional – to indicate dark/light)
Circuit Connections
Build a simple voltage divider using the LDR and a fixed resistor. The middle node will connect to an analog input on the Arduino. Make sure power is removed while wiring the circuit.
- Place the LDR and 10 kΩ resistor on the breadboard in series.
- Connect one end of the LDR to 5 V on the Arduino.
- Connect the free end of the resistor to GND on the Arduino.
- Connect the junction between LDR and resistor to A0 (analog pin 0).
- (Optional) Connect an LED to digital pin 13 with a 220 Ω resistor to indicate darkness.
Here is the circuit diagram for reference. Analog pin A0 measures the voltage from the divider.
Arduino Code
// LDR Sensor Project
// Circuit Creation
const int ldrPin = A0; // analog input pin
int ldrValue = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // optional LED indicator
}
void loop() {
ldrValue = analogRead(ldrPin);
Serial.println(ldrValue);
// turn on LED when it is dark (value less than threshold)
if (ldrValue < 500) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(500);
}
Upload the Code
- Connect the Arduino board to your computer using the USB cable.
- Open the Arduino IDE and paste the code into a new sketch.
- Select the correct board and port from the Tools menu.
- Click the Upload button.
- Open the Serial Monitor at 9600 baud to observe the light readings.
Code Explanation
Let us understand how the Arduino code works step by step. This will help you modify the project later and build more advanced circuits.
Declaring the LDR Pin
const int ldrPin = A0; sets the analog pin used to read the voltage divider.
setup() Function
The setup() function initializes serial communication and configures
pin 13 as an output for the optional LED indicator.
loop() Function
The loop() function continuously reads the analog value from the LDR,
prints it over the serial port, and toggles the LED based on a threshold.
analogRead() Function
The analogRead(ldrPin) command returns a value from 0 (dark) to 1023 (bright).
Serial.println()
The Serial.println() sends the sensor reading to the Serial Monitor,
allowing you to observe light changes in real time.
Common Mistakes & Troubleshooting
- No readings: Ensure the LDR and resistor are correctly placed forming a voltage divider and that A0 is connected.
- Values stuck at 0 or 1023: Check for short circuits or open connections.
- Serial output not showing: Confirm the baud rate is set to 9600 in the Serial Monitor.
- LED not lighting: Verify the threshold in code and check LED orientation.
What You Learned
- How to build and read a voltage divider with an LDR sensor.
- Using
analogRead()to measure sensor values. - Interpreting analog readings and using thresholds.
- Sending data to the Serial Monitor for debugging.
- Controlling an LED based on sensor input.