Arduino LDR Light Sensor: Wiring, Code and Simulator
To build this Arduino LDR Light Sensor project you need an Arduino Uno and a photoresistor (LDR), a 10 kΩ resistor, a 220 Ω resistor and a yellow LED. A bare photoresistor and a 10 kΩ resistor make a voltage divider; A0 reads the middle, and the sketch works out the LDR’s resistance. Make the 7 connections in the wiring table, upload the Arduino C++ code, and move the Light slider: the voltage and the computed resistance follow, and the night light on pin 9 dims as it gets brighter.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | photoresistor (LDR) | A bare light-dependent resistor: about 10 kΩ in room light, 1 MΩ in the dark, a few hundred ohms in sunlight. Make a divider with a 10 kΩ resistor and read the middle on an analog pin. |
| 1 | 10 kΩ resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | yellow LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | photoresistor (LDR) 1 |
| 2 | photoresistor (LDR) 2 | 10 kΩ resistor leg 1 |
| 3 | Arduino Uno GND | 10 kΩ resistor leg 2 |
| 4 | Arduino Uno A0 | photoresistor (LDR) 2 |
| 5 | Arduino Uno pin 9 | 220 Ω resistor leg 1 |
| 6 | 220 Ω resistor leg 2 | yellow LED anode (A, long leg) |
| 7 | Arduino Uno GND | yellow LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a photoresistor (LDR), a 10 kΩ resistor, a 220 Ω resistor and a yellow LED from the parts palette.
- Wire 1. Connect Arduino Uno 5V to photoresistor (LDR) 1.
- Wire 2. Connect photoresistor (LDR) 2 to 10 kΩ resistor leg 1.
- Wire 3. Connect Arduino Uno GND to 10 kΩ resistor leg 2.
- Wire 4. Connect Arduino Uno A0 to photoresistor (LDR) 2.
- Wire 5. Connect Arduino Uno pin 9 to 220 Ω resistor leg 1.
- Wire 6. Connect 220 Ω resistor leg 2 to yellow LED anode (A, long leg).
- Wire 7. Connect Arduino Uno GND to yellow LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Move the Light slider: the voltage and the computed resistance follow, and the night light on pin 9 dims as it gets brighter.
Arduino C++ code
// Light meter: a bare photoresistor (LDR) in a voltage divider.
// 5V -- LDR --+-- 10 kOhm -- GND
// '-- A0
// More light -> lower LDR resistance -> higher voltage at A0.
// The LED on pin 9 gets brighter as the room gets darker (a night light).
const float VCC = 5.0;
const float R_FIXED = 10000.0;
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop() {
int raw = analogRead(A0); // 0..1023
float v = raw * VCC / 1023.0; // volts at the divider's middle
float rLdr = R_FIXED * (VCC - v) / max(v, 0.001);
Serial.print("A0 = ");
Serial.print(raw);
Serial.print(" V = ");
Serial.print(v, 2);
Serial.print(" R_ldr = ");
Serial.print(rLdr, 0);
Serial.println(" ohm");
analogWrite(9, 255 - raw / 4); // darker -> brighter night light
delay(300);
}
What to try
Move the Light slider: the voltage and the computed resistance follow, and the night light on pin 9 dims as it gets brighter.
Frequently asked questions
How do I wire the LDR Light Sensor project on an Arduino Uno?
Make 7 connections: Arduino Uno 5V to photoresistor (LDR) 1; photoresistor (LDR) 2 to 10 kΩ resistor leg 1; Arduino Uno GND to 10 kΩ resistor leg 2; Arduino Uno A0 to photoresistor (LDR) 2; Arduino Uno pin 9 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to yellow LED anode (A, long leg); Arduino Uno GND to yellow LED cathode (C, short leg).
What parts do I need for this Arduino project?
An Arduino Uno and a photoresistor (LDR), a 10 kΩ resistor, a 220 Ω resistor and a yellow LED.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
A bare photoresistor and a 10 kΩ resistor make a voltage divider; A0 reads the middle, and the sketch works out the LDR’s resistance.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator
- Arduino Push Button LED: Wiring, Code and Simulator