Board and part

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

This is a light-dependent resistor wired to an Arduino Nano 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 Nano · 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 Nano

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 Nano 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. Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all.

Pins to leave alone on the Arduino Nano

A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.

On a real bench

The Nano plugs into the breadboard itself, straddling the center channel, so the LDR goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.

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 Nano 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 is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.

Pitfalls

Common mistakes with the LDR on the Arduino Nano

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 Nano?

Yes. The circuit at the top of this page is an Arduino Nano 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 Nano?

Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all.

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.