Dusk switch on an Arduino Uno
A photoresistor sensor module turns a lamp on at dusk: its comparator output switches the LED, and its analog output shows the light. The whole build, an Arduino Uno and 1 more part, 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $31. On Mokxi they're free.
What you'll learn
- Analog and digital outputs on the same sensor
- A comparator makes a yes or no decision
- Setting a threshold with a trimmer
- Doing two jobs in the main loop without blocking
- Voltage divider with a light dependent resistor
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A photoresistor module has a light sensor and a small comparator chip on one board. The comparator watches the light level against a blue trimmer you can turn. When it gets darker than that setting, the DO pin goes high.
The sketch reads DO on pin 2 and switches the Uno's own LED on pin 13 to match. Only when the state changes does it print "dusk: lamp on" or "daylight: lamp off", so the monitor stays quiet.
The AO pin goes to A0. It gives the light as a voltage, and brighter light reads lower. The sketch prints that reading every 500 ms, so you can see where the switch point sits.
Parts list
3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Photoresistor sensor moduleIn Desk Buddy Kit, Escape Box Kit.
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
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 |
|---|---|
| Photoresistor sensor module pin DO | Arduino Uno R3 pin 2 |
| Photoresistor sensor module pin VCC | Arduino Uno R3 pin 5V |
| Photoresistor sensor module pin GND | Arduino Uno R3 pin GND |
| Photoresistor sensor module pin AO | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Dusk switch: a photoresistor sensor module turns a lamp on at dusk.
//
// module VCC -> 5 V GND -> GND AO -> A0 DO -> pin 2
// lamp the board's LED on pin 13
//
// The module has two outputs. AO is the light as a voltage: the LDR under a
// 10 k resistor, so more light reads lower. DO is the module's own comparator
// against the blue trimmer: high in the dark, low in the light. The sketch
// switches the lamp on DO, so the trimmer sets the light level it comes on
// at, and prints AO so you can see where that is.
//
// Drag the light slider on the module down past about 100 lux and the lamp
// comes on; the DO lamp on the module goes out at the same moment.
const int AO_PIN = A0;
const int DO_PIN = 2;
bool dark = false;
unsigned long printedAt = 0;
void setup() {
Serial.begin(9600);
pinMode(DO_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Dusk switch ready");
}
void loop() {
bool nowDark = digitalRead(DO_PIN) == HIGH;
if (nowDark != dark) {
dark = nowDark;
digitalWrite(LED_BUILTIN, dark ? HIGH : LOW);
Serial.println(dark ? "dusk: lamp on" : "daylight: lamp off");
}
if (millis() - printedAt >= 500) {
printedAt = millis();
Serial.print("light reading ");
Serial.println(analogRead(AO_PIN));
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial, sets pin 2 as an input for the comparator and LED_BUILTIN as an output, and prints "Dusk switch ready".
void setup() {
Serial.begin(9600);
pinMode(DO_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Dusk switch ready");
}loop sketch.ino
Reads DO and only acts when it differs from the saved dark value, switching the lamp and printing a message. Separately, it uses millis() to print the A0 reading every 500 ms without blocking.
void loop() {
bool nowDark = digitalRead(DO_PIN) == HIGH;
if (nowDark != dark) {
dark = nowDark;
digitalWrite(LED_BUILTIN, dark ? HIGH : LOW);
Serial.println(dark ? "dusk: lamp on" : "daylight: lamp off");
}
if (millis() - printedAt >= 500) {
printedAt = millis();
Serial.print("light reading ");
Serial.println(analogRead(AO_PIN));
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lamp comes on at the wrong light level This project
Turn the blue trimmer on the module. It sets where DO flips. - The lamp is backward This project
DO is high in the dark. If you want the opposite, changeHIGHtoLOWin the comparison. - The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board. - Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.