Practical case: Automatic twilight switch

Automatic twilight switch prototype (Maker Style)

Level: Basic. Build a circuit that automatically turns on an LED when ambient light drops below a specific level.

Objective and use case

You will design and assemble a light-sensing circuit using a photoresistor (LDR) and a transistor to control an LED based on environmental brightness. The circuit acts as a logical NOT gate relative to light intensity: Light = Output OFF, Dark = Output ON.

Why it is useful:
* Street lighting: Automating street lamps to turn on only at night to save energy.
* Garden lights: Solar-powered garden fixtures that activate at dusk.
* Security systems: Triggering low-light recording or illumination.
* Display efficiency: Adjusting screen brightness or backlighting based on room conditions.

Expected outcome:
* When the LDR is exposed to bright light, the LED remains OFF.
* When the LDR is covered (simulating darkness), the LED turns ON.
* The voltage at the transistor base (V_BASE) increases as light intensity decreases.

Target audience: Beginners learning about sensors and transistor switching.

Materials

  • V1: 9 V DC battery or power supply.
  • R1: 10 kΩ resistor, function: upper leg of voltage divider (pull-up).
  • R2: LDR (Light Dependent Resistor), GL5528 or similar, function: light sensor (lower leg).
  • R3: 470 Ω resistor, function: LED current limiting.
  • Q1: 2N3904 NPN transistor, function: electronic switch.
  • D1: Red LED, function: output indicator.

Wiring guide

Construct the circuit following these connections using the specific node names:

  • Power Supply:

    • V1 (+): Connects to node VCC.
    • V1 (-): Connects to node 0 (GND).
  • Sensor Stage (Voltage Divider):

    • R1 (10 kΩ): Connects between VCC and node V_BASE.
    • R2 (LDR): Connects between node V_BASE and 0 (GND).
  • Switching Stage:

    • Q1 (Base): Connects to node V_BASE.
    • Q1 (Emitter): Connects to node 0 (GND).
    • Q1 (Collector): Connects to node N_LED_CATHODE.
  • Output Stage:

    • R3 (470 Ω): Connects between VCC and node N_LED_ANODE.
    • D1 (Anode): Connects to node N_LED_ANODE.
    • D1 (Cathode): Connects to node N_LED_CATHODE.

Conceptual block diagram

Conceptual block diagram — Light-Controlled Switch
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

[ SENSOR STAGE ]                   [ SWITCHING STAGE ]                 [ OUTPUT STAGE ]

   [ VCC 9 V Source ]
          |
          v
   [ R1: 10k Pull-Up ]
          |
          v
   [ Node: V_BASE  ] --(Trigger)--> [ Base: Q1 (2N3904)   ]
          |                         [                     ]
          v                         [ Coll: N_LED_CATHODE ] --(Sink)--> [ Cathode: D1 LED ]
   [ R2: LDR Sensor ]               [                     ]             [ Node: N_LED_ANODE ]
          |                         [ Emit: GND           ]             [ Anode:   D1 LED   ]
          v                                                             [         ^         ]
       [ GND ]                                                          [         |         ]
                                                                        [ R3: 470 Resistor  ]
                                                                                  ^
                                                                                  |
                                                                             [ VCC 9 V ]
Schematic (ASCII)

Measurements and tests

To validate the circuit operation, perform the following steps with a multimeter:

  1. Light Condition (Simulation): Shine a flashlight on R2 (LDR) or ensure the room is bright.

    • Measure voltage at V_BASE relative to 0 (GND). It should be low (< 0.6 V).
    • Observe D1: It should be OFF.
    • Measure voltage at N_LED_CATHODE relative to 0 (GND). It should be close to VCC (floating high through the LED).
  2. Dark Condition (Simulation): Cover R2 (LDR) completely with your finger or a cap.

    • Measure voltage at V_BASE. It should rise above 0.7 V.
    • Observe D1: It should turn ON.
    • Measure voltage at N_LED_CATHODE (Collector). It should drop to near 0 V (Saturation voltage, approx 0.1 V – 0.2 V).

SPICE netlist and simulation

Reference SPICE Netlist (ngspice) — excerptFull SPICE netlist (ngspice)

