Night light with a light sensor on an Arduino Uno
A light-dependent resistor in a divider fades a lamp up when the room goes dark. Drag the slider on the sensor. The whole build, an Arduino Uno and 6 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.
BeginnerRuns in your browser. Free, and no account needed.
Step by step, with the wiring and the common mistakes: Arduino LDR Night Light (Photoresistor)
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Night lamp: a real light sensor fades a lamp up when the room goes dark.
//
// A0 the middle of the divider: 5 V, the LDR, A0, 10k, GND
// pin 9 the lamp, through 220 ohm to GND
// pin 8 the indicator, through 220 ohm to GND
//
// The light-dependent resistor and the fixed 10k make a divider, and A0 reads
// the middle of it. Light on the cell drops its resistance, so the middle goes
// up: a bright room reads high and a covered cell reads low. That is the whole
// sensor, and it is why an LDR always comes with a resistor.
//
// # The threshold, and why there are two of them
//
// One threshold gives a lamp that flutters on and off while the light sits on
// it. Two (switch on below DARK, switch off above LIGHT, and do nothing in
// between) is hysteresis, and it is what every thermostat, every light
// sensor and every comparator circuit with a bit of feedback does. The gap
// here is about a fifth of the range, which is wide enough that a hand moving
// past the cell does not set it off.
//
// # The fade
//
// Pin 9 is one of the PWM outputs, so analogWrite gives any brightness from 0
// to 255. It walks there rather than jumping: a night light that snaps on is a
// torch. Eight counts every twenty milliseconds is about six tenths of a second
// end to end, and the indicator on pin 8 follows the sensor instantly, so the
// two disagree for exactly as long as the ramp takes.
const int SENSOR = A0;
const int LAMP = 9;
const int INDICATOR = 8;
// Out of 1023. Below DARK it is night; above LIGHT it is day.
const int DARK = 380;
const int LIGHT = 580;
const int STEP = 8;
const int FULL = 255;
int level = 0;
bool night = false;
int said = -1;
void setup() {
pinMode(LAMP, OUTPUT);
pinMode(INDICATOR, OUTPUT);
analogWrite(LAMP, 0);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: night lamp");
Serial.println("drag the slider on the LDR to change the light in the room");
}
void loop() {
int light = analogRead(SENSOR);
// Two thresholds with a gap between them, so the lamp does not flutter.
if (!night && light < DARK) {
night = true;
} else if (night && light > LIGHT) {
night = false;
}
digitalWrite(INDICATOR, night ? HIGH : LOW);
int want = night ? FULL : 0;
if (level < want) {
level += STEP;
if (level > want) {
level = want;
}
} else if (level > want) {
level -= STEP;
if (level < want) {
level = want;
}
}
analogWrite(LAMP, level);
if (level == want && level != said) {
said = level;
Serial.print(night ? "dark (" : "light (");
Serial.print(light);
Serial.print("): lamp at ");
Serial.println(level);
}
delay(20);
}
Parts list
8 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Light sensor
- 1 × Resistor, 10k Ω
- 2 × Resistor, 220 Ω
- 1 × LED, yellow
- 1 × LED, white
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 9; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 8; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 5V; Light sensor pin 1
- Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω pin 2; LED, yellow pin C; LED, white pin C
- Arduino Uno R3 pin A0; Light sensor pin 2; Resistor, 10k Ω pin 1
- Resistor, 220 Ω (1) pin 2; LED, yellow pin A
- Resistor, 220 Ω (2) pin 2; LED, white pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.