Practical case: Single-axis solar tracker

Single-axis solar tracker prototype (Maker Style)

Level: Medium. Design a control circuit that compares light levels from two sensors to orient a motor towards the brightest light source.

Objective and use case

This practical case guides you through building an analog control loop that automatically orients a mechanism towards a light source using photoresistors (LDRs) and operational amplifiers. You will construct a «sun seeker» that actively balances two light inputs to drive a motor in the corresponding direction.

  • Real-world applications:
  • Solar Energy: Increases photovoltaic panel efficiency by keeping panels perpendicular to the sun throughout the day.
  • Robotics: Enables light-seeking behaviors (phototaxis) in autonomous robots.
  • Home Automation: Controls smart blinds to regulate room temperature based on sunlight intensity.
  • Expected outcome:
  • When the light source is balanced, the motor remains stationary.
  • When LDR1 is shaded, the voltage difference triggers the motor to spin Clockwise (CW).
  • When LDR2 is shaded, the motor spins Counter-Clockwise (CCW).
  • Target audience: Electronics students familiar with voltage dividers and OpAmps.

Materials

  • V1: 9 V DC power supply (Power source).
  • R1: Photoresistor (LDR), function: Left light sensor.
  • R2: Photoresistor (LDR), function: Right light sensor.
  • R3: 10 kΩ resistor, function: Voltage divider bottom leg for R1.
  • R4: 10 kΩ resistor, function: Voltage divider bottom leg for R2.
  • U1: LM358, function: Dual Operational Amplifier (Comparators).
  • U2: L293D, function: H-Bridge Motor Driver IC.
  • M1: 9 V DC Gear Motor, function: Tracking actuator.
  • C1: 100 nF capacitor, function: Power supply decoupling.

Wiring guide

This circuit uses two parallel voltage dividers compared by two OpAmps to determine motor direction.

  • Power Supply:
  • Connect V1 positive terminal to node VCC.
  • Connect V1 negative terminal to node GND (0).
  • Connect C1 between VCC and GND.

  • Sensors (Dual Voltage Divider):

  • Connect R1 (LDR Left) between VCC and node VA (Sensor Voltage A).
  • Connect R3 between VA and GND.
  • Connect R2 (LDR Right) between VCC and node VB (Sensor Voltage B).
  • Connect R4 between VB and GND.

  • Comparators (LM358 – U1):

  • Comparator A (Turn Right/CW Logic):
    • Connect U1 Non-inverting input (+) to node VA.
    • Connect U1 Inverting input (-) to node VB.
    • Connect U1 Output A to node SIG_CW.
  • Comparator B (Turn Left/CCW Logic):
    • Connect U1 Non-inverting input (+) to node VB.
    • Connect U1 Inverting input (-) to node VA.
    • Connect U1 Output B to node SIG_CCW.
  • Connect U1 VCC pin to VCC and GND pin to GND.

  • Motor Driver (L293D – U2):

  • Connect U2 Input 1 to node SIG_CW.
  • Connect U2 Input 2 to node SIG_CCW.
  • Connect U2 Enable 1 pin to VCC.
  • Connect U2 Output 1 to node M_POS.
  • Connect U2 Output 2 to node M_NEG.
  • Connect U2 VCC1 (Logic) and VCC2 (Power) to VCC.
  • Connect U2 GND pins to GND.

  • Actuator:

  • Connect M1 (Motor) between nodes M_POS and M_NEG.

Conceptual block diagram

Conceptual block diagram — LM358 Solar Tracking Logic
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

[ INPUTS / SENSORS ]               [ LOGIC / PROCESSING ]                  [ ACTUATOR ]

   [ Power Supply Block ]
   [ Source: V1 (9 V)    ] --(VCC/GND Power)--> (Distributes to all ICs and Sensors)
   [ Filter: C1 (100nF) ]

                                         [ U1: LM358 Dual OpAmp ]
                                         |                      |
   [ Left Light Sensor  ]                | Comparator A (Logic) |
   [ Top: R1 (LDR)      ] --(Signal VA)->| Input: VA > VB ?     |--(SIG_CW)--->+
   [ Bot: R3 (10k Ohm)  ]                | Output: Turn CW      |              |
                                         |                      |              |
                                         |                      |              v
                                         | Comparator B (Logic) |      [ U2: L293D H-Bridge ]
   [ Right Light Sensor ]                | Input: VB > VA ?     |      |                    |
   [ Top: R2 (LDR)      ] --(Signal VB)->| Output: Turn CCW     |      | Input 1: CW Sig    |
   [ Bot: R4 (10k Ohm)  ]                |                      |      | Input 2: CCW Sig   |===(High Current)==> [ M1: Gear Motor ]
                                         +----------+-----------+      | Enable: VCC        |      (9 V DC)
                                                    |                  | VCC1/VCC2: 9 V      |
                                                    +--(SIG_CCW)------>| GND: Common        |
                                                                       +--------------------+
Schematic (ASCII)

Electrical diagram

Electrical diagram for practical case: Single-axis solar tracker
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

Measurements and tests

Follow these steps to validate the tracker logic:

  1. Static Equilibrium Test:

    • Expose both LDRs to ambient light equally.
    • Measure the voltage at node VA and VB. They should be approximately equal.
    • Measure SIG_CW and SIG_CCW. Both should be Low (approx. 0 V) or balanced, keeping the motor stopped.
  2. Left Shade Simulation:

    • Cover R1 (Left LDR) with your hand.
    • Observation: The resistance of R1 increases, causing voltage at VA to drop.
    • Logic Check: Since VB > VA, Comparator B (Non-inverting = VB) should go High (SIG_CCW ≈ VCC).
    • Actuator: The motor should spin Counter-Clockwise.
  3. Right Shade Simulation:

    • Expose R1 to light and cover R2 (Right LDR).
    • Observation: The resistance of R2 increases, causing voltage at VB to drop.
    • Logic Check: Since VA > VB, Comparator A (Non-inverting = VA) should go High (SIG_CW ≈ VCC).
    • Actuator: The motor should spin Clockwise.

SPICE netlist and simulation

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

* Single-axis Solar Tracker Simulation
* Based on Practical Electronics Breadboard Case

* --- Power Supply ---
* V1: 9 V DC power supply
V1 VCC 0 DC 9V
* C1: 100 nF capacitor (Decoupling)
C1 VCC 0 100nF

* --- Dynamic Light Stimulus (Virtual Control) ---
* This source simulates the position of the sun moving from Left to Right.
* 0V = Light on Left Sensor, 5V = Light on Right Sensor.
* Sweeps linearly from 0V to 5V over 100ms.
V_LIGHT LIGHT_POS 0 PWL(0 0 100m 5)