* Practical case: Automatic twilight switch
* 
* This netlist implements a twilight switch where an LED turns ON
* when the light level drops (simulated by increasing LDR resistance).

* --- Models ---
* Standard NPN Transistor Model
.model 2N3904 NPN(IS=1E-14 VAF=100 BF=200 IKF=0.3 XTB=1.5 BR=3 CJC=8E-12 CJE=25E-12 TR=460E-9 TF=400E-12 ITF=0.6 VTF=10 XTF=30 RB=10 RC=1 RE=0.1)
* Generic Red LED Model (Vf approx 1.8V)
.model LED_RED D(IS=1e-14 N=2.5 RS=5 BV=5 IBV=10u)

* --- Power Supply ---
* V1: 9 V DC source connected to VCC and GND (0)
V1 VCC 0 DC 9

* --- Sensor Stage (Voltage Divider) ---
* R1: 10 kΩ Pull-up resistor
R1 VCC V_BASE 10k

* R2: LDR (Light Dependent Resistor)
* ... (truncated in public view) ...

Copy this content into a .cir file and run with ngspice.

🔒 Part of this section is premium. With the 7-day pass or the monthly membership you can access the full content (materials, wiring, detailed build, validation, troubleshooting, variants and checklist) and download the complete print-ready PDF pack.

* Practical case: Automatic twilight switch
* 
* This netlist implements a twilight switch where an LED turns ON
* when the light level drops (simulated by increasing LDR resistance).

* --- Models ---
* Standard NPN Transistor Model
.model 2N3904 NPN(IS=1E-14 VAF=100 BF=200 IKF=0.3 XTB=1.5 BR=3 CJC=8E-12 CJE=25E-12 TR=460E-9 TF=400E-12 ITF=0.6 VTF=10 XTF=30 RB=10 RC=1 RE=0.1)
* Generic Red LED Model (Vf approx 1.8V)
.model LED_RED D(IS=1e-14 N=2.5 RS=5 BV=5 IBV=10u)

* --- Power Supply ---
* V1: 9 V DC source connected to VCC and GND (0)
V1 VCC 0 DC 9

* --- Sensor Stage (Voltage Divider) ---
* R1: 10 kΩ Pull-up resistor
R1 VCC V_BASE 10k

* R2: LDR (Light Dependent Resistor)
* Modeled as a behavioral resistor to simulate changing light conditions.
* Low Resistance = Bright Light (LED OFF), High Resistance = Dark (LED ON).
* Simulation: Resistance ramps from 100 Ohm to 3000 Ohm over 5ms.
* The switching threshold (Vbe ~ 0.65V) occurs around R2 = 780 Ohms.
R2 V_BASE 0 R='100 + 2900 * (time / 0.005)'

* --- Switching Stage ---
* Q1: 2N3904 NPN Transistor
* Base -> V_BASE, Collector -> N_LED_CATHODE, Emitter -> GND (0)
Q1 N_LED_CATHODE V_BASE 0 2N3904

* --- Output Stage ---
* R3: 470 Ω LED current limiting resistor
R3 VCC N_LED_ANODE 470

* D1: Red LED
* Anode -> N_LED_ANODE, Cathode -> N_LED_CATHODE
D1 N_LED_ANODE N_LED_CATHODE LED_RED

* --- Simulation Directives ---
* Perform a transient analysis for 5ms to observe the switching behavior
.tran 10u 5m

* Print required voltages for verification
* V_BASE: Shows the sensor voltage rising.
* N_LED_CATHODE: Shows the collector voltage dropping when Q1 turns ON.
.print tran V(V_BASE) V(N_LED_CATHODE) V(N_LED_ANODE)

