How to wire an LDR to an Arduino Uno
To wire an LDR to an Arduino Uno, 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
LDR to Arduino Uno 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 pin | Arduino Uno pin | Wire color | Why |
|---|---|---|---|
| LDR, one leg | 5V | red | The top of the voltage divider. |
| LDR, other leg | pin A0 | amber | The middle of the divider, where the reading is taken. |
| 10 k resistor, one end | The LDR leg on pin A0 | n/a | Joined to the same node as the analog pin. |
| 10 k resistor, other end | GND | n/a | The bottom of the divider. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (10 kΩ) pin 1 | 2 | amber |
| R1 (10 kΩ) pin 2 | GND | blue |
Power and logic levels for LDR and Arduino Uno
Logic level
The Arduino Uno’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 Uno’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. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.
Pins to leave alone
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.
The working code for the Arduino Uno
Library: none, the sketch reads the divider with analogRead(). This is the sketch the circuit above runs.
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);
}
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% DarkCommon mistakes wiring an LDR to an 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.
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.