* --- Sensors (LDRs) ---
* Modeled as voltage-dependent resistors controlled by LIGHT_POS.
* R1 (Left LDR): Resistance increases as Light moves Right (LIGHT_POS increases).
* Range: 1k (Bright) to 50k (Dark).
R1 VCC VA R = '1k + 49k * (V(LIGHT_POS)/5)'
* ... (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.

* Single-axis Solar Tracker Simulation
* Based on Practical Electronics Breadboard Case

* --- Power Supply ---
* V1: 9 V DC power supply
V1 VCC 0 DC 9V
* C1: 100 nF capacitor (Decoupling)
C1 VCC 0 100nF

* --- Dynamic Light Stimulus (Virtual Control) ---
* This source simulates the position of the sun moving from Left to Right.
* 0V = Light on Left Sensor, 5V = Light on Right Sensor.
* Sweeps linearly from 0V to 5V over 100ms.
V_LIGHT LIGHT_POS 0 PWL(0 0 100m 5)

* --- Sensors (LDRs) ---
* Modeled as voltage-dependent resistors controlled by LIGHT_POS.
* R1 (Left LDR): Resistance increases as Light moves Right (LIGHT_POS increases).
* Range: 1k (Bright) to 50k (Dark).
R1 VCC VA R = '1k + 49k * (V(LIGHT_POS)/5)'

* R2 (Right LDR): Resistance decreases as Light moves Right.
* Range: 50k (Dark) to 1k (Bright).
R2 VCC VB R = '1k + 49k * (1 - V(LIGHT_POS)/5)'

* --- Voltage Divider Bottom Legs ---
* R3: 10 kΩ resistor for R1
R3 VA 0 10k
* R4: 10 kΩ resistor for R2
R4 VB 0 10k

* --- Comparators (U1: LM358) ---
* U1 is a Dual OpAmp. We define a subcircuit matching the 8-pin DIP pinout.
* Pinout: 1=OutA, 2=In-A, 3=In+A, 4=GND, 5=In+B, 6=In-B, 7=OutB, 8=VCC
* Wiring Guide:
* Comparator A (CW): (+) VA, (-) VB -> Out SIG_CW
* Comparator B (CCW): (+) VB, (-) VA -> Out SIG_CCW
XU1 SIG_CW VB VA 0 VB VA SIG_CCW VCC LM358_DIP8

* --- Motor Driver (U2: L293D) ---
* U2 is an H-Bridge Driver. We define a subcircuit for the used pins.
* Pinout used: 1=EN1, 2=IN1, 3=OUT1, 4/5=GND, 6=OUT2, 7=IN2, 8=VCC2, 16=VCC1
* Wiring Guide:
* IN1=SIG_CW, IN2=SIG_CCW, OUT1=M_POS, OUT2=M_NEG, EN1=VCC
XU2 VCC SIG_CW M_POS 0 0 M_NEG SIG_CCW VCC VCC L293D_BRIDGE

* --- Actuator (M1: 9V DC Gear Motor) ---
* Modeled as a resistive/inductive load.
R_M1 M_POS M_INT 20
L_M1 M_INT M_NEG 5mH

* --- Subcircuit Definitions ---

.subckt LM358_DIP8 OUTA INMA INPA GND INPB INMB OUTB VCC
* Comparator A Behavior (Sigmoid for convergence)
* Output swings approx 0V to VCC-1.5V
B_OUTA OUTA 0 V = (V(VCC)-1.5) / (1 + exp(-50*(V(INPA)-V(INMA)))) + 0.05
* Comparator B Behavior
B_OUTB OUTB 0 V = (V(VCC)-1.5) / (1 + exp(-50*(V(INPB)-V(INMB)))) + 0.05
.ends

.subckt L293D_BRIDGE EN1 IN1 OUT1 GND1 GND2 OUT2 IN2 VCC2 VCC1
* Logic Threshold approx 2.0V.
* Output Voltage ~ VCC2 - 1.4V drop.
* Enable Logic
B_EN node_en 0 V = 1 / (1 + exp(-50*(V(EN1)-2.0)))
* Output 1 (M_POS)
B_O1 OUT1 0 V = V(node_en) * (1/(1+exp(-50*(V(IN1)-2.0)))) * (V(VCC2)-1.4)
* Output 2 (M_NEG)
B_O2 OUT2 0 V = V(node_en) * (1/(1+exp(-50*(V(IN2)-2.0)))) * (V(VCC2)-1.4)
.ends

* --- Simulation Directives ---
.op
* Transient analysis: 100ms duration to capture the full light sweep
.tran 100u 100m

* Print signals to verify logic:
* VA/VB: Sensor Voltages
* SIG_CW/CCW: Comparator Logic Outputs
* M_POS/M_NEG: Motor Drive Voltages
.print tran V(VA) V(VB) V(SIG_CW) V(SIG_CCW) V(M_POS) V(M_NEG) V(LIGHT_POS)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (3024 rows)
Index   time            v(va)           v(vb)           v(sig_cw)
0	0.000000e+00	8.181818e+00	1.500000e+00	7.550000e+00
1	1.000000e-06	8.181454e+00	1.500012e+00	7.550000e+00
2	2.000000e-06	8.181089e+00	1.500025e+00	7.550000e+00
3	4.000000e-06	8.180361e+00	1.500049e+00	7.550000e+00
4	8.000000e-06	8.178903e+00	1.500098e+00	7.550000e+00
5	1.600000e-05	8.175990e+00	1.500196e+00	7.550000e+00
6	3.200000e-05	8.170168e+00	1.500392e+00	7.550000e+00
7	6.400000e-05	8.158542e+00	1.500784e+00	7.550000e+00
8	1.280000e-04	8.135365e+00	1.501569e+00	7.550000e+00
9	2.280000e-04	8.099394e+00	1.502797e+00	7.550000e+00
10	3.280000e-04	8.063833e+00	1.504028e+00	7.550000e+00
11	4.280000e-04	8.028586e+00	1.505260e+00	7.550000e+00
12	5.280000e-04	7.993645e+00	1.506495e+00	7.550000e+00
13	6.280000e-04	7.959008e+00	1.507732e+00	7.550000e+00
14	7.280000e-04	7.924669e+00	1.508970e+00	7.550000e+00
15	8.280000e-04	7.890626e+00	1.510211e+00	7.550000e+00
16	9.280000e-04	7.856873e+00	1.511454e+00	7.550000e+00
17	1.028000e-03	7.823409e+00	1.512699e+00	7.550000e+00
18	1.128000e-03	7.790228e+00	1.513945e+00	7.550000e+00
19	1.228000e-03	7.757327e+00	1.515194e+00	7.550000e+00
20	1.328000e-03	7.724703e+00	1.516445e+00	7.550000e+00
21	1.428000e-03	7.692352e+00	1.517698e+00	7.550000e+00
22	1.528000e-03	7.660271e+00	1.518953e+00	7.550000e+00
23	1.628000e-03	7.628457e+00	1.520211e+00	7.550000e+00
... (3000 more rows) ...

Common mistakes and how to avoid them

  1. LDRs placed too close together:

    • Symptom: The system is insensitive and requires extreme light angles to react.
    • Solution: Mount the LDRs with a physical blinder (a piece of cardboard or plastic) between them so a shadow is cast on one LDR when the light is not perfectly centered.
  2. Driving the motor directly from OpAmps:

    • Symptom: The motor hums but doesn’t turn, or the OpAmp overheats and fails.
    • Solution: Always use a current driver stage like the L293D or a transistor H-Bridge. OpAmps cannot supply the current required by motors (typically >100 mA).
  3. Lack of Deadband (Jittering):

    • Symptom: The motor constantly vibrates back and forth when the light is centered.
    • Solution: This basic topology is a «bang-bang» controller. In advanced designs, add hysteresis resistors to the OpAmps to create a small voltage window where the motor remains off.

Troubleshooting

  • Motor spins in the wrong direction:
    • Cause: The motor polarity is reversed relative to the sensor placement.
    • Fix: Swap the connections of M1 (M_POS and M_NEG) OR physically swap the positions of R1 and R2.
  • Motor runs continuously even in equal light:
    • Cause: Large tolerance difference between the two LDRs or fixed resistors (R3/R4).
    • Fix: Replace one fixed resistor (e.g., R3) with a 10k trim potentiometer to calibrate the bridge balance manually.
  • Nothing happens when light changes:
    • Cause: L293D Enable pin not connected high.
    • Fix: Ensure the Enable pin of the driver is connected to VCC.

Possible improvements and extensions

  1. Sensitivity Control: Replace the fixed resistors R3 and R4 with a single multi-turn potentiometer. Connect the wiper to ground and the ends to the LDRs to allow fine-tuning of the center point.
  2. Solar Power Integration: Replace V1 with a small solar panel and a charging circuit to make the tracker self-sustaining.

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 control circuit described in the text?




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




Question 3: What is a real-world application of this 'sun seeker' circuit mentioned in the text?




Question 4: How does the motor behave when the light source is balanced between the two sensors?




Question 5: What happens to the motor when LDR1 is shaded?




Question 6: What happens to the motor when LDR2 is shaded?




Question 7: Which component is identified as U1 in the context of this circuit?




Question 8: Which component is identified as U2 and is responsible for driving the motor?




Question 9: What is the role of the 10 kΩ resistors (R3 and R4) in the circuit design?




Question 10: Who is the specific target audience for this practical case?




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

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

Follow me:


Practical case: Object counter on conveyor belt

Object counter on conveyor belt prototype (Maker Style)

Level: Basic – Build a light interruption sensor system to detect objects moving on a line.

Objective and use case

In this practical case, you will build an optical barrier using a photoresistor (LDR) and an operational amplifier configured as a voltage comparator. The circuit detects when an opaque object interrupts a continuous beam of light, triggering a signal that can be counted or processed.

Why it is useful:
* Industrial automation: Used to count products moving on a conveyor belt.
* Safety barriers: Detects if a person or object crosses a dangerous boundary.
* Intruder alarms: Triggers a warning when a beam of invisible or visible light is broken.
* Parking systems: Detects the presence of a vehicle in a specific spot.

Expected outcome:
* State A (Light path clear): The sensor receives light, and the output indicator (Red LED) remains OFF (Logic Low).
* State B (Object detected): The object blocks the light, increasing LDR resistance. The output indicator turns ON (Logic High).
* Signal Threshold: The comparator switches states when the sensor voltage crosses the adjustable reference voltage (approx. 2.5 V).

Target audience: Level Basic

Materials

  • V1: 5 V DC power supply, function: main circuit power.
  • R1: 10 kΩ resistor, function: voltage divider top for reference.
  • R2: 10 kΩ resistor, function: voltage divider bottom for reference.
  • R3: 10 kΩ resistor, function: pull-up resistor for the sensor node.
  • R4: Photoresistor (LDR), function: light detection sensor.
  • R5: 330 Ω resistor, function: current limiting for output indicator LED.
  • R6: 330 Ω resistor, function: current limiting for emitter LED.
  • D1: White LED, function: light emitter (simulates the beam source).
  • D2: Red LED, function: output indicator (object detected).
  • U1: LM358 or similar OpAmp, function: voltage comparator.

Wiring guide

This circuit relies on comparing two voltages: a fixed reference (V_REF) and a variable sensor voltage (V_SENSE).

Power Connections
* V1 (+) connects to node VCC.
* V1 (-) connects to node 0 (GND).
* U1 (Pin 8 / VCC) connects to VCC.
* U1 (Pin 4 / GND) connects to 0.

Reference Voltage (V_REF)
* R1 connects between VCC and V_REF.
* R2 connects between V_REF and 0.
* U1 (Pin 2 / Inverting Input) connects to V_REF.
* Note: This sets a fixed threshold of 2.5 V.

Sensor Voltage (V_SENSE)
* R3 connects between VCC and V_SENSE.
* R4 (LDR) connects between V_SENSE and 0.
* U1 (Pin 3 / Non-Inverting Input) connects to V_SENSE.
* Logic: When light is blocked, R4 resistance increases, V_SENSE rises. If V_SENSE > V_REF, Output goes High.

Light Emitter (Source)
* R6 connects between VCC and NODE_EMIT.
* D1 (Anode) connects to NODE_EMIT.
* D1 (Cathode) connects to 0.
* Place D1 physically facing R4 (LDR).

Output Stage
* U1 (Pin 1 / Output) connects to V_OUT.
* R5 connects between V_OUT and NODE_LED.
* D2 (Anode) connects to NODE_LED.
* D2 (Cathode) connects to 0.

Conceptual block diagram

Conceptual block diagram — LM358 Light Beam Interruption Logic
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

[ INPUTS / SENSORS ]                     [ LOGIC / PROCESSING ]             [ OUTPUTS ]

    [ LIGHT SOURCE ]
    [ VCC -> R6 -> D1 (White) ]
             |
      (Light Beam Path)
             |
             V
    [ SENSOR DIVIDER ]
    [ VCC -> R3 -> Node -> R4 ] --(V_SENSE)-->+----------------+
    [ (R4=LDR, varies w/ light)]              |   Pin 3 (+)    |
                                              |                |
                                              |    U1 LM358    |
                                              |   (Comparator) | --(Pin 1)--> [ R5 (330) ] --> [ D2 (Red LED) ] --> GND
                                              |                |
    [ REFERENCE DIVIDER ]                     |                |
    [ VCC -> R1 -> Node -> R2 ] --(V_REF)---->|   Pin 2 (-)    |
    [ (Fixed 2.5 V Threshold)  ]               +----------------+
Schematic (ASCII)

Electrical diagram

Electrical diagram — Object counter on conveyor belt
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

Measurements and tests

  1. Reference Check: Use a multimeter to measure the voltage between V_REF and 0. It should be approximately 2.5 V (half of VCC).
  2. Light Condition (Clear Path): Ensure the Emitter LED (D1) shines on the LDR (R4). Measure V_SENSE. It should be lower than V_REF (e.g., < 2.0 V). The Output LED (D2) should be OFF.
  3. Dark Condition (Object Detected): Place an object (cardboard or finger) between D1 and R4. Measure V_SENSE. It should rise higher than V_REF (e.g., > 3.0 V). The Output LED (D2) should turn ON.
  4. Comparator Output: Measure V_OUT relative to 0. In the «Dark» state, it should be close to 3.5 V – 4 V (High). In the «Light» state, it should be close to 0 V (Low).

SPICE netlist and simulation

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

* Practical case: Object counter on conveyor belt

* -----------------------------------------------------------------------------
* Power Supply
* Wiring: V1 (+) to VCC, V1 (-) to 0 (GND)
* -----------------------------------------------------------------------------
V1 VCC 0 DC 5

* -----------------------------------------------------------------------------
* Reference Voltage Divider
* Wiring: R1 between VCC and V_REF, R2 between V_REF and 0
* Function: Sets threshold voltage (approx 2.5V)
* -----------------------------------------------------------------------------
R1 VCC V_REF 10k
R2 V_REF 0 10k

* -----------------------------------------------------------------------------
* Sensor Network
* Wiring: R3 between VCC and V_SENSE, R4 (LDR) between V_SENSE and 0
* Simulation Note: R4 is modeled as a behavioral resistor to simulate the
* ... (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: Object counter on conveyor belt

* -----------------------------------------------------------------------------
* Power Supply
* Wiring: V1 (+) to VCC, V1 (-) to 0 (GND)
* -----------------------------------------------------------------------------
V1 VCC 0 DC 5

* -----------------------------------------------------------------------------
* Reference Voltage Divider
* Wiring: R1 between VCC and V_REF, R2 between V_REF and 0
* Function: Sets threshold voltage (approx 2.5V)
* -----------------------------------------------------------------------------
R1 VCC V_REF 10k
R2 V_REF 0 10k

* -----------------------------------------------------------------------------
* Sensor Network
* Wiring: R3 between VCC and V_SENSE, R4 (LDR) between V_SENSE and 0
* Simulation Note: R4 is modeled as a behavioral resistor to simulate the
* changing resistance of an LDR when an object blocks the light.
* -----------------------------------------------------------------------------
R3 VCC V_SENSE 10k

* R4 (LDR) Implementation:
* Resistance = 1k (Light/No Object) to 100k (Dark/Object Detected)
* Controlled by dummy voltage source V_OBJ_CTRL
R4 V_SENSE 0 R='1k + 99k / (1 + exp(-50 * (V(V_OBJ_CTRL) - 2.5)))'

* -----------------------------------------------------------------------------
* Light Emitter (Source)
* Wiring: R6 between VCC and NODE_EMIT, D1 Anode to NODE_EMIT, Cathode to 0
* -----------------------------------------------------------------------------
R6 VCC NODE_EMIT 330
D1 NODE_EMIT 0 D_WHITE

* -----------------------------------------------------------------------------
* Comparator (U1: LM358)
* Wiring: Pin 8=VCC, Pin 4=0, Pin 3=V_SENSE (+), Pin 2=V_REF (-), Pin 1=V_OUT
* -----------------------------------------------------------------------------
XU1 V_SENSE V_REF VCC 0 V_OUT LM358_COMP

* -----------------------------------------------------------------------------
* Output Stage
* Wiring: R5 between V_OUT and NODE_LED, D2 Anode to NODE_LED, Cathode to 0
* -----------------------------------------------------------------------------
R5 V_OUT NODE_LED 330
D2 NODE_LED 0 D_RED

* -----------------------------------------------------------------------------
* Dynamic Stimuli (Object Simulation)
* This source drives the behavioral LDR (R4).
* Logic: 0V = Clear (Light), 5V = Object (Dark)
* Timing: Wait 50us, Pulse High for 100us, Repeat every 300us
* -----------------------------------------------------------------------------
V_OBJ V_OBJ_CTRL 0 PULSE(0 5 50u 10u 10u 100u 300u)

* -----------------------------------------------------------------------------
* Models and Subcircuits
* -----------------------------------------------------------------------------
.model D_WHITE D(IS=1e-14 N=4 RS=10) ; High Vf simulation for White LED
.model D_RED D(IS=1e-12 N=2 RS=5)    ; Standard Red LED

* Behavioral OpAmp Subcircuit (Comparator)
* Pinout Order: Non-Inv(+), Inv(-), VCC, GND, Output
.subckt LM358_COMP P M V_POS V_NEG OUT
  * Sigmoid function for robust switching behavior (Rail-to-Rail logic approx)
  * V(OUT) approaches V_POS when P > M, V_NEG when P < M
  B1 OUT 0 V = V(V_POS) * (1 / (1 + exp(-100 * (V(P) - V(M)))))
.ends

* -----------------------------------------------------------------------------
* Analysis Directives
* -----------------------------------------------------------------------------
.op
.tran 1u 500u

* Print required signals for batch processing
.print tran V(V_SENSE) V(V_REF) V(V_OUT) V(V_OBJ_CTRL)

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (1064 rows)
Index   time            v(v_sense)      v(v_ref)        v(v_out)
0	0.000000e+00	4.545455e-01	2.500000e+00	7.345271e-89
1	1.000000e-08	4.545455e-01	2.500000e+00	7.345271e-89
2	2.000000e-08	4.545455e-01	2.500000e+00	7.345271e-89
3	4.000000e-08	4.545455e-01	2.500000e+00	7.345271e-89
4	8.000000e-08	4.545455e-01	2.500000e+00	7.345271e-89
5	1.600000e-07	4.545455e-01	2.500000e+00	7.345271e-89
6	3.200000e-07	4.545455e-01	2.500000e+00	7.345271e-89
7	6.400000e-07	4.545455e-01	2.500000e+00	7.345271e-89
8	1.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
9	2.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
10	3.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
11	4.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
12	5.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
13	6.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
14	7.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
15	8.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
16	9.280000e-06	4.545455e-01	2.500000e+00	7.345271e-89
17	1.028000e-05	4.545455e-01	2.500000e+00	7.345271e-89
18	1.128000e-05	4.545455e-01	2.500000e+00	7.345271e-89
19	1.228000e-05	4.545455e-01	2.500000e+00	7.345271e-89
20	1.328000e-05	4.545455e-01	2.500000e+00	7.345271e-89
21	1.428000e-05	4.545455e-01	2.500000e+00	7.345271e-89
22	1.528000e-05	4.545455e-01	2.500000e+00	7.345271e-89
23	1.628000e-05	4.545455e-01	2.500000e+00	7.345271e-89
... (1040 more rows) ...

Common mistakes and how to avoid them

  • Swapping OpAmp inputs: Connecting the Reference to the Non-Inverting (+) input instead of the Inverting (-) input will reverse the logic (LED turns OFF when object is detected). Ensure V_SENSE goes to the Non-Inverting (+) pin for «Dark Detection».
  • Ambient Light interference: The LDR is very sensitive. If the room is bright, the «Dark» state might not be dark enough to trigger the threshold. Use a small tube or tape to shield the LDR.
  • Incorrect LDR placement: If the LDR (R4) is placed in the top leg of the voltage divider (swapped with R3), the logic is inverted. Ensure R4 connects to Ground (0).

Troubleshooting

  • Output LED never turns ON:
    • Check if the object actually blocks the light completely.
    • Measure V_SENSE. If it never exceeds 2.5 V, increase the value of R3 (e.g., to 22 kΩ) to raise the voltage sensitivity.
  • Output LED never turns OFF:
    • The LDR might be receiving insufficient light from the Emitter.
    • Check alignment of D1 and R4.
    • Measure V_REF. If R1 is disconnected, V_REF might be 0 V, causing the output to stay High.
  • Output flickers:
    • The light source might be unstable, or the voltage is hovering exactly at the threshold. Add a decoupling capacitor (e.g., 100 nF) across the power rails near the OpAmp.

Possible improvements and extensions

  1. Adjustable Sensitivity: Replace R1 or R2 with a 10 kΩ potentiometer. This allows you to fine-tune the V_REF threshold to work in different ambient light conditions.
  2. Hysteresis (Schmidt Trigger): Add a high-value feedback resistor (e.g., 1 MΩ) between the Output (V_OUT) and the Non-Inverting input (V_SENSE). This prevents the LED from flickering if the object moves slowly across the beam.

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 component used to detect light intensity in this circuit?




Question 2: What is the specific role of the operational amplifier in this project?




Question 3: According to the expected outcome, what defines 'State B'?




Question 4: What is the status of the Red LED output indicator when an object is detected?




Question 5: What is the approximate reference voltage threshold for the comparator to switch states?




Question 6: How does the LDR's resistance change when an opaque object blocks the light beam?




Question 7: Which of the following is a listed industrial use case for this sensor system?




Question 8: In 'State A' (Light path clear), what is the logic state of the output?




Question 9: What type of safety application is mentioned for this optical barrier?




Question 10: What triggers the intruder alarm function in this system?




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

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

Follow me:


Practical case: Simple light intensity meter

Simple light intensity meter prototype (Maker Style)

Level: Basic – Build a circuit where an LED dims as ambient light increases.

Objective and use case

You will construct a simple analog light sensor circuit using a photoresistor (LDR) in a configuration where the light output is inversely proportional to the ambient light intensity. This creates a «Dark Sensor» effect without using transistors.

Why it is useful:
* Automatic Lighting: Simulates street lamps or night lights that turn on automatically when it gets dark.
* Battery Efficiency: Ensures indicators are only active during low-light conditions when visibility is poor.
* Security Systems: Can detect if a sealed container or dark room has been breached by light.
* Concept Demonstration: Demonstrates current division and non-linear resistance components in parallel circuits.

Expected outcome:
* Dark condition: The LDR resistance is high, forcing current through the LED. The Red LED turns ON.
* Bright condition: The LDR resistance drops significantly, shunting current away from the LED. The Red LED turns OFF or dims significantly.
* Voltage shift: You will measure a voltage drop at the shared node as light increases.
* Target audience: Beginners and students familiar with basic breadboarding.

Materials

  • V1: 5 V DC supply, function: main power source
  • R1: 470 Ω resistor, function: current limiting and voltage divider upper leg
  • R2: LDR (GL5528 or similar), function: ambient light sensor (variable resistor)
  • D1: Red LED, function: low-light indicator

Wiring guide

We will use a «current shunt» topology. The LDR is placed in parallel with the LED.

  • VCC: Connect positive terminal of V1 to one side of R1.
  • VA: Connect the other side of R1 to the Anode (long leg) of D1.
  • VA: Connect one leg of R2 (LDR) to the same node (Anode of D1).
  • 0 (GND): Connect the Cathode (flat side/short leg) of D1 to the negative terminal of V1.
  • 0 (GND): Connect the remaining leg of R2 (LDR) to the negative terminal of V1.

Conceptual block diagram

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

Schematic

[ POWER SOURCE ]              [ CURRENT LIMITER ]               [ SHUNT TOPOLOGY ]

                                                              +--> [ D1: Red LED ] --> GND
                                                              |    (Output Indicator)
    [ V1: 5 V DC ] --(+)--> [ R1: 470 Ω ] --(Node VA)--> [ + ]
                                                              |
                                                              +--> [ R2: LDR ] --> GND
                                                                   (Light Sensor)
Schematic (ASCII)

Electrical diagram

Electrical diagram for case: Simple light intensity meter
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

Measurements and tests

To validate that the circuit behaves inversely to light intensity:

  1. Set up the multimeter: Select DC Voltage mode (20 V range).
  2. Connect probes: Place the Red probe on node VA (Anode of LED) and Black probe on 0 (GND).
  3. Test 1 (Ambient/Bright Light):
    • Expose the LDR to bright light.
    • Observation: The LED should be DIM or OFF.
    • Measurement: The voltage at VA should drop below the LED forward voltage (likely < 1.5 V). The low resistance of the LDR shunts the current to ground.
  4. Test 2 (Darkness):
    • Cover the LDR completely with your finger or a cap.
    • Observation: The LED should light up BRIGHTLY.
    • Measurement: The voltage at VA should rise to the LED’s forward voltage (approx. 1.8 V to 2.0 V for a red LED). The high resistance of the LDR forces current through the LED.

SPICE netlist and simulation

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

* Practical case: Simple light intensity meter

* --- Models ---
* Generic Red LED Model
* Parameters: IS=saturation current, N=emission coefficient, RS=series resistance
* BV=breakdown voltage, IBV=breakdown current, CJO=junction capacitance
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=20p)

* --- Power Supply ---
* V1: 5V DC supply (Main power source)
* Connected between VCC and GND (0)
V1 VCC 0 DC 5

* --- Circuit Components ---
* R1: 470 Ohm resistor
* Function: Current limiting and voltage divider upper leg
* Wiring: Connects Positive Terminal of V1 (VCC) to Node VA
R1 VCC VA 470

* D1: Red LED
* ... (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: Simple light intensity meter

* --- Models ---
* Generic Red LED Model
* Parameters: IS=saturation current, N=emission coefficient, RS=series resistance
* BV=breakdown voltage, IBV=breakdown current, CJO=junction capacitance
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=20p)

* --- Power Supply ---
* V1: 5V DC supply (Main power source)
* Connected between VCC and GND (0)
V1 VCC 0 DC 5

* --- Circuit Components ---
* R1: 470 Ohm resistor
* Function: Current limiting and voltage divider upper leg
* Wiring: Connects Positive Terminal of V1 (VCC) to Node VA
R1 VCC VA 470

* D1: Red LED
* Function: Low-light indicator
* Wiring: Anode to Node VA, Cathode to Negative Terminal of V1 (0)
D1 VA 0 DLED

* R2: LDR (GL5528 or similar)
* Function: Ambient light sensor (variable resistor)
* Wiring: Connects Node VA to Negative Terminal of V1 (0)
* Note: Modeled as a behavioral resistor where Resistance = V(V_LDR_CTRL).
* This allows simulating the change from Light (Low R) to Dark (High R).
R2 VA 0 R='V(V_LDR_CTRL)'

* --- Dynamic Stimuli (Simulation Only) ---
* V_LDR_SRC: Generates a voltage signal representing the LDR resistance in Ohms.
* Logic: 
*   - 100V (representing 100 Ohms) = Bright Light -> V(VA) drops -> LED OFF
*   - 10kV (representing 10k Ohms) = Dark -> V(VA) rises -> LED ON
* Timing: Fast pulse to demonstrate switching.
* PULSE(v1 v2 td tr tf pw per)
V_LDR_SRC V_LDR_CTRL 0 PULSE(100 10000 10u 100u 100u 500u 1000u)

* --- Analysis Directives ---
* Transient analysis: 5us step size, 2ms duration
.tran 5u 2ms

* Print specific nodes to verify operation
* V(VA): Voltage at the LED/LDR node (Should swing between ~0.8V and ~1.8V)
* V(V_LDR_CTRL): The resistance value being simulated
.print tran V(VA) V(V_LDR_CTRL)

.op
.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (441 rows)
Index   time            v(va)           v(v_ldr_ctrl)
0	0.000000e+00	8.771739e-01	1.000000e+02
1	5.000000e-08	8.771739e-01	1.000000e+02
2	1.000000e-07	8.771739e-01	1.000000e+02
3	2.000000e-07	8.771739e-01	1.000000e+02
4	4.000000e-07	8.771739e-01	1.000000e+02
5	8.000000e-07	8.771739e-01	1.000000e+02
6	1.600000e-06	8.771739e-01	1.000000e+02
7	3.200000e-06	8.771739e-01	1.000000e+02
8	6.400000e-06	8.771739e-01	1.000000e+02
9	1.000000e-05	8.771739e-01	1.000000e+02
10	1.016024e-05	9.861073e-01	1.158634e+02
11	1.048071e-05	1.182699e+00	1.475902e+02
12	1.112165e-05	1.342799e+00	2.110437e+02
13	1.175485e-05	1.386540e+00	2.737299e+02
14	1.276008e-05	1.418826e+00	3.732481e+02
15	1.399489e-05	1.436968e+00	4.954940e+02
16	1.646450e-05	1.455127e+00	7.399857e+02
17	2.140373e-05	1.468889e+00	1.228969e+03
18	2.640373e-05	1.474732e+00	1.723969e+03
19	3.140373e-05	1.478748e+00	2.218969e+03
20	3.640373e-05	1.480441e+00	2.713969e+03
21	4.140373e-05	1.481529e+00	3.208969e+03
22	4.640373e-05	1.482571e+00	3.703969e+03
23	5.140373e-05	1.483189e+00	4.198969e+03
... (417 more rows) ...

Common mistakes and how to avoid them

  1. Placing components in Series:
    • Mistake: Wiring Source -> Resistor -> LDR -> LED -> Ground.
    • Result: This creates a «Light Sensor» (brighter light = brighter LED), which is the opposite of the objective.
    • Solution: Ensure the LDR is in parallel with the LED (sharing the same start and end nodes).
  2. Using a resistor value that is too high for R1:
    • Mistake: Using a 10 kΩ resistor for R1.
    • Result: The LED never turns on brightly even in total darkness because the current is too restricted.
    • Solution: Use 330 Ω to 470 Ω for a 5 V source to ensure sufficient current for the LED when the LDR is high-resistance.
  3. Expecting a «Hard» On/Off switch:
    • Mistake: Expecting digital-like switching.
    • Result: The LED dims gradually rather than snapping off.
    • Solution: Understand that this is a passive analog circuit. For a hard «snap» action, a transistor or comparator would be required.

Troubleshooting

  • Symptom: LED is always ON, even in bright light.
    • Cause: R1 value is too low, or LDR has very high resistance even in light (or is disconnected).
    • Fix: Check LDR connections. If correct, increase R1 to 1 kΩ to make it easier for the LDR to pull the voltage down.
  • Symptom: LED is always OFF.
    • Cause: LED wired backwards or R1 is too high.
    • Fix: Flip the LED orientation. Ensure R1 is < 1 kΩ.
  • Symptom: Source gets hot.
    • Cause: Short circuit. Likely R1 was bypassed, connecting VCC directly to the LDR or LED.
    • Fix: Ensure R1 is strictly between VCC and the VA node.

Possible improvements and extensions

  1. Sensitivity Adjustment: Replace R1 with a 1 kΩ potentiometer to tune exactly how dark it needs to be before the LED turns on.
  2. Color Mixing: Put a Green LED in series with the LDR (instead of parallel). As light increases, the Green LED gets brighter while the Red LED (parallel) gets dimmer, creating a color-shifting light monitor.

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 ambient light sensor in this circuit?




Question 3: What happens to the Red LED in a 'Dark condition'?




Question 4: Why is this circuit useful for battery efficiency?




Question 5: What electrical concept does this circuit demonstrate?




Question 6: How does the circuit achieve the 'Dark Sensor' effect without transistors?




Question 7: What happens to the LDR resistance in a 'Bright condition'?




Question 8: Which of the following is a listed use case for this circuit?




Question 9: What happens to the voltage at the shared node as light increases?




Question 10: In the 'Dark condition', why does the LED turn on?




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

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

Follow me:


Practical case: Secret drawer alarm sensor

Secret drawer alarm sensor prototype (Maker Style)

Level: Basic — Build a circuit that activates a buzzer when it detects light upon opening a dark drawer.

Objective and use case

In this practical case, you will build a light-sensitive alarm system using a photoresistor (LDR) and a transistor driver. The circuit remains silent in the dark but activates an audible alarm immediately when light hits the sensor.

  • Security: Protects private drawers or boxes by alerting you if they are opened.
  • Safety: Can be used to signal if a refrigerator or pantry door is not fully closed.
  • Automation: Demonstrates how to use environmental inputs (light) to control output devices (sound).

Expected outcome:
* Darkness (Drawer closed): The buzzer remains OFF (0 V across the buzzer).
* Light (Drawer open): The buzzer turns ON immediately.
* Threshold: The transistor switches the load when the base voltage exceeds approximately 0.6 V–0.7 V.
* Target Audience: Beginners and hobbyists learning about sensor interfacing.

Materials

  • V1: 9 V DC battery or power supply, function: Main power source.
  • R1: Photoresistor (LDR) GL5528, function: Detects light intensity (variable resistance).
  • R2: 10 kΩ resistor, function: Pull-down resistor to form a voltage divider.
  • Q1: 2N2222 NPN Transistor, function: Electronic switch to drive the buzzer.
  • LS1: 9 V Active Piezo Buzzer, function: Audible alarm output.
  • SW1: SPST Toggle Switch, function: Master On/Off switch (optional).

Wiring guide

Construct the circuit connecting the components between the specific nodes defined below. Use a breadboard for easy assembly.

  • VCC: Connect the positive terminal of V1 and one side of SW1. Connect the other side of SW1 to the main VCC rail.
  • 0 (GND): Connect the negative terminal of V1, the Emitter of Q1, and one leg of R2.
  • V_BASE: Connect the other leg of R2, one leg of R1, and the Base of Q1.
  • VCC (Connection): Connect the other leg of R1 to the VCC rail.
  • V_COLLECTOR: Connect the Collector of Q1 to the negative wire of LS1.
  • VCC (Load): Connect the positive wire of LS1 to the VCC rail.

Conceptual block diagram

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

Schematic

[ INPUTS / POWER ]                  [ LOGIC / CONTROL ]                     [ OUTPUT ]

                                             (VCC Rail)
    [ 9 V Battery ] --> [ SW1 Switch ] --+------->+----------------------------------+
                                        |        |                                  |
                                        |        v                                  v
    [ Light Source ] --> [ LDR (R1) ] --+--> [ Voltage Divider ]                    |
                         (Sensor)            [ (Node: V_BASE)  ] --(Trigger)--> [ Q1 Transistor ]
                                        +--> [ R1 vs R2 Logic  ]                [ (NPN Switch)  ] --(Ground Path)--> [ LS1 Buzzer ]
                                        |                                       [ Collector Pin ]                    (Active Alarm)
    [ Resistor R2 ] ----(Pull-Down)-----+                                           |
    (10k Ohm)                                                                       v
                                                                                 [ GND ]
Schematic (ASCII)

Electrical diagram

Electrical diagram for case: Secret drawer alarm sensor
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

Measurements and tests

Follow these steps to validate the circuit operation:

  1. LDR Resistance Check:
    • Set your multimeter to measure Resistance (Ω).
    • Measure R1 in full light; it should read a low value (e.g., 500 Ω – 2 kΩ).
    • Cover R1 completely; it should read a high value (e.g., > 100 kΩ).
  2. Voltage Divider Test:
    • Power on the circuit (VCC = 9 V).
    • Set multimeter to DC Voltage. Connect the black probe to 0 (GND) and the red probe to V_BASE.
    • In Dark: The voltage should be close to 0 V (below 0.6 V).
    • In Light: The voltage should rise significantly (above 0.7 V).
  3. Output Verification:
    • Expose the sensor to light. The buzzer LS1 should sound.
    • Cover the sensor with your hand. The buzzer should stop immediately.

SPICE netlist and simulation

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

* Practical case: Secret drawer alarm sensor
* Ngspice Netlist
*
* Circuit Description:
* A light-activated alarm using a photoresistor (LDR) and an NPN transistor.
* When the drawer opens (Light), LDR resistance drops, Base voltage rises,
* Q1 turns ON, and the Buzzer sounds.
*
* Simulation Scenario:
* 0ms - 2ms: System OFF (Master Switch Open).
* 2ms: Master Switch closes (System Armed). Drawer is Closed (Dark).
* 5ms: Drawer Opens (Light hits LDR). Alarm triggers.

* --- Power Supply (V1) ---
* 9V DC Battery
V1 BAT_POS 0 DC 9

* --- Master Switch (SW1) ---
* Connects Battery Positive to Main VCC Rail.
* Modeled as a voltage-controlled switch closing at t=2ms.
* ... (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: Secret drawer alarm sensor
* Ngspice Netlist
*
* Circuit Description:
* A light-activated alarm using a photoresistor (LDR) and an NPN transistor.
* When the drawer opens (Light), LDR resistance drops, Base voltage rises,
* Q1 turns ON, and the Buzzer sounds.
*
* Simulation Scenario:
* 0ms - 2ms: System OFF (Master Switch Open).
* 2ms: Master Switch closes (System Armed). Drawer is Closed (Dark).
* 5ms: Drawer Opens (Light hits LDR). Alarm triggers.

* --- Power Supply (V1) ---
* 9V DC Battery
V1 BAT_POS 0 DC 9

* --- Master Switch (SW1) ---
* Connects Battery Positive to Main VCC Rail.
* Modeled as a voltage-controlled switch closing at t=2ms.
S1 BAT_POS VCC CTRL_SW 0 SW_MODEL
V_SW_CTRL CTRL_SW 0 PULSE(0 5 2ms 1u 1u 100ms)
.model SW_MODEL SW(Vt=2.5 Ron=0.01 Roff=100Meg)

* --- Photoresistor (R1 / LDR) ---
* LDR GL5528 connecting VCC to Base.
* Modeled as a behavioral resistor B_R1.
* Resistance logic controlled by V_LDR_RES:
*   Dark (Closed) = 1 MegOhm
*   Light (Open)  = 2 kOhm
* Simulation: Transitions from Dark to Light at t=5ms.
V_LDR_RES RES_CTRL 0 PWL(0 1Meg 4.99ms 1Meg 5ms 2k)
B_R1 VCC V_BASE I=(V(VCC) - V(V_BASE)) / V(RES_CTRL)

* --- Resistor (R2) ---
* 10k Ohm pull-down resistor from Base to Ground.
R2 V_BASE 0 10k

* --- Transistor (Q1) ---
* 2N2222 NPN Transistor acting as the switch for the buzzer.
* Connections: Collector=V_COLLECTOR, Base=V_BASE, Emitter=0
Q1 V_COLLECTOR V_BASE 0 2N2222MOD

* --- Buzzer (LS1) ---
* 9V Active Piezo Buzzer.
* Modeled as a 1k Ohm resistive load connected between VCC and Collector.
* (Not modeled as a voltage source per requirements).
R_LS1 VCC V_COLLECTOR 1k

* --- Component Models ---
.model 2N2222MOD NPN(Is=14.34f Xti=3 Eg=1.11 Vaf=74.03 Bf=255.9 Ne=1.307 Ise=14.34f Ikf=.2847 Xtb=1.5 Br=6.092 Nc=2 Isc=0 Ikr=0 Rc=1 Cjc=7.306p Mjc=.3416 Vjc=.75 Fc=.5 Cje=22.01p Mje=.377 Vje=.75 Tr=46.91n Tf=411.1p Itf=.6 Vtf=1.7 Xtf=3 Rb=10)

* --- Analysis Directives ---
.op
* Transient analysis for 10ms to capture the sequence.
.tran 10u 10ms

* Print directives to verify operation
* V(VCC): Power rail status
* V(V_BASE): Transistor drive voltage (Low=Dark, High=Light)
* V(V_COLLECTOR): Output node (High=Off, Low=Alarm On)
.print tran V(VCC) V(V_BASE) V(V_COLLECTOR)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (1057 rows)
Index   time            v(vcc)          v(v_base)       v(v_collector)
0	0.000000e+00	8.999090e-02	8.909999e-04	8.999089e-02
1	1.000000e-07	8.999090e-02	8.909999e-04	8.999089e-02
2	2.000000e-07	8.999090e-02	8.909999e-04	8.999089e-02
3	4.000000e-07	8.999090e-02	8.909999e-04	8.999089e-02
4	8.000000e-07	8.999090e-02	8.909999e-04	8.999089e-02
5	1.600000e-06	8.999090e-02	8.909999e-04	8.999089e-02
6	3.200000e-06	8.999090e-02	8.909999e-04	8.999089e-02
7	6.400000e-06	8.999090e-02	8.909999e-04	8.999089e-02
8	1.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
9	2.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
10	3.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
11	4.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
12	5.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
13	6.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
14	7.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
15	8.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
16	9.280000e-05	8.999090e-02	8.909999e-04	8.999089e-02
17	1.028000e-04	8.999090e-02	8.909999e-04	8.999089e-02
18	1.128000e-04	8.999090e-02	8.909999e-04	8.999089e-02
19	1.228000e-04	8.999090e-02	8.909999e-04	8.999089e-02
20	1.328000e-04	8.999090e-02	8.909999e-04	8.999089e-02
21	1.428000e-04	8.999090e-02	8.909999e-04	8.999089e-02
22	1.528000e-04	8.999090e-02	8.909999e-04	8.999089e-02
23	1.628000e-04	8.999090e-02	8.909999e-04	8.999089e-02
... (1033 more rows) ...

Common mistakes and how to avoid them

  1. Reversing the Voltage Divider: If you swap R1 (LDR) and R2 (Fixed Resistor), the alarm will sound in the dark and stop in the light (Inverse logic). Ensure R1 is connected to VCC and R2 to GND.
  2. Using a Passive Buzzer: A passive buzzer requires an oscillating AC signal to make sound. This circuit provides DC. You must use an Active Buzzer (which has an internal oscillator).
  3. Transistor Pinout Errors: Confusing the Collector (C) and Emitter (E) is common. For the 2N2222 in a TO-92 package, verify the pinout datasheet; usually, with the flat side facing you, the pins are E-B-C or E-B-C depending on the manufacturer.

Troubleshooting

  • Buzzer sounds continuously (even in dark):
    • Ambient light is too strong. Place the circuit in a box.
    • R2 value is too high. Try replacing R2 with a lower value (e.g., 4.7 kΩ) to pull the base voltage down harder.
  • Buzzer never sounds:
    • R2 value is too low.
    • LS1 is connected backwards (check polarity).
    • Q1 is damaged or connected incorrectly.
  • Buzzer is too quiet:
    • Battery voltage might be low.
    • Ensure the buzzer is rated for the supply voltage used (9 V).

Possible improvements and extensions

  1. Sensitivity Control: Replace the fixed resistor R2 with a 50 kΩ potentiometer. This allows you to fine-tune exactly how much light is needed to trigger the alarm.
  2. Latching Alarm: Add a Silicon Controlled Rectifier (SCR) instead of the NPN transistor, or add a feedback loop. This would keep the alarm sounding even if the thief quickly closes the drawer again, forcing a manual reset.

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 function of the circuit described in the text?




Question 2: Which component is used to detect light intensity in this project?




Question 3: What is the expected behavior of the buzzer when the drawer is closed (in darkness)?




Question 4: What is the role of the Q1 (2N2222 NPN Transistor) in the circuit?




Question 5: At approximately what base voltage does the transistor switch the load?




Question 6: What is the function of the R2 (10 kΩ resistor) in this specific voltage divider configuration?




Question 7: Which component serves as the main power source for the circuit?




Question 8: What is a practical safety use case mentioned for this circuit?




Question 9: How is the master On/Off switch (SW1) typically connected in this type of circuit?




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




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

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

Follow me:


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)

Electrical diagram

Electrical diagram for case: Automatic twilight switch
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

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:


Practical case: R-2R Resistor Network (Simple DAC)

R-2R Resistor Network (Simple DAC) prototype (Maker Style)

Level: Medium. Construct a resistive ladder to convert 4-bit binary signals into precise analog voltage levels.

Objective and use case

In this session, you will build a 4-bit Digital-to-Analog Converter (DAC) using an R-2R resistor ladder topology. This circuit sums binary weighted inputs to produce an analog output proportional to the digital value.

Why it is useful:
* Audio Synthesis: Used in simple function generators to create sine or triangle waves from digital microcontrollers.
* Video Signals: Historically used in VGA adapters to generate color intensity levels.
* Cost-Effective Control: Allows generating variable control voltages without dedicated DAC chips.
* Signal Processing Education: Demonstrates superposition and Thevenin’s theorem in a practical way.

Expected outcome:
* A stable output voltage (VOUT) that ranges from 0 V to approximately 4.68 V (given a 5 V supply).
* Sixteen distinct voltage steps (from binary 0000 to 1111).
* Linear relationship between the binary input value and the measured analog voltage.

Target audience and level: Electronics students and hobbyists familiar with basic circuit laws.

Materials

  • V1: 5 V DC supply, function: Logic high reference and main power.
  • R1: 10 kΩ resistor, function: Series resistor (R) in ladder spine (Bit 0-1).
  • R2: 10 kΩ resistor, function: Series resistor (R) in ladder spine (Bit 1-2).
  • R3: 10 kΩ resistor, function: Series resistor (R) in ladder spine (Bit 2-3).
  • R4: 20 kΩ resistor, function: Parallel resistor (2R) for Bit 0 (LSB).
  • R5: 20 kΩ resistor, function: Parallel resistor (2R) for Bit 1.
  • R6: 20 kΩ resistor, function: Parallel resistor (2R) for Bit 2.
  • R7: 20 kΩ resistor, function: Parallel resistor (2R) for Bit 3 (MSB).
  • R8: 20 kΩ resistor, function: Termination resistor (2R) connected to Ground.
  • SW1: SPDT switch (or jumper wire), function: Bit 0 input (LSB), switches between VCC and GND.
  • SW2: SPDT switch (or jumper wire), function: Bit 1 input, switches between VCC and GND.
  • SW3: SPDT switch (or jumper wire), function: Bit 2 input, switches between VCC and GND.
  • SW4: SPDT switch (or jumper wire), function: Bit 3 input (MSB), switches between VCC and GND.

Wiring guide

This guide uses node names: VCC (5 V), 0 (GND), B0 (Bit 0 Input), B1 (Bit 1 Input), B2 (Bit 2 Input), B3 (Bit 3 Input), and internal ladder nodes N0, N1, N2. VOUT is the analog output.

  • V1 Connection: Connect V1 positive terminal to VCC and negative to 0.
  • Input Switches (Digital Inputs):
    • SW1: Common to B0, Position 1 to 0, Position 2 to VCC.
    • SW2: Common to B1, Position 1 to 0, Position 2 to VCC.
    • SW3: Common to B2, Position 1 to 0, Position 2 to VCC.
    • SW4: Common to B3, Position 1 to 0, Position 2 to VCC.
  • Ladder «R» Resistors (Spine):
    • R1: Connects between node N0 and node N1.
    • R2: Connects between node N1 and node N2.
    • R3: Connects between node N2 and node VOUT.
  • Ladder «2R» Resistors (Branches):
    • R8 (Termination): Connects between node N0 and 0.
    • R4: Connects between node B0 and node N0.
    • R5: Connects between node B1 and node N1.
    • R6: Connects between node B2 and node N2.
    • R7: Connects between node B3 and node VOUT.
  • Output: Monitor voltage at node VOUT relative to 0.

Conceptual block diagram

Conceptual block diagram — LM358 R-2R Ladder DAC
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

[ DIGITAL INPUTS ]              [ R-2R LADDER NETWORK ]                 [ ANALOG OUTPUT ]
(Switches toggle VCC/GND)           (Voltage Summing Logic)

                                                                           +--> [ Multimeter ]
                                                                           |    (Measure V)
[ SW4: Bit 3 (MSB) ] --(High/Low)--> [ R7: 20k (2R) ] --(Bit 3 Weight)---->+--> [ VOUT Node  ]
                                                            ^
                                                            |
                                                     [ R3: 10k (R) ]
                                                            |
[ SW3: Bit 2       ] --(High/Low)--> [ R6: 20k (2R) ] --(Bit 2 Weight)---->+ (Node N2)
                                                            ^
                                                            |
                                                     [ R2: 10k (R) ]
                                                            |
[ SW2: Bit 1       ] --(High/Low)--> [ R5: 20k (2R) ] --(Bit 1 Weight)---->+ (Node N1)
                                                            ^
                                                            |
                                                     [ R1: 10k (R) ]
                                                            |
[ SW1: Bit 0 (LSB) ] --(High/Low)--> [ R4: 20k (2R) ] --(Bit 0 Weight)---->+ (Node N0)
                                                            |
                                                            v
                                                     [ R8: 20k (2R) ]
                                                            |
                                                           GND
Schematic (ASCII)

Electrical diagram

Electrical diagram for case: Practical case: R-2R Resistor Network (Simple DAC)
Generated from the validated SPICE netlist for this case.

🔒 This electrical diagram is premium. With the 7-day pass or the monthly membership you can unlock the complete didactic material and the print-ready PDF pack.🔓 See premium access plans

Measurements and tests

To validate the DAC, you will set the switches to specific binary codes and measure the resulting voltage at VOUT. The formula for the output is:
$VOUT = Vref × ((Decimal Value) / 16)$

  1. Zero Check: Set all switches (SW1-SW4) to 0 (GND). Measure VOUT. It should be exactly 0 V.
  2. LSB Check (Bit 0): Set SW1 to VCC and others to 0 (Binary 0001).
    • Calculation: $5 V × (1/16) = 0.3125 V$.
    • Verify VOUT is approx 0.31 V.
  3. MSB Check (Bit 3): Set SW4 to VCC and others to 0 (Binary 1000).
    • Calculation: $5 V × (8/16) = 2.5 V$.
    • Verify VOUT is approx 2.5 V.
  4. Full Scale Check: Set all switches to VCC (Binary 1111).
    • Calculation: $5 V × (15/16) = 4.6875 V. * Verify VOUT is approx 4.69 V. <! – – SPICE_INSERT_POINT – – > ## Common mistakes and how to avoid them 1. Floating Inputs: Leaving a switch open (disconnected) instead of connecting it to Ground for logic «0». * Solution: R – 2R ladders require inputs to be strictly atV_{ref}$ or $0 V$. Use SPDT switches or verify your jumper wires connect to GND when «off».
  5. Swapping R and 2R: Placing a 10 kΩ resistor where a 20 kΩ is required (or vice versa).
    • Solution: Double-check color codes. 10 kΩ is usually Brown-Black-Orange; 20 kΩ is Red-Black-Orange.
  6. Loading the Output: Connecting a low-impedance load (like a speaker or LED) directly to VOUT.
    • Solution: This circuit has a relatively high output impedance ($R$). Always use an Op-Amp buffer (voltage follower) if you need to drive a load.

Troubleshooting

  • Symptom: $V_{OUT}$ is 2.5 V when it should be 1.25 V.
    • Cause: The MSB (Bit 3) might be stuck high, or resistors are swapped.
    • Fix: Check switch continuity and verify resistor placement at node VOUT.
  • Symptom: Output voltages are non-linear or random.
    • Cause: Poor connection on the «spine» resistors (R1, R2, R3).
    • Fix: Re-seat the resistors on the breadboard to ensure the ladder chain is intact.
  • Symptom: Output never reaches near 4.6 V.
    • Cause: Resistor tolerance accumulation or low power supply voltage.
    • Fix: Measure V1 actual voltage. Use 1% tolerance metal film resistors for better precision.

Possible improvements and extensions

  1. 8-Bit Expansion: Add four more stages to the ladder (using more R and 2R resistors) to create an 8-bit DAC with 256 voltage steps.
  2. Active Buffering: Connect VOUT to an LM358 Op-Amp configured as a unity-gain buffer to drive an LED or a small audio speaker safely.

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 function of the circuit described in the text?




Question 2: Which resistor topology is used to build the DAC in this session?




Question 3: What is the expected maximum output voltage (V_OUT) given a 5 V supply?




Question 4: How many distinct voltage steps can a 4-bit DAC produce?




Question 5: Which of the following is a historical use case mentioned for this type of circuit?




Question 6: What relationship is expected between the binary input value and the measured analog voltage?




Question 7: What theoretical concepts does this project demonstrate practically?




Question 8: Why is the R-2R ladder considered a cost-effective control method?




Question 9: Which application involves creating sine or triangle waves from digital microcontrollers?




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




SPICE netlist and simulation

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

* Practical case: R-2R Resistor Network (Simple DAC)

* --- Power Supply ---
* V1: 5 V DC supply, function: Logic high reference and main power
V1 VCC 0 DC 5

* --- Digital Inputs (Simulated Switches) ---
* Modeled as PULSE voltage sources to strictly simulate user input/switching.
* Generates a binary counting sequence (0000 to 1111) to test the full truth table.
* Logic High = 5V (VCC), Logic Low = 0V (GND).

* SW1 (Bit 0 LSB): Toggles every 100us (Period)
VB0 B0 0 PULSE(0 5 0 1u 1u 50u 100u)

* SW2 (Bit 1): Toggles every 200us (Period)
VB1 B1 0 PULSE(0 5 0 1u 1u 100u 200u)

* SW3 (Bit 2): Toggles every 400us (Period)
VB2 B2 0 PULSE(0 5 0 1u 1u 200u 400u)

* ... (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: R-2R Resistor Network (Simple DAC)

* --- Power Supply ---
* V1: 5 V DC supply, function: Logic high reference and main power
V1 VCC 0 DC 5

* --- Digital Inputs (Simulated Switches) ---
* Modeled as PULSE voltage sources to strictly simulate user input/switching.
* Generates a binary counting sequence (0000 to 1111) to test the full truth table.
* Logic High = 5V (VCC), Logic Low = 0V (GND).

* SW1 (Bit 0 LSB): Toggles every 100us (Period)
VB0 B0 0 PULSE(0 5 0 1u 1u 50u 100u)

* SW2 (Bit 1): Toggles every 200us (Period)
VB1 B1 0 PULSE(0 5 0 1u 1u 100u 200u)

* SW3 (Bit 2): Toggles every 400us (Period)
VB2 B2 0 PULSE(0 5 0 1u 1u 200u 400u)

* SW4 (Bit 3 MSB): Toggles every 800us (Period)
VB3 B3 0 PULSE(0 5 0 1u 1u 400u 800u)

* --- R-2R Ladder Network ---

* -- Spine Resistors (R = 10k) --
* R1: Connects between node N0 and node N1
R1 N0 N1 10k

* R2: Connects between node N1 and node N2
R2 N1 N2 10k

* R3: Connects between node N2 and node VOUT
R3 N2 VOUT 10k

* -- Branch/Termination Resistors (2R = 20k) --
* R8 (Termination): Connects between node N0 and 0 (GND)
R8 N0 0 20k

* R4 (Bit 0 Input): Connects between node B0 and node N0
R4 B0 N0 20k

* R5 (Bit 1 Input): Connects between node B1 and node N1
R5 B1 N1 20k

* R6 (Bit 2 Input): Connects between node B2 and node N2
R6 B2 N2 20k

* R7 (Bit 3 Input - MSB): Connects between node B3 and node VOUT
R7 B3 VOUT 20k

* --- Simulation Directives ---
* Transient analysis to capture the full binary counting sequence (approx 1ms)
.tran 2u 1000u

* --- Output Printing ---
* Monitor the Input Bits and the Analog Output Voltage
.print tran V(B0) V(B1) V(B2) V(B3) V(VOUT)

.op
.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)
Show raw data table (1384 rows)
Index   time            v(b0)           v(b1)           v(b2)
0	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
1	1.000000e-08	5.000000e-02	5.000000e-02	5.000000e-02
2	2.000000e-08	1.000000e-01	1.000000e-01	1.000000e-01
3	4.000000e-08	2.000000e-01	2.000000e-01	2.000000e-01
4	8.000000e-08	4.000000e-01	4.000000e-01	4.000000e-01
5	1.600000e-07	8.000000e-01	8.000000e-01	8.000000e-01
6	3.200000e-07	1.600000e+00	1.600000e+00	1.600000e+00
7	6.400000e-07	3.200000e+00	3.200000e+00	3.200000e+00
8	1.000000e-06	5.000000e+00	5.000000e+00	5.000000e+00
9	1.064000e-06	5.000000e+00	5.000000e+00	5.000000e+00
10	1.192000e-06	5.000000e+00	5.000000e+00	5.000000e+00
11	1.448000e-06	5.000000e+00	5.000000e+00	5.000000e+00
12	1.960000e-06	5.000000e+00	5.000000e+00	5.000000e+00
13	2.984000e-06	5.000000e+00	5.000000e+00	5.000000e+00
14	4.984000e-06	5.000000e+00	5.000000e+00	5.000000e+00
15	6.984000e-06	5.000000e+00	5.000000e+00	5.000000e+00
16	8.984000e-06	5.000000e+00	5.000000e+00	5.000000e+00
17	1.098400e-05	5.000000e+00	5.000000e+00	5.000000e+00
18	1.298400e-05	5.000000e+00	5.000000e+00	5.000000e+00
19	1.498400e-05	5.000000e+00	5.000000e+00	5.000000e+00
20	1.698400e-05	5.000000e+00	5.000000e+00	5.000000e+00
21	1.898400e-05	5.000000e+00	5.000000e+00	5.000000e+00
22	2.098400e-05	5.000000e+00	5.000000e+00	5.000000e+00
23	2.298400e-05	5.000000e+00	5.000000e+00	5.000000e+00
... (1360 more rows) ...
Carlos Núñez Zorrilla
Carlos Núñez Zorrilla
Electronics & Computer Engineer

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

Follow me: