Photoresistor night light: A0 dims the LED on pin 9, on an Arduino Uno
A photoresistor and a 10k resistor in a divider on A0 set the brightness of an LED on pin 9: the darker the room, the brighter the LED. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
What you'll learn
- A light sensor in a voltage divider
- Reading light with
analogRead - Turning a reading into brightness with
map - Why the light and LED respond in opposite directions
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
The photoresistor and a 10k resistor form a voltage divider on A0. The 10k goes to 5V and the photoresistor goes to ground. In bright light the photoresistor has low resistance and pulls A0 down toward 0 V. In the dark its resistance is high, so the 10k pulls A0 up toward 5 V.
The sketch reads A0, scales 0 to 1023 into 0 to 255, and sets the brightness of the LED on pin 9. So the darker it gets, the brighter the LED. It also prints every reading to the serial monitor.
Parts list
6 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Light sensor
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 10 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Light sensor pin 1 | Arduino Uno R3 pin GND |
| LED, yellow pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 9 |
| Resistor, 10k Ω pin 2 | Arduino Uno R3 pin 5V |
| Light sensor pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin A0 |
| LED, yellow pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A night light: the darker it gets, the brighter the LED.
//
// 10k from 5V -> A0; the photoresistor from A0 -> GND
// pin 9 -> 220 ohm -> LED -> GND
//
// In bright light the photoresistor's resistance is low, so it pulls A0
// down towards 0 V; in the dark it is high, and the 10k pulls A0 up towards
// 5 V. map() turns the 0 to 1023 reading into a 0 to 255 brightness.
//
// Drag the light level on the photoresistor.
const int SENSOR = A0;
const int LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: night light, photoresistor on A0, LED on pin 9");
}
void loop() {
int reading = analogRead(SENSOR);
analogWrite(LED_PIN, map(reading, 0, 1023, 0, 255));
Serial.println(reading);
delay(100);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets LED_PIN (pin 9) as an output and starts serial at 9600 with a short message.
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: night light, photoresistor on A0, LED on pin 9");
}loop sketch.ino
Reads SENSOR (A0), maps the reading from 0 to 1023 into 0 to 255, and passes it to analogWrite. Dark means a high reading and a bright LED. It prints the raw reading and waits 100 ms.
void loop() {
int reading = analogRead(SENSOR);
analogWrite(LED_PIN, map(reading, 0, 1023, 0, 255));
Serial.println(reading);
delay(100);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED gets brighter in the light, not the dark This project
The divider is swapped. The 10k should go to 5V and the photoresistor to ground, with A0 between them. - The LED never changes This project
Check that A0 connects to the point between the photoresistor and the 10k resistor. - The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful. - Readings go the wrong way: higher in the dark. Light sensor
Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.