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

QtyPartWhat it does
1Arduino Unothe microcontroller board
1photoresistor (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.
110 kΩ resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1yellow LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno 5Vphotoresistor (LDR) 1
2photoresistor (LDR) 210 kΩ resistor leg 1
3Arduino Uno GND10 kΩ resistor leg 2
4Arduino Uno A0photoresistor (LDR) 2
5Arduino Uno pin 9220 Ω resistor leg 1
6220 Ω resistor leg 2yellow LED anode (A, long leg)
7Arduino Uno GNDyellow LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a photoresistor (LDR), a 10 kΩ resistor, a 220 Ω resistor and a yellow LED from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to photoresistor (LDR) 1.
  4. Wire 2. Connect photoresistor (LDR) 2 to 10 kΩ resistor leg 1.
  5. Wire 3. Connect Arduino Uno GND to 10 kΩ resistor leg 2.
  6. Wire 4. Connect Arduino Uno A0 to photoresistor (LDR) 2.
  7. Wire 5. Connect Arduino Uno pin 9 to 220 Ω resistor leg 1.
  8. Wire 6. Connect 220 Ω resistor leg 2 to yellow LED anode (A, long leg).
  9. Wire 7. Connect Arduino Uno GND to yellow LED cathode (C, short leg).
  10. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  11. 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