Darkness Switch with Relay
An automatic light switch: when the room goes dark, a relay goes “click” and swaps a red light for a green one. The potentiometer sets how dark it has to be before it switches.
Bill of materials
Everything you need to build it, with the reference you will see on the schematic.
| Qty | Part | Ref. |
|---|---|---|
| 1 | 100 kΩ potentiometer | RV1 |
| 1 | 10 kΩ resistor (brown-black-orange) | R1 |
| 1 | GL5528 photoresistor (LDR) | LDR1 |
| 2 | BC547 NPN transistor | Q1, Q2 |
| 1 | 1 kΩ resistor (brown-black-red) | R2 |
| 1 | 9 V relay (coil of about 330 Ω, one changeover contact) | K1 |
| 1 | Diode 1N4007 | D1 |
| 2 | 470 Ω resistor (yellow-violet-brown) | R3, R4 |
| 1 | green LED | D2 |
| 1 | red LED | D3 |
| 1 | 9 V battery with snap clip | BT1 |
| 1 | Breadboard (400 tie-points or more) | |
| 10 | Jumper wires |
Missing parts? The kits recommended by Impeca Tech include almost all of them. See the kits
Schematic & wiring
On the left, the circuit diagram; on the right, the same circuit built on a breadboard. Both connect exactly the same things.
Wire it step by step
-
Place the components
- 100 kΩ potentiometer RV1: its three legs in 3f (end 1), 4f (wiper, the middle one) and 5f (end 2).
- 10 kΩ resistor R1 (bands: brown, black, orange, gold): one leg in 4i and the other in 7i (either way round).
- Photoresistor (LDR) LDR1: one leg in 9f and the other in 11f (either way round).
- BC547 transistor Q1 with the flat face (the one with the text) towards you, left to right: collector (C) in 6f, base (B) in 7f, emitter (E) in 8f.
- 1 kΩ resistor R2 (bands: brown, black, red, gold): one leg in 8i and the other in 12i (either way round).
- BC547 transistor Q2 with the flat face (the one with the text) towards you, left to right: collector (C) in 13f, base (B) in 14f, emitter (E) in 15f.
- 1N4007 diode D1: the end with the silver stripe (cathode) in 16i and the other end (anode) in 13i.
- 470 Ω resistor R3 (bands: yellow, violet, brown, gold): one leg in 19f and the other in 22f (either way round).
- green LED D2: the long leg (+, anode) in 22h and the short leg (−, cathode, flat side) in 21h.
- 470 Ω resistor R4 (bands: yellow, violet, brown, gold): one leg in 20i and the other in 17i (either way round).
- red LED D3: the long leg (+, anode) in 17f and the short leg (−, cathode, flat side) in 18f.
-
Add the jumper wires
- Red wire: from 3j to the + (red) rail.
- Red wire: from 6j to the + (red) rail.
- Red wire: from 16j to the + (red) rail.
- Orange wire: from 4j to 5j.
- Turquoise wire: from 7j to 9j.
- Black wire: from 11j to the − (blue) rail.
- Black wire: from 15j to the − (blue) rail.
- Black wire: from 18j to the − (blue) rail.
- Black wire: from 21j to the − (blue) rail.
- Purple wire: from 12j to 14j.
-
Connect the external parts
- 9 V relay (K1), one wire per terminal: A1 → the + rail; A2 → 13j; COM → the + rail; NO → 19j; NC → 20j.
-
Connect the battery
- Connect the 9 V battery to its clip: the clip’s red lead to the + (red) rail and the black one to the − (blue) rail. Do this last, once everything else is in place.
-
Test the circuit
- In normal light the red LED should be on. Cover the LDR with your hand or the dark tube: you hear the “click” and it switches to green. Take your hand away and it returns to red. If nothing changes, turn RV1 towards the other end to vary the sensitivity. Measure the coil with the multimeter: more than 6.5 V when active and almost 0 V at rest.
How to read the holes: the letter is the row and the number is the column of the breadboard (for example 7f). The red (+) and blue (−) rails are the two long lines along the edge.
How does it work?
- 1In light, the LDR has low resistance and keeps the base of Q1 near 0 V
- 2As it gets dark, the LDR’s resistance rises and the base voltage goes up
- 3Q1 and Q2 (a Darlington pair) conduct and the relay coil magnetises
- 4The contact changes with a “click”: the red LED goes off and the green one comes on
The LDR and the sum of RV1 (a 100 kΩ potentiometer used as a variable resistor) plus R1 (10 kΩ) form a voltage divider. In light, the LDR is a few hundred ohms to a few kΩ and the base voltage stays below 1.4 V: the transistors do not conduct. In dim light (about 3 lux) the LDR is above 30 kΩ and in the dark above 200 kΩ, the voltage rises and Q1 and Q2 conduct. Q1 and Q2 form a Darlington pair: the current of Q1 feeds the base of Q2, so the total gain is huge and a tiny current is enough to move the relay (about 27 mA in the coil). R2 (1 kΩ) limits the base current of Q2. Turn RV1 to change the threshold: more resistance needs more darkness to switch; with RV1 at minimum the circuit is very sensitive and may switch even in an ordinary room. D1 (1N4007) protects the transistors from the coil’s voltage spike at turn-off. Here the relay contacts only drive LEDs: never connect them to household mains.
Testing & troubleshooting
What you should see
- Daylight (1000 lux), sensitivity at half
- Red LED current (NC contact)≈ 15 mA
- Green LED current (NO contact)under 0.1 mA
- Coil voltage≈ 0.00 V
- Lit room (200 lux)
- Red LED current (NC contact)≈ 15 mA
- Green LED current (NO contact)under 0.1 mA
- Coil voltage≈ 0.00 V
- Dim light (3 lux)
- Red LED current (NC contact)under 0.1 mA
- Green LED current (NO contact)≈ 15 mA
- Coil voltage≈ 8.87 V
- Darkness (0.1 lux)
- Red LED current (NC contact)under 0.1 mA
- Green LED current (NO contact)≈ 15 mA
- Coil voltage≈ 8.88 V
- Potentiometer at minimum, daylight (relay must not stay on)
- Red LED current (NC contact)≈ 15 mA
- Green LED current (NO contact)under 0.1 mA
- Coil voltage≈ 0.00 V
Approximate values, calculated with typical parts. With real parts they can vary by 10 % or more.
If something does not work
The relay is always on, even in light
RV1 is too close to the minimum and the circuit is too sensitive: turn it towards more resistance. Also check the LDR receives light (do not hide it under wires or your hand) and that R1 is 10 kΩ (brown-black-orange).
The relay does not switch even in the dark
Turn RV1 towards the minimum to raise the sensitivity and check the battery is fresh (below about 7.5 V the relay does not pull in well). Check that Q1 and Q2 have their flat side as in the drawing, and that the 1 kΩ R2 sits between the emitter of Q1 and the base of Q2.
It keeps switching on and off with repeated clicks
You are right at the darkness threshold: the LDR sits in an in-between light level. Turn RV1 a little to move the threshold away from the room light, or give the LDR steadier light. Also check the battery is not run down.
Want to automate this circuit with code?
Learn how to program sensors and custom schedules in our e-book “Arduino from Scratch”.