Wiring guide

How to wire an LDR to an Arduino Nano

To wire an LDR to an Arduino Nano, connect 1 to 5V and 2 to pin A0. The LDR and its 10 kilohm resistor form a divider between 5 V and GND, so the pin sees 0 to 5 V, the converter’s own range. The sketch below uses no library beyond what the board includes. It reads the divider and prints the light level with the word Dark, Dim or Bright.

  • 5 V logic
  • Library: none
  • 2 wires, 1 helper parts
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.
Pin to pin

LDR to Arduino Nano wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

LDR pinArduino Nano pinWire colorWhy
LDR, one leg5VredThe top of the voltage divider.
LDR, other legpin A0amberThe middle of the divider, where the reading is taken.
10 k resistor, one endThe LDR leg on pin A0n/aJoined to the same node as the analog pin.
10 k resistor, other endGNDn/aThe bottom of the divider.

Helper parts

The extra parts in the circuit above, and where their wires go:

FromToWire color
R1 (10 kΩ) pin 12amber
R1 (10 kΩ) pin 2GNDblue
Voltage

Power and logic levels for LDR and Arduino Nano

Logic level

The Arduino Nano’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The LDR takes 5 V from 5V. That is the Arduino Nano’s 5 V rail, so the part shares the current the board can supply.

This pair

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

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.

The sketch

The working code for the Arduino Nano

Library: none, the sketch reads the divider with analogRead(). This is the sketch the circuit above runs.

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);
}
Output

What you should see

Open the serial monitor at 9600 baud. Each line shows the raw reading, the level as a percentage and Dark, Dim or Bright. More light lowers the LDR’s resistance and raises the reading.

The LDR has no polarity, so either leg can go to either side. Swap the LDR and the resistor and the readings invert.

The serial monitor prints this, recorded from the circuit above with the light turned up and then down:

Light: 856  83%  Bright
Light: 987  96%  Bright
Light: 10  0%  Dark
Pitfalls

Common mistakes wiring an LDR to an 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.

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.