.op
.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (508 rows)
Index   time            v(v_base)       v(n_led_cathode v(n_led_anode)
0	0.000000e+00	8.910891e-02	8.519679e+00	9.000000e+00
1	1.000000e-07	8.915880e-02	8.519729e+00	9.000000e+00
2	2.000000e-07	8.920993e-02	8.519780e+00	9.000000e+00
3	4.000000e-07	8.931227e-02	8.519882e+00	9.000000e+00
4	8.000000e-07	8.951694e-02	8.520087e+00	9.000000e+00
5	1.600000e-06	8.992625e-02	8.520496e+00	9.000000e+00
6	3.200000e-06	9.074475e-02	8.521314e+00	9.000000e+00
7	6.400000e-06	9.238131e-02	8.522950e+00	9.000000e+00
8	1.280000e-05	9.565263e-02	8.526219e+00	9.000000e+00
9	2.280000e-05	1.007592e-01	8.531319e+00	9.000000e+00
10	3.280000e-05	1.058600e-01	8.536410e+00	9.000000e+00
11	4.280000e-05	1.109549e-01	8.541491e+00	9.000000e+00
12	5.280000e-05	1.160440e-01	8.546563e+00	9.000000e+00
13	6.280000e-05	1.211273e-01	8.551627e+00	9.000000e+00
14	7.280000e-05	1.262047e-01	8.556682e+00	9.000000e+00
15	8.280000e-05	1.312764e-01	8.561728e+00	9.000000e+00
16	9.280000e-05	1.363422e-01	8.566765e+00	9.000000e+00
17	1.028000e-04	1.414023e-01	8.571793e+00	9.000000e+00
18	1.128000e-04	1.464566e-01	8.576812e+00	9.000000e+00
19	1.228000e-04	1.515051e-01	8.581823e+00	9.000000e+00
20	1.328000e-04	1.565479e-01	8.586824e+00	9.000000e+00
21	1.428000e-04	1.615849e-01	8.591815e+00	9.000000e+00
22	1.528000e-04	1.666162e-01	8.596796e+00	9.000000e+00
23	1.628000e-04	1.716418e-01	8.601767e+00	9.000000e+00
... (484 more rows) ...

Common mistakes and how to avoid them

  1. Swapping the Resistor and LDR: Placing the LDR on top and R1 on the bottom creates a «Morning Alarm» (turns on when light detected) instead of a twilight switch. Ensure R1 connects to VCC and the LDR connects to 0.
  2. LED Polarity Reversed: The LED will not light up if the anode and cathode are swapped. Ensure the flat side (Cathode) connects to the transistor collector.
  3. Transistor Pinout Confusion: Confusing Collector, Base, and Emitter on the 2N3904 is common. Verify the datasheet for your specific package (usually E-B-C from left to right when flat side faces you).

Troubleshooting

  • LED is always ON:
    • Ambient light might be too low. Use a flashlight to test the sensor.
    • R1 (Pull-up) value is too low, providing too much base current even in light. Increase R1 to 22 kΩ or 47 kΩ.
  • LED is always OFF:
    • Check transistor orientation.
    • R1 might be too high, preventing the base voltage from reaching 0.7 V even in darkness.
    • LDR might be shorted.
  • LED is dim in darkness:
    • The battery voltage (V1) is low.
    • R3 (Current limiting) is too high; try reducing it slightly (do not go below 220 Ω).

Possible improvements and extensions

  1. Sensitivity Adjustment: Replace R1 with a 50 kΩ or 100 kΩ potentiometer to manually tune the exact darkness level required to trigger the LED.
  2. Hysteresis: Add a feedback resistor between the Collector and the Base to create a «Schmitt Trigger» effect, preventing the LED from flickering at the twilight threshold.

More Practical Cases on Prometeo.blog

Find this product and/or books on this topic on Amazon

Go to Amazon

As an Amazon Associate, I earn from qualifying purchases. If you buy through this link, you help keep this project running.

Quick Quiz

Question 1: What is the primary objective of the circuit described in the text?




Question 2: Which component acts as the light sensor in this circuit?




Question 3: How does the circuit behave logically relative to light intensity?




Question 4: What is a common real-world use case for this type of circuit mentioned in the text?




Question 5: What happens to the voltage at the transistor base (V_BASE) as light intensity decreases?




Question 6: Which component functions as the electronic switch in the circuit?




Question 7: What is the function of the resistor R3 (470 Ω) typically found in this circuit?




Question 8: What is the role of the 10 kΩ resistor (R1) in the materials list?




Question 9: What is the expected state of the LED when the LDR is exposed to bright light?




Question 10: Who is the target audience for this circuit project?




Carlos Núñez Zorrilla
Carlos Núñez Zorrilla
Electronics & Computer Engineer

Telecommunications Electronics Engineer and Computer Engineer (official degrees in Spain).

Follow me:
Scroll to Top