Automatic night lamp with LDR and relay
Build it step by step
0 of 7 doneBuild the divider
Check its voltage in light and dark with a multimeter.
Set the reference
Turn the trimmer to mid position.
Add the comparator
Confirm pin 1 changes when you cover the LDR.
Add the transistor and relay
Hear the click and check the diode direction.
Add hysteresis
Fit the 100 kΩ feedback resistor.
Connect a 12 V lamp
Use a separate low-voltage supply for the lamp.
Tune at dusk
Turn the trimmer until it switches at the light level you want.
How do you build an automatic night lamp with LDR and relay?
An LDR and transistor switch a relay at dusk, and an LM393 comparator gives a clean on and off without flicker. You need LM393 Comparator, LDR Photoresistor, Resistor 10 kΩ × 2, Trimmer potentiometer 10 kΩ (3296), Resistor 100 kΩ, BC547 NPN transistor, Resistor 1 kΩ, Relay 5 V SPDT (sugar cube), Rectifier Diode 1N4007, 5 V 2 A USB adapter and Perfboard (dot PCB) 7 × 9 cm. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 11
Parts list
- Integrated CircuitsBuyLM393 Comparator × 1
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 2
- PassivesBuy
- PassivesBuyResistor 100 kΩ × 1
- SemiconductorsBuy
- PassivesBuyResistor 1 kΩ × 1
- SwitchesBuy
- SemiconductorsBuy
- PowerBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 5 V | LM393 pin 8 and pin 4 to GND | Power for the comparator |
| 5 V to LDR to a node, 10 kΩ from the node to GND | LM393 pin 3 (+ input) | Light divider (high in light) |
| 5 V to the trimmer, wiper | LM393 pin 2 (− input) | Threshold reference |
| 100 kΩ | Between LM393 pin 1 (output) and pin 3 (+ input) | Hysteresis feedback |
| 10 kΩ | From 5 V to LM393 pin 1 | Pull-up for the open-collector output |
| LM393 pin 1 | 1 kΩ to BC547 base | Relay driver |
| BC547 collector | Relay coil, other coil end to 5 V | Relay switching |
| 1N4007 | Across the coil, stripe to 5 V | Flyback protection |
| Relay contacts | A 12 V lamp and its supply, in series | Low-voltage load only |
No code needed
The LDR and 10 kΩ resistor form a divider whose voltage is high in daylight and low in the dark. The comparator compares it with the trimmer's voltage. When it falls below the setting the output goes low, which would leave the relay off, so wire the divider the other way round (LDR to ground, resistor to 5 V) if you want the lamp on in the dark: test with the lamp and swap the two if it behaves backwards. The 100 kΩ feedback gives hysteresis, so the lamp does not flicker as light slowly changes at dusk. Switch only a low-voltage lamp, such as a 12 V LED lamp with its own supply. This build never switches mains; if you want a mains lamp, a qualified electrician must wire the mains side with an approved relay unit in an enclosure.
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Cover the LDR | Relay clicks and the lamp changes state |
| Uncover it | Relay clicks back |
| Slowly shade the LDR | One clean click, no chatter |
| Torch near the LDR at night | Lamp turns off (see the note) |
If it does not work
Relay chatters
Check the 100 kΩ feedback resistor and the pull-up.
Behaves backwards
Swap the LDR and the 10 kΩ resistor.
Relay stays on
Check the BC547 pins (flat side view: E, B, C).
Switches at the wrong light
Adjust the trimmer.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the automatic night lamp with LDR and relay?
LM393 Comparator, LDR Photoresistor, Resistor 10 kΩ × 2, Trimmer potentiometer 10 kΩ (3296), Resistor 100 kΩ, BC547 NPN transistor, Resistor 1 kΩ, Relay 5 V SPDT (sugar cube), Rectifier Diode 1N4007, 5 V 2 A USB adapter and Perfboard (dot PCB) 7 × 9 cm.
How long does the automatic night lamp with LDR and relay take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the automatic night lamp with LDR and relay works?
Cover the LDR: Relay clicks and the lamp changes state. Uncover it: Relay clicks back. Slowly shade the LDR: One clean click, no chatter.
Relay chatters: what should I check?
Check the 100 kΩ feedback resistor and the pull-up.
Behaves backwards: what should I check?
Swap the LDR and the 10 kΩ resistor.
Relay stays on: what should I check?
Check the BC547 pins (flat side view: E, B, C).
Switches at the wrong light: what should I check?
Adjust the trimmer.
