Board and part

Arduino Uno LDR Simulator: Wiring, Code and a Live Circuit

This is a light-dependent resistor wired to an Arduino Uno in a voltage divider with a 10 k resistor, read live: the serial monitor prints the raw reading, a percent and the words Dark, Dim or Bright four times a second.

  • 5 V logic
  • No library: analogRead()
  • Divider on pin A0
  • 10 k fixed resistor
Arduino Uno · LDRlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the LDR to the Arduino Uno

Four connections: the LDR from 5V to pin A0, and the 10 k resistor from pin A0 to GND. In the editor, drag the light slider on the sensor and the reading follows it.

Every connection in the circuit above, with the board’s own pin names.

LDR pinArduino Uno pinWhy
LDR, one leg5VThe top of the voltage divider.
LDR, other legpin A0The middle of the divider, where the reading is taken.
10 k resistor, one endThe LDR leg on pin A0Joined to the same node as the analog pin.
10 k resistor, other endGNDThe bottom of the divider.

Power

The LDR and the 10 k resistor make a divider between 5 V and GND. The LDR has no polarity, so either leg can go to either side. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

Pins to leave alone on the Arduino Uno

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

On a real bench

On a real bench the Uno stands beside the breadboard: the LDR goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

LDR light level on Arduino Uno pin A0
const int LDR_PIN = A0;   // the divider's middle, pin A0

void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(LDR_PIN);               // 0 to 1023
  int percent = map(raw, 0, 1023, 0, 100);
  Serial.print("Light: ");
  Serial.print(raw);
  Serial.print("  ");
  Serial.print(percent);
  Serial.print("%  ");
  if (percent < 25) {
    Serial.println("Dark");
  } else if (percent < 60) {
    Serial.println("Dim");
  } else {
    Serial.println("Bright");
  }
  delay(250);
}

An LDR is a resistor whose value falls as light rises: very high in the dark, a few hundred ohms in strong light. A microcontroller reads voltage, not resistance, so the LDR is paired with a fixed 10 k resistor to make a voltage divider. More light means a lower LDR resistance, so more of the supply drops across the 10 k and the pin reads higher.

analogRead() gives 0 to 1023, and map() turns that into a percent. The three bands (under 25, under 60, above) are the sketch's guess at dark, dim and bright; the numbers depend on the LDR and the resistor, so change them to suit your room.

Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.

Pitfalls

Common mistakes with the LDR on the Arduino Uno

No fixed resistor

An LDR alone is a resistor between two pins, not a voltage. With nothing from the analog pin to ground the pin floats, and the reading barely follows the light. The 10 k to GND is what makes it a divider.

Reading it backwards

With the LDR on the supply side, as here, bright light reads high. Swap the LDR and the resistor and bright reads low. Both work as long as the code agrees with the wiring.

One threshold for a switch

If the sketch turns a lamp on at one reading and off at the same reading, it flickers whenever the light sits near it. Use two thresholds with a gap between them, as the full tutorial does.

FAQ

Questions and answers

Can I simulate an LDR with an Arduino Uno?

Yes. The circuit at the top of this page is an Arduino Uno with a light-dependent resistor, running in your browser on a simulated ATmega328P. Drag the light slider on the sensor and its resistance changes the way a real cell does, falling as the light rises. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

What resistor should I use with an LDR?

One near the LDR’s resistance at the light level where you want the most sensitivity. 10 k suits indoor light and the common GL55 cells.

What does analogRead() give on the Arduino Uno?

The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

Is the reading in lux?

No. The reading is a voltage, and the link between voltage and lux depends on the cell and the resistor. For real lux readings, a BH1750 sensor measures them directly.

Build your own LDR project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.