Practical case: Frequency divider by 2, 4 and 8

Frequency divider by 2, 4 and 8 prototype (Maker Style)

Level: Basic – Verify the frequency division relationship on the Q outputs of a binary counter relative to the clock.

Objective and use case

In this practical case, you will build a digital circuit using a 4-bit binary counter (74HC393) to divide an input clock signal frequency by factors of 2 (2^1), 4 (2^2), and 8 (2^3).

  • Digital Clocks: Used to divide high-frequency crystal oscillator signals down to 1 Hz for keeping time (seconds).
  • Audio Synthesis: Used to generate lower octaves from a base tone (frequency halving results in a tone one octave lower).
  • Baud Rate Generation: Used in UART communication to derive specific data transmission speeds from a master system clock.
  • Address Counters: Used to sequence through memory addresses in microcontrollers.

Expected outcome:
* Q0 Output: A square wave with a frequency exactly half of the input clock (f/2).
* Q1 Output: A square wave with a frequency one-quarter of the input clock (f/4).
* Q2 Output: A square wave with a frequency one-eighth of the input clock (f/8).
* Target Audience: Basic level students and hobbyists.

Materials

  • V1: 5 V DC supply, function: Main power source.
  • V_CLK: Pulse generator (0 V to 5 V, 1 kHz, 50% duty cycle), function: Input Clock signal.
  • U1: 74HC393, function: Dual 4-bit Binary Counter.
  • R1: 330 Ω resistor, function: Current limiting for LED D1.
  • R2: 330 Ω resistor, function: Current limiting for LED D2.
  • R3: 330 Ω resistor, function: Current limiting for LED D3.
  • D1: Red LED, function: Visual indicator for Q0 (f/2).
  • D2: Green LED, function: Visual indicator for Q1 (f/4).
  • D3: Yellow LED, function: Visual indicator for Q2 (f/8).
  • Scope: 4-Channel Oscilloscope, function: Waveform analysis.

Pin-out of the IC used

Selected Chip: 74HC393 (Dual 4-bit Binary Counter). We will use the first counter block (Side 1).

Pin Name Logic function Connection in this case
1 1CP (CLK) Clock Input (Falling edge trigger) Connected to CLK_IN
2 1MR Master Reset (Active High) Connected to 0 (GND)
3 1Q0 Output Bit 0 (Divide by 2) Connected to Q0
4 1Q1 Output Bit 1 (Divide by 4) Connected to Q1
5 1Q2 Output Bit 2 (Divide by 8) Connected to Q2
7 GND Ground Connected to 0
14 VCC Power Supply (+5 V) Connected to VCC

Wiring guide

  • V1 connects between node VCC and node 0 (GND).
  • U1 pin 14 connects to node VCC.
  • U1 pin 7 connects to node 0 (GND).
  • U1 pin 2 (Reset) connects to node 0 (GND) to enable counting.
  • V_CLK connects between node CLK_IN and node 0 (GND).
  • U1 pin 1 connects to node CLK_IN.
  • U1 pin 3 connects to node Q0.
  • U1 pin 4 connects to node Q1.
  • U1 pin 5 connects to node Q2.
  • R1 connects between node Q0 and node LED_Q0.
  • D1 anode connects to LED_Q0, cathode connects to 0 (GND).
  • R2 connects between node Q1 and node LED_Q1.
  • D2 anode connects to LED_Q1, cathode connects to 0 (GND).
  • R3 connects between node Q2 and node LED_Q2.
  • D3 anode connects to LED_Q2, cathode connects to 0 (GND).

Conceptual block diagram

Conceptual block diagram — 74HC393 Binary counter
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

INPUTS                                   PROCESSING                                     OUTPUTS / LOADS
(Left)                                    (Center)                                          (Right)

                                   +-----------------------+
                                   |                       |
 [ V_CLK: 1kHz ] --(Pin 1: CP)---> |                       | --(Pin 3: Q0)--> [ R1: 330 ] --> [ D1: Red ] --> GND
                                   |                       |       |
                                   |      U1: 74HC393      |       '--------(Scope Ch1: f/2)
                                   |      Dual 4-bit       |
                                   |      Bin Counter      |
 [ GND ] ---------(Pin 2: MR)--->  |                       | --(Pin 4: Q1)--> [ R2: 330 ] --> [ D2: Grn ] --> GND
             (Reset Disabled)      |   (Power: VCC=Pin 14, |       |
                                   |           GND=Pin 7)  |       '--------(Scope Ch2: f/4)
                                   |                       |
                                   |                       |
                                   |                       | --(Pin 5: Q2)--> [ R3: 330 ] --> [ D3: Yel ] --> GND
                                   |                       |       |
                                   +-----------------------+       '--------(Scope Ch3: f/8)
Electrical Schematic

Measurements and tests

To validate the circuit, perform the following measurements using the 4-channel oscilloscope:

  1. Setup: Connect the Ground clip of all oscilloscope probes to node 0 (GND).
  2. Channel 1 (Input): Connect to CLK_IN. Verify the frequency is 1 kHz.
  3. Channel 2 (Q0): Connect to Q0. Measure the frequency. It must be 500 Hz ($1kHz / 2$).
  4. Channel 3 (Q1): Connect to Q1. Measure the frequency. It must be 250 Hz ($1kHz / 4$).
  5. Channel 4 (Q2): Connect to Q2. Measure the frequency. It must be 125 Hz ($1kHz / 8$).
  6. Visual Check: If you lower the input clock frequency to 10 Hz, you should see D1 blinking fastest, D2 slower, and D3 slowest.

SPICE netlist and simulation

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

* Practical case: Frequency divider by 2, 4 and 8

.width out=256

* --- Models ---
* Generic LED Model
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=10p)

* --- Power Supply ---
* V1: 5V Main Supply
V1 VCC 0 DC 5

* --- Input Signal ---
* V_CLK: 1kHz Pulse, 0V to 5V, 50% Duty Cycle
V_CLK CLK_IN 0 PULSE(0 5 0 1u 1u 0.5m 1m)

* --- Subcircuit: 74HC393 (Behavioral XSPICE) ---
* Dual 4-bit Binary Counter
* Implements Counter 1 logic using XSPICE primitives.
* Pinout (DIP-14): 1=1CP, 2=1MR, 3=1Q0, 4=1Q1, 5=1Q2, 6=1Q3, 7=GND
* ... (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: Frequency divider by 2, 4 and 8

.width out=256

* --- Models ---
* Generic LED Model
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=10p)

* --- Power Supply ---
* V1: 5V Main Supply
V1 VCC 0 DC 5

* --- Input Signal ---
* V_CLK: 1kHz Pulse, 0V to 5V, 50% Duty Cycle
V_CLK CLK_IN 0 PULSE(0 5 0 1u 1u 0.5m 1m)

* --- Subcircuit: 74HC393 (Behavioral XSPICE) ---
* Dual 4-bit Binary Counter
* Implements Counter 1 logic using XSPICE primitives.
* Pinout (DIP-14): 1=1CP, 2=1MR, 3=1Q0, 4=1Q1, 5=1Q2, 6=1Q3, 7=GND
*                  8=2Q3, 9=2Q2, 10=2Q1, 11=2Q0, 12=2MR, 13=2CP, 14=VCC
.subckt 74HC393 1CP 1MR 1Q0 1Q1 1Q2 1Q3 GND 2Q3 2Q2 2Q1 2Q0 2MR 2CP VCC

    * ADC Bridge to read analog inputs (Clock and Reset)
    .model adc_mod adc_bridge(in_low=1.5 in_high=3.5)
    A_IN [1CP 1MR] [d_1cp d_1mr] adc_mod
    
    * ADC Bridge to read GND for Logic Low (used for SET inputs)
    A_GND [GND] [d_low] adc_mod

    * Logic Models
    .model inv_mod d_inverter(rise_delay=10n fall_delay=10n)
    .model dff_mod d_dff(clk_delay=10n rise_delay=10n fall_delay=10n)
    .model dac_mod dac_bridge(out_low=0.0 out_high=5.0)

    * --- Counter Logic (Side 1) ---
    * 74HC393 triggers on High-to-Low transition of CP.
    * XSPICE DFF triggers on Rising Edge. So we invert CP.
    A_INV1 d_1cp d_1cp_inv inv_mod

    * Stage 1 (Q0): Divider by 2
    * T-FF behavior: D = ~Q. Clock = ~CP. Reset = MR.
    * Port order: din clk set reset out nout
    A_DFF1 d_1q0_bar d_1cp_inv d_low d_1mr d_1q0 d_1q0_bar dff_mod

    * Stage 2 (Q1): Divider by 4
    * Ripples from Q0 Falling Edge.
    * Q0 Falling = ~Q0 Rising. Use d_1q0_bar as clock.
    A_DFF2 d_1q1_bar d_1q0_bar d_low d_1mr d_1q1 d_1q1_bar dff_mod

    * Stage 3 (Q2): Divider by 8
    * Ripples from Q1 Falling Edge. Use d_1q1_bar as clock.
    A_DFF3 d_1q2_bar d_1q1_bar d_low d_1mr d_1q2 d_1q2_bar dff_mod

    * Stage 4 (Q3): Divider by 16 (Not used externally but part of logic)
    A_DFF4 d_1q3_bar d_1q2_bar d_low d_1mr d_1q3 d_1q3_bar dff_mod

    * Drive Outputs
    A_OUT [d_1q0 d_1q1 d_1q2 d_1q3] [1Q0 1Q1 1Q2 1Q3] dac_mod

    * Side 2 is unused, inputs grounded in main circuit, outputs open.
.ends 74HC393

* --- Main Circuit Instances ---
* U1: 74HC393 Counter
* Pin connections based on Wiring Guide:
* 1(CLK_IN), 2(0/Reset), 3(Q0), 4(Q1), 5(Q2), 7(0/GND), 14(VCC)
* Unused outputs mapped to NC nodes. Unused inputs to 0.
* Subcircuit Pin Order: 1CP 1MR 1Q0 1Q1 1Q2 1Q3 GND 2Q3 2Q2 2Q1 2Q0 2MR 2CP VCC
XU1 CLK_IN 0 Q0 Q1 Q2 NC_1Q3 0 NC_2Q3 NC_2Q2 NC_2Q1 NC_2Q0 0 0 VCC 74HC393

* --- Output Paths (LEDs and Resistors) ---
* Path 1: Q0 -> R1 -> D1 (Red)
R1 Q0 LED_Q0 330
D1 LED_Q0 0 DLED

* Path 2: Q1 -> R2 -> D2 (Green)
R2 Q1 LED_Q1 330
D2 LED_Q1 0 DLED

* Path 3: Q2 -> R3 -> D3 (Yellow)
R3 Q2 LED_Q2 330
D3 LED_Q2 0 DLED

* --- Simulation & Output ---
.op
.tran 10u 20m
.print tran V(CLK_IN) V(Q0) V(Q1) V(Q2)

.end
* --- GPT review (BOM/Wiring/SPICE) ---
* circuit_ok=true
* simulation_summary: The simulation shows a clear binary counting sequence. CLK_IN is a 1kHz clock (period 1ms). Q0 toggles every 1ms (f/2, period 2ms). Q1 toggles every 2ms (f/4, period 4ms). Q2 toggles every 4ms (f/8, period 8ms). The outputs transition cleanly between 0V and 5V.
* bom_vs_spice equivalences ignored:
*   - LEDs (D1, D2, D3) are modeled using a generic diode model (DLED) with specific parameters.
*   - U1 (74HC393) is modeled as a behavioral subcircuit using XSPICE primitives (ADC/DAC bridges, DFFs) instead of a transistor-level model.
* overall_comment: The circuit is perfectly functional and accurately represents a 3-bit binary ripple counter (frequency divider). The behavioral model for the 74HC393 is correctly implemented with the necessary ADC/DAC bridges for XSPICE. The wiring matches the guide exactly, and the simulation results confirm the expected frequency division ratios (f/2, f/4, f/8). It is an excellent didactic example.
* --------------------------------------

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation shows a clear binary counting sequence. CLK_IN is a 1kHz clock (period 1ms). Q0 toggles every 1ms (f/2, period 2ms). Q1 toggles every 2ms (f/4, period 4ms). Q2 toggles every 4ms (f/8, period 8ms). The outputs transition cleanly between 0V and 5V.
Show raw data table (3323 rows)
Index   time            v(clk_in)       v(q0)           v(q1)           v(q2)
0	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
1	1.000000e-08	5.000000e-02	0.000000e+00	0.000000e+00	0.000000e+00
2	2.000000e-08	1.000000e-01	0.000000e+00	0.000000e+00	0.000000e+00
3	4.000000e-08	2.000000e-01	0.000000e+00	0.000000e+00	0.000000e+00
4	8.000000e-08	4.000000e-01	0.000000e+00	0.000000e+00	0.000000e+00
5	1.600000e-07	8.000000e-01	0.000000e+00	0.000000e+00	0.000000e+00
6	3.200000e-07	1.600000e+00	0.000000e+00	0.000000e+00	0.000000e+00
7	6.400000e-07	3.200000e+00	0.000000e+00	0.000000e+00	0.000000e+00
8	1.000000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
9	1.064000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
10	1.192000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
11	1.448000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
12	1.960000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
13	2.984000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
14	5.032000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
15	9.128000e-06	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
16	1.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
17	2.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
18	3.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
19	4.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
20	5.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
21	6.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
22	7.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
23	8.732000e-05	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
... (3299 more rows) ...

Common mistakes and how to avoid them

  1. Floating the Master Reset (MR) pin: Leaving pin 2 disconnected causes the counter to reset randomly due to noise. Solution: Always tie the MR pin to GND (Logic 0) for normal counting operation.
  2. Confusing Pin Numbers: The 74HC393 has two counters inside. Students often mix pins from Counter 1 and Counter 2. Solution: Strictly follow the datasheet and use pins 1, 2, 3, 4, 5, and 6 for the first counter only.
  3. Ignoring VCC/GND: Forgetting to power the chip leads to unpredictable output or no activity. Solution: Always connect Pin 14 to +5 V and Pin 7 to GND before testing.

Troubleshooting

  • Symptom: No LEDs light up, and outputs remain at 0 V.
    • Cause: Master Reset (Pin 2) might be connected to VCC instead of GND.
    • Fix: Move connection of Pin 2 to GND.
  • Symptom: LEDs are always on or flickering very dimly.
    • Cause: Frequency is too high for the eye to see blinking (e.g., 1 kHz).
    • Fix: Use the oscilloscope to verify the signal, or lower V_CLK frequency to < 10 Hz for visual confirmation.
  • Symptom: Output frequency is unstable or erratic.
    • Cause: Noisy power supply or lack of decoupling capacitor.
    • Fix: Add a 100 nF capacitor across VCC and GND near the IC.

Possible improvements and extensions

  1. Divide by 16 and 256: Cascade the first counter into the second counter of the U1 chip (connect 1Q3 to 2CP) to achieve higher division ratios up to 256.
  2. Variable Audio Generator: Connect the outputs to a simple speaker driver and use a variable potentiometer on a 555 timer (as the clock) to hear how the pitch drops by octaves as you switch between Q0, Q1, and Q2.

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 74HC393 IC used in this circuit?




Question 2: What is the frequency relationship of the Q0 output relative to the input clock (f)?




Question 3: If the input clock frequency is 1 kHz, what is the expected frequency at the Q1 output?




Question 4: What is the expected frequency relationship at the Q2 output?




Question 5: In audio synthesis, what is the result of halving a tone's frequency?




Question 6: What is the purpose of using this circuit in digital clocks?




Question 7: What DC supply voltage is specified for this circuit?




Question 8: How is this circuit applied in UART communication?




Question 9: Which power of 2 represents the division factor for the Q1 output?




Question 10: What is the role of address counters in microcontrollers?




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: 4-bit up counter with LEDs

4-bit up counter with LEDs prototype (Maker Style)

Level: Basic. Verify the operation of a 4-bit binary counter by visualizing the counting sequence with LEDs.

Objective and use case

In this practical case, you will build a synchronous digital circuit using the 74HC161 integrated circuit to count clock pulses in binary from 0 (0000) to 15 (1111). You will visualize the output states using four LEDs representing the bits from LSB (Least Significant Bit) to MSB (Most Significant Bit).

Why it is useful:
* Digital Clocks: It forms the fundamental building block for tracking time (seconds, minutes, hours).
* Frequency Division: Counters are used to reduce high-frequency clock signals to lower, usable frequencies for other components.
* Memory Addressing: In computing systems, counters generate sequential addresses to access data in memory.
* Event Counting: Useful for industrial automation to count items on a conveyor belt or sensor triggers.
* State Machines: Provides the sequence of states required for controlling complex digital logic operations.

Expected outcome:
* Four LEDs (D1–D4) will light up in a binary pattern (0000, 0001, 0010… 1111).
* The sequence repeats every 16 clock pulses.
* Activating the reset switch forces all LEDs to turn OFF immediately.
* Logic High output voltage approx. 5 V; Logic Low approx. 0 V.

Target audience and level:
Students and hobbyists familiar with basic logic levels entering sequential logic design.

Materials

  • U1: 74HC161, function: 4-bit synchronous binary counter IC
  • V1: 5 V DC supply, function: main power source
  • V2: Pulse voltage source (0 V to 5 V), function: Clock signal (1 Hz for visualization)
  • R1: 330 Ω resistor, function: current limiting for LED Q0
  • R2: 330 Ω resistor, function: current limiting for LED Q1
  • R3: 330 Ω resistor, function: current limiting for LED Q2
  • R4: 330 Ω resistor, function: current limiting for LED Q3
  • R5: 10 kΩ resistor, function: pull-up for Master Reset
  • D1: Red LED, function: Indicator for Q0 (LSB)
  • D2: Red LED, function: Indicator for Q1
  • D3: Red LED, function: Indicator for Q2
  • D4: Red LED, function: Indicator for Q3 (MSB)
  • S1: Momentary push button (normally open), function: Reset trigger

Pin-out of the IC used

Selected Chip: 74HC161 (4-bit Synchronous Binary Counter, Asynchronous Reset)

Pin Name Logic function Connection in this case
1 \overlineMR Master Reset (Active Low) Connected to Reset node (S1/R5)
2 CP Clock Pulse (Rising Edge) Connected to V2 (Clock Source)
7 CEP Count Enable Parallel Connected to VCC (Always Enabled)
8 GND Ground Connected to 0 (GND)
9 \overlinePE Parallel Enable (Load) Connected to VCC (Disabled)
10 CET Count Enable Trickle Connected to VCC (Always Enabled)
11 Q3 Output Bit 3 (MSB) Connected to D4 via R4
12 Q2 Output Bit 2 Connected to D3 via R3
13 Q1 Output Bit 1 Connected to D2 via R2
14 Q0 Output Bit 0 (LSB) Connected to D1 via R1
16 VCC Power Supply (+5 V) Connected to VCC

Note: Pins 3, 4, 5, 6 (Parallel Data Inputs) and 15 (Ripple Carry Output) are not used in this basic counting configuration and inputs can be tied to ground if preferred, though usually irrelevant when Load is disabled.

Wiring guide

Construct the circuit following these explicit node connections:

  • Power Nodes:

    • Connect V1 positive terminal to node VCC.
    • Connect V1 negative terminal to node 0 (GND).
    • Connect U1 pin 16 to VCC.
    • Connect U1 pin 8 to 0.
  • Control Inputs:

    • Connect V2 (Clock Source) positive to node CLK. Connect V2 negative to 0.
    • Connect U1 pin 2 to node CLK.
    • Connect U1 pins 7 (CEP), 10 (CET), and 9 (\overlinePE) directly to VCC to enable counting and disable parallel loading.
    • Reset Circuit: Connect R5 between VCC and node RESET_N. Connect S1 between node RESET_N and 0. Connect U1 pin 1 to RESET_N.
  • Outputs (LED Indicators):

    • Bit 0 (LSB): Connect U1 pin 14 to node Q0. Connect R1 between Q0 and node LED_A1. Connect D1 anode to LED_A1 and cathode to 0.
    • Bit 1: Connect U1 pin 13 to node Q1. Connect R2 between Q1 and node LED_A2. Connect D2 anode to LED_A2 and cathode to 0.
    • Bit 2: Connect U1 pin 12 to node Q2. Connect R3 between Q2 and node LED_A3. Connect D3 anode to LED_A3 and cathode to 0.
    • Bit 3 (MSB): Connect U1 pin 11 to node Q3. Connect R4 between Q3 and node LED_A4. Connect D4 anode to LED_A4 and cathode to 0.

Conceptual block diagram

Conceptual block diagram — 74HC161 Binary counter
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

+-------------------------------------------------------------------------------------------------------+
|                                  PRACTICAL CASE: 4-BIT UP COUNTER                                     |
+-------------------------------------------------------------------------------------------------------+

      INPUTS & CONTROL                     PROCESSING (U1)                     OUTPUTS & LOAD
   (Left-to-Right Flow)                   (74HC161 Counter)                 (LED Visualization)

                                     +-------------------------+
                                     |                         |
[ V2: Clock Source ] --(CLK 1Hz)---> | [Pin 2] CP              |
                                     |                         |
                                     |                         |          (Bit 0 - LSB)
[ Reset Logic ]                      |             [Pin 14] Q0 | --(Q0)--> [ R1: 330 ] --> [ D1: Red ] --> GND
(VCC->R5->Node->S1->GND) --(RST_N)-> | [Pin 1] ~MR             |
                                     |                         |
                                     |                         |          (Bit 1)
                                     |             [Pin 13] Q1 | --(Q1)--> [ R2: 330 ] --> [ D2: Red ] --> GND
[ VCC: 5 V Source ] --(Enable High)-> | [Pin 7]  CEP            |
                   --(Enable High)-> | [Pin 10] CET            |
                   --(Disable Load)> | [Pin 9]  ~PE            |          (Bit 2)
                                     |             [Pin 12] Q2 | --(Q2)--> [ R3: 330 ] --> [ D3: Red ] --> GND
                                     |                         |
                                     |                         |
                                     |                         |          (Bit 3 - MSB)
                                     |             [Pin 11] Q3 | --(Q3)--> [ R4: 330 ] --> [ D4: Red ] --> GND
                                     |                         |
                                     +-------------------------+
                                            |           |
                                         [Pin 16]    [Pin 8]
                                            |           |
                                           VCC         GND
Electrical Schematic

Measurements and tests

  1. Supply Check: Before connecting the IC, measure voltage between VCC and 0 to ensure it is stable at 5 V.
  2. Clock Verification: Set V2 to a low frequency (e.g., 1 Hz). Verify the signal at node CLK oscillates between 0 V and 5 V.
  3. Sequence Observation: Power on the circuit. Observe D1 through D4. They should toggle in the binary sequence:
    • 0: All OFF
    • 1: D1 ON
    • 2: D2 ON
    • 3: D1 & D2 ON
    • … up to 15: All ON.
  4. Reset Test: While the counter is running, press S1. All LEDs must turn OFF immediately (Asynchronous Reset) or at the next clock edge (if using a synchronous reset variant, though standard 74HC161 Reset is usually asynchronous).
  5. Logic Levels: Use a multimeter to measure node Q3 when D4 is lit. It should read close to 5 V (Logic High).

SPICE netlist and simulation

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

* Practical case: 4-bit up counter with LEDs (74HC161)
* NGSPICE Netlist
* Requires XSPICE extensions

.width out=256

* --- Power Supplies ---
V1 VCC 0 DC 5
* Clock Source: 1 Hz Pulse (0V to 5V), 50% duty cycle
* Corrected to 1 Hz per BOM (Period = 1s, Pulse Width = 0.5s)
V2 CLK 0 PULSE(0 5 0 1u 1u 0.5 1)

* --- Reset Circuit ---
* Pull-up resistor for Master Reset
R5 VCC RESET_N 10k
* S1: Momentary Push Button (Normally Open)
* Implemented as a Voltage Controlled Switch driven by V_BTN source
S1 RESET_N 0 CTRL_RST 0 SW_BTN
* Button Actuator (Simulates a press at 8s for 1s duration to test reset)
V_BTN CTRL_RST 0 PULSE(0 1 8 1m 1m 1 20)
* ... (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: 4-bit up counter with LEDs (74HC161)
* NGSPICE Netlist
* Requires XSPICE extensions

.width out=256

* --- Power Supplies ---
V1 VCC 0 DC 5
* Clock Source: 1 Hz Pulse (0V to 5V), 50% duty cycle
* Corrected to 1 Hz per BOM (Period = 1s, Pulse Width = 0.5s)
V2 CLK 0 PULSE(0 5 0 1u 1u 0.5 1)

* --- Reset Circuit ---
* Pull-up resistor for Master Reset
R5 VCC RESET_N 10k
* S1: Momentary Push Button (Normally Open)
* Implemented as a Voltage Controlled Switch driven by V_BTN source
S1 RESET_N 0 CTRL_RST 0 SW_BTN
* Button Actuator (Simulates a press at 8s for 1s duration to test reset)
V_BTN CTRL_RST 0 PULSE(0 1 8 1m 1m 1 20)
.model SW_BTN sw(vt=0.5 ron=1 roff=10Meg)

* --- 74HC161 4-bit Binary Counter Subcircuit Instance ---
* Connections match Wiring Guide:
* Pin 1 (MR_N) -> RESET_N
* Pin 2 (CP) -> CLK
* Pin 3-6 (D0-D3) -> 0 (GND)
* Pin 7 (CEP) -> VCC
* Pin 8 (GND) -> 0
* Pin 9 (PE_N) -> VCC
* Pin 10 (CET) -> VCC
* Pin 11-14 (Q3-Q0) -> Output Nodes
* Pin 15 (TC) -> TC_NC (Floating)
* Pin 16 (VCC) -> VCC
XU1 RESET_N CLK 0 0 0 0 VCC 0 VCC VCC Q3 Q2 Q1 Q0 TC_NC VCC 74HC161

* --- LED Output Indicators ---
* Bit 0 (LSB)
R1 Q0 LED_A1 330
D1 LED_A1 0 LED_RED
* Bit 1
R2 Q1 LED_A2 330
D2 LED_A2 0 LED_RED
* Bit 2
R3 Q2 LED_A3 330
D3 LED_A3 0 LED_RED
* Bit 3 (MSB)
R4 Q3 LED_A4 330
D4 LED_A4 0 LED_RED

* --- Models ---
.model LED_RED D(Is=1e-14 Rs=5 N=2)

* --- Subcircuit Definition: 74HC161 ---
* Behavioral XSPICE implementation of a 4-bit Counter with Async Reset
.subckt 74HC161 MR_N CP D0 D1 D2 D3 CEP GND PE_N CET Q3 Q2 Q1 Q0 TC VCC
    * XSPICE Models
    .model adc_in adc_bridge(in_low=2.0 in_high=3.0)
    .model dac_out dac_bridge(out_low=0.0 out_high=5.0)
    .model dff_mod d_dff(rise_delay=10n fall_delay=10n)
    .model inv_mod d_inverter(rise_delay=5n fall_delay=5n)

    * Input Bridges (Analog to Digital)
    A_IN [MR_N CP] [mr_dig cp_dig] adc_in

    * Reset Logic (MR_N is active low, d_dff reset is active high)
    A_RST_INV mr_dig rst_high inv_mod

    * Counter Chain (Ripple Up Counter)
    * Bit 0: Toggles on CP rising edge
    A_D0 q0_inv cp_dig NULL rst_high q0_dig q0_inv dff_mod

    * Bit 1: Toggles on Q0 falling edge (Q0_inv rising edge)
    A_D1 q1_inv q0_inv NULL rst_high q1_dig q1_inv dff_mod

    * Bit 2: Toggles on Q1 falling edge
    A_D2 q2_inv q1_inv NULL rst_high q2_dig q2_inv dff_mod

    * Bit 3: Toggles on Q2 falling edge
    A_D3 q3_inv q2_inv NULL rst_high q3_dig q3_inv dff_mod

    * Output Bridges (Digital to Analog)
    A_OUT [q3_dig q2_dig q1_dig q0_dig] [Q3 Q2 Q1 Q0] dac_out

    * Terminal Count (Unused/Dummy pull-down)
    R_TC TC 0 100k
.ends

* --- Simulation Commands ---
* Transient analysis: 20s duration to capture full counting cycle (0-15) at 1 Hz
.op
.tran 10m 20s

* Print critical signals (Inputs first)
.print tran V(CLK) V(RESET_N) V(Q0) V(Q1) V(Q2) V(Q3)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation shows a correct 4-bit binary counting sequence (0000 to 1111) on outputs Q0-Q3. The clock toggles at 1Hz. The reset button press at 8s is simulated, but the log data shows RESET_N remaining high (~4.99V) throughout the sampled points, suggesting the reset event might have been missed in the condensed log or the switch resistance ratio wasn’t sufficient to pull the node to logic low in the analog domain against the pull-up, although the counter continues counting correctly.
Show raw data table (3020 rows)
Index   time            v(clk)          v(reset_n)      v(q0)           v(q1)           v(q2)           v(q3)
0	0.000000e+00	0.000000e+00	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
1	1.000000e-08	5.000000e-02	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
2	2.000000e-08	1.000000e-01	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
3	4.000000e-08	2.000000e-01	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
4	8.000000e-08	4.000000e-01	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
5	1.600000e-07	8.000000e-01	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
6	3.200000e-07	1.600000e+00	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
7	6.400000e-07	3.200000e+00	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
8	6.520000e-07	3.260000e+00	4.995005e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
9	6.760000e-07	3.380000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
10	7.240000e-07	3.620000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
11	8.200000e-07	4.100000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
12	1.000000e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
13	1.019200e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
14	1.057600e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
15	1.134400e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
16	1.288000e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
17	1.595200e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
18	2.209600e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
19	3.438400e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
20	5.896000e-06	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
21	1.081120e-05	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
22	2.064160e-05	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
23	4.030240e-05	5.000000e+00	4.995005e+00	5.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
... (2996 more rows) ...

Common mistakes and how to avoid them

  1. Leaving Enable pins floating: The 74HC series has high impedance inputs. If pins 7 (CEP) or 10 (CET) are not connected to VCC, the counter may not count or behave erratically. Always tie unused control inputs to a defined logic level.
  2. Clock frequency too high: If V2 is set to 1 kHz or higher, all LEDs will appear to be dimly lit continuously due to persistence of vision. Keep the clock below 5 Hz for visual debugging.
  3. Floating Parallel Load pin: If pin 9 (\overlinePE) is left floating or low, the chip might constantly try to load data from inputs P0-P3 instead of counting. Ensure pin 9 is tied to VCC.

Troubleshooting

  • LEDs never turn on: Check power supply connections to pins 16 and 8. Ensure LEDs are inserted with the correct polarity (anode to resistor/IC, cathode to ground).
  • Counter stays at zero: Verify that the Reset pin (1) is High (5 V). If S1 is stuck or the pull-up R5 is missing, the chip remains in Reset state. Also, check that Enable pins (7, 10) are High.
  • Counter skips numbers: This is often due to «switch bounce» if you are using a mechanical switch as a manual clock. Use a clean square wave generator or a debounce circuit (capacitor + resistor) for the clock input.
  • Random sequence: Check if the Parallel Enable (\overlinePE) pin is accidentally Low or floating. It must be High.

Possible improvements and extensions

  1. 8-bit Counter: Cascade a second 74HC161 by connecting the Carry Output (pin 15) of the first counter to the Enable Trickle (pin 10) of the second counter. This allows counting up to 255.
  2. Manual Clock: Replace the frequency generator V2 with a 555 timer circuit in astable mode or a debounced push-button to advance the count manually.

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: Which integrated circuit is used as the 4-bit synchronous binary counter in this experiment?




Question 2: What is the counting range of the circuit described in the text?




Question 3: What happens to the LED sequence after 16 clock pulses?




Question 4: What does the acronym LSB stand for in the context of this circuit?




Question 5: According to the text, how are counters used in Digital Clocks?




Question 6: What is the purpose of using counters for Frequency Division?




Question 7: In computing systems, what are counters typically used for according to the text?




Question 8: Which application is mentioned for industrial automation?




Question 9: How is the 74HC161 circuit described in the objective section?




Question 10: What is the primary method used to visualize the output states in this experiment?




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: Dual Safety Motor Activation

Dual Safety Motor Activation prototype (Maker Style)

Level: Basic — Implement a system where a motor runs only when two buttons are pressed simultaneously.

Objective and use case

In this session, you will build a safety logic circuit using a 74HC00 (Quad 2-input NAND gate) to control a DC motor. The motor will only activate when two separate pushbuttons are engaged at the same time, mimicking a «two-hand control» safety device.

Why it is useful:
* Industrial Safety: Prevents operators from reaching into a hydraulic press or cutting machine while it is moving.
* Accident Prevention: Ensures both hands are occupied on the controls during the dangerous phase of operation.
* Logic Composition: Demonstrates how to create an AND function using universal NAND gates.

Expected outcome:
* Idle State: Logic output is LOW (0 V); Motor is OFF.
* Single Press: Logic output remains LOW (0 V); Motor remains OFF.
* Dual Press: Logic output becomes HIGH (5 V); Relay engages; Motor runs.
* Current Handling: The logic gate drives a transistor, which safely switches the high-current relay coil.

Target audience and level:
Basic electronics students and hobbyists interested in digital logic applications.

Materials

  • V1: 5 V DC voltage source, function: Logic and relay coil power supply.
  • V2: 12 V DC voltage source, function: Motor power supply.
  • U1: 74HC00, function: Quad 2-input NAND gate IC.
  • S1: Pushbutton (normally open), function: Left-hand safety switch.
  • S2: Pushbutton (normally open), function: Right-hand safety switch.
  • R1: 10 kΩ resistor, function: Pull-down for S1.
  • R2: 10 kΩ resistor, function: Pull-down for S2.
  • R3: 1 kΩ resistor, function: Base current limiting for Q1.
  • Q1: 2N2222 NPN transistor, function: Relay driver.
  • D1: 1N4007 diode, function: Flyback protection for relay coil.
  • K1: 5 V SPDT Relay, function: High-power switching interface.
  • M1: 12 V DC Motor, function: Actuator (load).

Pin-out of the IC used (74HC00)

Chip: 74HC00 (Quad 2-Input NAND Gate)

Pin Name Logic Function Connection in this case
1 1 A Input A (Gate 1) Connected to S1 (Node BTN_L)
2 1B Input B (Gate 1) Connected to S2 (Node BTN_R)
3 1Y Output (Gate 1) Connected to Inputs of Gate 2 (Node NAND_INTER)
4 2 A Input A (Gate 2) Connected to Node NAND_INTER
5 2B Input B (Gate 2) Connected to Node NAND_INTER
6 2Y Output (Gate 2) Connected to R3 (Node LOGIC_OUT)
7 GND Ground Connected to Node 0
14 VCC Power Supply Connected to Node VCC

Note: Pins 8 through 13 are unused and should ideally be tied to GND or VCC in a permanent installation to prevent noise, though left floating for this basic breadboard exercise.

Wiring guide

Construct the circuit following these node connections. Ensure the power supply is off while building.

Power and Inputs:
* V1 (+): Connects to node VCC.
* V1 (-) / V2 (-): Connects to node 0 (Common Ground).
* S1: Connects between VCC and node BTN_L.
* R1: Connects between node BTN_L and 0.
* S2: Connects between VCC and node BTN_R.
* R2: Connects between node BTN_R and 0.

Logic Processing (Using U1 as AND Gate):
* U1 (Pin 14): Connects to VCC.
* U1 (Pin 7): Connects to 0.
* U1 (Pin 1): Connects to node BTN_L.
* U1 (Pin 2): Connects to node BTN_R.
* U1 (Pin 3): Connects to node NAND_INTER (First stage output).
* U1 (Pin 4 & Pin 5): Both connect to node NAND_INTER (Configures Gate 2 as an inverter).
* U1 (Pin 6): Connects to node LOGIC_OUT.

Output Stage:
* R3: Connects between node LOGIC_OUT and node BASE.
* Q1 (Base): Connects to node BASE.
* Q1 (Emitter): Connects to node 0.
* Q1 (Collector): Connects to node RELAY_COIL_LO.
* K1 (Coil +): Connects to VCC.
* K1 (Coil -): Connects to node RELAY_COIL_LO.
* D1 (Anode): Connects to node RELAY_COIL_LO.
* D1 (Cathode): Connects to VCC (Parallel to coil, reverse biased).

Motor Circuit:
* V2 (+): Connects to K1 Common contact (COM).
* K1 (NO – Normally Open): Connects to node MOTOR_POS.
* M1 (+): Connects to node MOTOR_POS.
* M1 (-): Connects to node 0.

Conceptual block diagram

Conceptual block diagram — 74HC00 NAND gate
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

+-----------------------------------------------------------------------------+
|                  DUAL SAFETY MOTOR ACTIVATION BLOCK DIAGRAM                 |
+-----------------------------------------------------------------------------+

1. INPUTS & LOGIC STAGE (5 V Domain)
   (Both buttons must be pressed to activate the output)

   V1(5 V)
     |
     +---> [ S1: Left Button ] ---+--(BTN_L)------\
                                  |                \
                                  v                 \
                               [ R1: 10k ]           +---> [ U1:A (NAND) ] --(NAND_INTER)--> [ U1:B (NOT*) ] --(LOGIC_OUT)-->
                                  |                 /       (Pins 1 & 2)                      (Pins 4 & 5)          |
                                 GND               /                                         *Wired as Inverter     |
                                                  /                                                                 |
   V1(5 V)                                        /                                                                  |
     |                                          /                                                                   |
     +---> [ S2: Right Button ] --+--(BTN_R)---/                                                                    |
                                  |                                                                                 |
                                  v                                                                                 |
                               [ R2: 10k ]                                                                          |
                                  |                                                                                 |
                                 GND                                                                                |
                                                                                                                    |
+-------------------------------------------------------------------------------------------------------------------+
|                                                                                                                   |
| 2. RELAY DRIVER STAGE (5 V Domain)                                                                                 |
|    (Low-Side Transistor Switch)                                                                                   |
|                                                                                                                   |
|    (From Logic Above)                                                                                             |
|            |                                         V1(5 V)                                                       |
|            v                                           |                                                          |
|      [ R3: 1k ]                                        |                                                          |
|            |                                           v                                                          |
|            +----------------------------------> [ Q1: Base ]                                                      |
|                                                        :                                                          |
|                                             (Controls Current Flow)                                               |
|                                                        :                                                          |
|                                      +-----------------+                                                          |
|                                      |                                                                            |
|                              [ Q1: Collector ]                                                                    |
|                                      ^                                                                            |
|                                      |                                                                            |
|                             (Node: RELAY_COIL_LO)                                                                 |
|                                      |                                                                            |
|                    +-----------------+-----------------+                                                          |
|                    |                                   |                                                          |
|           [ K1: Relay Coil ]                    [ D1: Diode ]                                                     |
|           (Control Side)                        (Protection)                                                      |
|                    |                            (Anode to Coll)                                                   |
|                    |                            (Cathode to VCC)                                                  |
|                    +-----------------+-----------------+                                                          |
|                                      ^                                                                            |
|                                      |                                                                            |
|                                    V1(5 V)                                                                         |
|                                                                                                                   |
|                              [ Q1: Emitter ]                                                                      |
|                                      |                                                                            |
|                                      v                                                                            |
|                                     GND                                                                           |
|                                                                                                                   |
+-------------------------------------------------------------------------------------------------------------------+
|                                                                                                                   |
| 3. MOTOR OUTPUT STAGE (12 V Domain)                                                                                |
|    (High Power Load)                                                                                              |
|                                                                                                                   |
|                       (Magnetic Link from K1 Coil Above)                                                          |
|                                      |                                                                            |
|                                      v                                                                            |
|    V2(12 V) ---------> [ K1: Switch (COM to NO) ] --(MOTOR_POS)--> [ M1: 12 V Motor ] ----> GND                     |
|                                                                                                                   |
+-------------------------------------------------------------------------------------------------------------------+
Electrical Schematic

Truth table

We are using two NAND gates. The first combines the inputs; the second inverts the result to create an AND function.

S1 (Left) S2 (Right) U1 Pin 3 (1Y) U1 Pin 6 (2Y) Motor State
0 (OFF) 0 (OFF) 1 (High) 0 (Low) STOP
0 (OFF) 1 (ON) 1 (High) 0 (Low) STOP
1 (ON) 0 (OFF) 1 (High) 0 (Low) STOP
1 (ON) 1 (ON) 0 (Low) 1 (High) RUN

Measurements and tests

  1. Idle Check: Power on V1. Do not press any buttons. Measure the voltage at LOGIC_OUT (Pin 6). It should be ~0 V. The motor should be stopped.
  2. Input Validation: Press S1 only. Measure voltage at Pin 1. It should be 5 V. Pin 2 should be 0 V. Output at Pin 6 must remain 0 V.
  3. Active Test: Press and hold both S1 and S2 simultaneously.
    • Listen for the «click» of the relay K1.
    • Observe M1 spinning.
    • Measure the voltage at LOGIC_OUT; it should be close to 5 V.
  4. Release Test: Release just one button. The motor must stop immediately.

SPICE netlist and simulation

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

* Practical case: Logic AND gate controlling a relay and motor

* ==============================================================================
* COMPONENT MODELS
* ==============================================================================
* Generic NPN Transistor Model (2N2222)
.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)

* Generic Diode Model (1N4007)
.model D1N4007 D(IS=7.02767n RS=0.0341512 N=1.80803 EG=1.11 XTI=3 BV=1000 IBV=10m CJO=10p VJ=0.7 M=0.5 FC=0.5 TT=100n)

* Ideal Switch Model for Buttons and Relay Contact
* Vt=2.5V (Logic Threshold), Ron=0.1 Ohm, Roff=10 MegOhm
.model SW_IDEAL SW(Vt=2.5 Vh=0.1 Ron=0.1 Roff=10Meg)

* ==============================================================================
* POWER SUPPLIES
* ==============================================================================
* V1: 5V DC Supply for Logic and Relay Coil
V1 VCC 0 DC 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.

* Practical case: Logic AND gate controlling a relay and motor

* ==============================================================================
* COMPONENT MODELS
* ==============================================================================
* Generic NPN Transistor Model (2N2222)
.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)

* Generic Diode Model (1N4007)
.model D1N4007 D(IS=7.02767n RS=0.0341512 N=1.80803 EG=1.11 XTI=3 BV=1000 IBV=10m CJO=10p VJ=0.7 M=0.5 FC=0.5 TT=100n)

* Ideal Switch Model for Buttons and Relay Contact
* Vt=2.5V (Logic Threshold), Ron=0.1 Ohm, Roff=10 MegOhm
.model SW_IDEAL SW(Vt=2.5 Vh=0.1 Ron=0.1 Roff=10Meg)

* ==============================================================================
* POWER SUPPLIES
* ==============================================================================
* V1: 5V DC Supply for Logic and Relay Coil
V1 VCC 0 DC 5

* V2: 12V DC Supply for Motor
V2 V_MOTOR_RAIL 0 DC 12

* ==============================================================================
* INPUT STAGE (Safety Switches)
* ==============================================================================
* Simulation of User Pressing Buttons:
* We use Pulse sources (V_ACT_...) to control ideal switches (S1, S2).
* This preserves the Pull-down resistor topology.

* S1: Left Safety Switch (Pushbutton NO)
* Connects VCC to BTN_L when pressed.
* Pulse Pattern: Period 120us, Pulse 50us (Tests asynchronous press)
V_ACT_L ACT_L 0 PULSE(0 5 10u 1u 1u 50u 120u)
S1 VCC BTN_L ACT_L 0 SW_IDEAL
R1 BTN_L 0 10k

* S2: Right Safety Switch (Pushbutton NO)
* Connects VCC to BTN_R when pressed.
* Pulse Pattern: Period 100us, Pulse 50us
V_ACT_R ACT_R 0 PULSE(0 5 20u 1u 1u 50u 100u)
S2 VCC BTN_R ACT_R 0 SW_IDEAL
R2 BTN_R 0 10k

* ==============================================================================
* LOGIC STAGE (U1: 74HC00 Quad NAND)
* ==============================================================================
* Implementing logic using Behavioral Voltage Sources (B-Sources) with continuous
* sigmoid functions for convergence robustness.
* Logic High = 5V, Logic Low = 0V. Threshold ~ 2.5V.

* U1 Gate 1: Inputs BTN_L (Pin 1), BTN_R (Pin 2) -> Output NAND_INTER (Pin 3)
* Function: NAND(BTN_L, BTN_R)
B_U1_G1 NAND_INTER 0 V=5 * (1 - ( (1/(1+exp(-20*(V(BTN_L)-2.5)))) * (1/(1+exp(-20*(V(BTN_R)-2.5)))) ))

* U1 Gate 2: Inputs NAND_INTER (Pin 4, 5) -> Output LOGIC_OUT (Pin 6)
* Function: NAND(NAND_INTER, NAND_INTER) = NOT(NAND_INTER)
* Combined Function: AND(BTN_L, BTN_R)
B_U1_G2 LOGIC_OUT 0 V=5 * (1 - ( (1/(1+exp(-20*(V(NAND_INTER)-2.5)))) * (1/(1+exp(-20*(V(NAND_INTER)-2.5)))) ))

* ==============================================================================
* OUTPUT DRIVER STAGE
* ==============================================================================
* R3: Base current limiting
R3 LOGIC_OUT BASE 1k

* Q1: 2N2222 Relay Driver
* Emitter to GND, Collector to RELAY_COIL_LO
Q1 RELAY_COIL_LO BASE 0 2N2222MOD

* ==============================================================================
* RELAY STAGE (K1)
* ==============================================================================
* Relay Coil Configuration:
* Connected between VCC and RELAY_COIL_LO.
* Modeled as Inductor + Series Resistor.
L_K1 VCC K1_NODE 10m
R_K1 K1_NODE RELAY_COIL_LO 100

* D1: Flyback Diode (1N4007)
* Anode to RELAY_COIL_LO, Cathode to VCC (Reverse biased)
D1 RELAY_COIL_LO VCC D1N4007

* Relay Contact (Switch):
* Logic: Switch closes when Coil is energized.
* Coil is energized when Q1 is ON (RELAY_COIL_LO is Low).
* Control Voltage = V(VCC) - V(RELAY_COIL_LO).
* If Q1 ON: 5V - 0.2V = 4.8V (> 2.5V Threshold) -> Switch CLOSED.
* If Q1 OFF: 5V - 5V = 0V (< 2.5V Threshold) -> Switch OPEN.
B_K1_CTRL K1_CTRL 0 V = V(VCC) - V(RELAY_COIL_LO)
S_K1 V_MOTOR_RAIL MOTOR_POS K1_CTRL 0 SW_IDEAL

* ==============================================================================
* LOAD (Motor M1)
* ==============================================================================
* M1: 12V DC Motor connected between MOTOR_POS and 0
* Modeled as Resistor + Inductor
R_M1 MOTOR_POS M1_INT 20
L_M1 M1_INT 0 5m

* ==============================================================================
* ANALYSIS COMMANDS
* ==============================================================================
.op
* Transient analysis: 1us step, 500us total time
.tran 1u 500u

* Print results for batch processing
* Inputs: BTN_L, BTN_R
* Output: MOTOR_POS (Load Voltage)
* Debug: LOGIC_OUT, RELAY_COIL_LO
.print tran V(BTN_L) V(BTN_R) V(MOTOR_POS) V(LOGIC_OUT) V(RELAY_COIL_LO) I(L_M1)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation confirms the AND logic behavior. The motor voltage (v(motor_pos)) only goes high (~12V) when both inputs (v(btn_l) and v(btn_r)) are high (~5V) simultaneously (e.g., around time index 60-100 and 300-340). When only one or neither is high, the motor voltage remains near zero.
Show raw data table (1202 rows)
Index   time            v(btn_l)        v(btn_r)        v(motor_pos)    v(logic_out)    v(relay_coil_lo l_m1#branch
0	0.000000e+00	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
1	1.000000e-08	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
2	2.000000e-08	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
3	4.000000e-08	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
4	8.000000e-08	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
5	1.600000e-07	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
6	3.200000e-07	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
7	6.400000e-07	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
8	1.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
9	2.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
10	3.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
11	4.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
12	5.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
13	6.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
14	7.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
15	8.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
16	9.280000e-06	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
17	1.000000e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
18	1.010000e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
19	1.027500e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
20	1.032344e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
21	1.040820e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
22	1.043167e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
23	1.047272e-05	4.995005e-03	4.995005e-03	2.399995e-05	-6.01853e-36	5.000000e+00	1.199998e-06
... (1178 more rows) ...

Common mistakes and how to avoid them

  1. Floating Inputs: Forgetting R1 or R2 causes the NAND gate inputs to float, often reading as «High» due to noise. Solution: Ensure pull-down resistors are firmly connected to Ground.
  2. Missing Flyback Diode: Omitting D1 creates voltage spikes when the relay turns off, which can destroy Q1 or reset the logic chip. Solution: Always place a diode across the relay coil (Cathode to positive).
  3. Direct Drive: Attempting to drive the motor or relay directly from the 74HC00 output pin. Solution: Always use a transistor (Q1) to amplify the current for inductive loads like relays.

Troubleshooting

  • Motor runs immediately upon power-up: Check if S1 or S2 are wired as Normally Closed instead of Normally Open, or if the transistor Q1 is shorted.
  • Relay clicks but motor doesn’t run: Check the V2 power supply and the connections on the relay contacts (COM and NO).
  • Logic works but gets hot: Check if VCC (Pin 14) and GND (Pin 7) are reversed. Disconnect power immediately.
  • Erratic behavior: Add a 100 nF decoupling capacitor between Pin 14 and Pin 7 of the IC, close to the chip.

Possible improvements and extensions

  1. Emergency Stop: Add a Normally Closed (NC) latching button in series with the relay coil or the base resistor R3 to cut power instantly regardless of logic state.
  2. Visual Feedback: Add a green LED (with a 330 Ω resistor) connected to node LOGIC_OUT to indicate when the safety condition is met, even if the motor power (V2) is off.

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 article?




Question 2: Which integrated circuit (IC) is used to implement the logic for this project?




Question 3: What is a common real-world application for this type of 'two-hand control' system?




Question 4: What logic function is effectively created using the universal NAND gates in this project?




Question 5: What is the state of the motor if only one button is pressed?




Question 6: Which component is mentioned as responsible for driving the high-current relay coil?




Question 7: What is the expected logic output voltage during a 'Dual Press' state?




Question 8: Why is this circuit considered an accident prevention measure?




Question 9: What is the status of the logic output when the system is in an 'Idle State'?




Question 10: Who is the 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: Veto Voting System

Veto Voting System prototype (Maker Style)

Level: Basic
Implement a logic circuit where a proposal passes only if two members vote «Yes» and a third member grants approval (no veto), using a single 74HC00 IC.

Objective and use case

In this practical case, you will build a decision-making circuit using digital logic. The system represents a committee where Member A and Member B must both vote in favor for a motion to pass, but Member C (the Chairperson) holds an «Authorization Key.» If Member C does not activate their switch (Logic Low), the vote is vetoed regardless of A and B.

  • Corporate Governance: Models a board where majority support is needed but the CEO has final approval.
  • Safety Interlocks: Represents a machine press where two operators must press buttons (A and B), but a Master Enable key (C) must be inserted for the machine to run.
  • Security Access: Requires two distinct user keys plus a central server authorization signal.

Expected Outcome:
* Output High (LED ON): Only when Input A is High, Input B is High, AND Input C is High.
* Output Low (LED OFF): Any other combination (e.g., if A or B vote «No», or if C exerts veto by setting their input Low).
* Target Audience: Students and hobbyists learning to construct complex logic functions (3-input AND) using universal NAND gates.

Materials

  • U1: 74HC00 (Quad 2-Input NAND Gate)
  • SW1: 3-position DIP Switch (or three individual SPST switches), function: Inputs A, B, and C
  • R1: 10 kΩ resistor, function: pull-down for Input A
  • R2: 10 kΩ resistor, function: pull-down for Input B
  • R3: 10 kΩ resistor, function: pull-down for Input C
  • R4: 330 Ω resistor, function: LED current limiting
  • D1: Red LED, function: Logic Output Indicator
  • V1: 5 V DC Power Supply

Pin-out of the IC used

Chip: 74HC00 (Quad 2-input NAND)
This project utilizes all four gates inside the chip to create a 3-input AND function (Y = A · B · C).

Pin Name Logic Function Connection in this case
1 1 A Input Connect to Switch A
2 1B Input Connect to Switch B
3 1Y Output Output of Gate 1 (\overlineA · B)
4 2 A Input Connect to Pin 3 (1Y)
5 2B Input Connect to Pin 3 (1Y)
6 2Y Output Output of Gate 2 (Inverted 1Y \to A · B)
7 GND Ground Connect to Power Supply 0 V
8 3Y Output Output of Gate 3 (\overline(A · B) · C)
9 3 A Input Connect to Switch C
10 3B Input Connect to Pin 6 (2Y)
11 4Y Output Final Output (drives LED)
12 4 A Input Connect to Pin 8 (3Y)
13 4B Input Connect to Pin 8 (3Y)
14 VCC Power Connect to +5 V

Wiring guide

  • VCC: Connect V1 positive terminal, U1 pin 14, and the common side of SW1.
  • GND: Connect V1 negative terminal, U1 pin 7, R1 (bottom), R2 (bottom), R3 (bottom), and D1 (cathode).
  • Input_A: Connect SW1 (Switch 1) to R1 (top) and U1 pin 1.
  • Input_B: Connect SW1 (Switch 2) to R2 (top) and U1 pin 2.
  • Input_C (Veto): Connect SW1 (Switch 3) to R3 (top) and U1 pin 9.
  • Node_NAND1: Connect U1 pin 3 (Output 1) to U1 pin 4 and U1 pin 5 (Inputs of Gate 2).
  • Node_AND_AB: Connect U1 pin 6 (Output 2) to U1 pin 10 (Input of Gate 3).
  • Node_NAND_FINAL: Connect U1 pin 8 (Output 3) to U1 pin 12 and U1 pin 13 (Inputs of Gate 4).
  • Vout: Connect U1 pin 11 (Final Output) to R4 (one side).
  • LED_Anode: Connect R4 (other side) to D1 (anode).

Conceptual block diagram

Conceptual block diagram — 74HC00 NAND gate
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

Practical Case: Veto Voting System (74HC00 Logic Flow)

INPUTS (Active High)           LOGIC PROCESSING (74HC00 Quad NAND)                               OUTPUT
=======================================================================================================

[ SW A + R1 ] --(Pin 1)-->+
                          |
                    [ U1: Gate 1 ] --(Pin 3)--> [ U1: Gate 2 ] --(Pin 6)---+
                    [ 2-In NAND  ]              [ NAND as NOT]             |
                          |                     (Pins 4+5)                 |
[ SW B + R2 ] --(Pin 2)-->+                                                |
                                                                           |
                                                                           v
                                                                     [ U1: Gate 3 ] --(Pin 8)--> [ U1: Gate 4 ] --(Pin 11)--> [ R4: 330R ] --> [ D1: LED ] --> GND
                                                                     [ 2-In NAND  ]              [ NAND as NOT]
                                                                     (Pin 10)      \             (Pins 12+13)
                                                                                    \
[ SW C + R3 ] -------------------------------------------------------(Pin 9)---------+
(Veto/Enable)

=======================================================================================================
Logic Summary:
1. Gate 1 & 2 form an AND gate for Inputs A & B.
2. Gate 3 & 4 form an AND gate for (Result of A/B) & Input C.
3. Final Function: LED turns ON only if A AND B AND C are all High.
Electrical Schematic

Truth table

The circuit implements the logic function Y = A · B · C.
* A/B: Voters
* C: Chairman/Authorization (0 = Veto/Block, 1 = Allow)

Input A (Voter 1) Input B (Voter 2) Input C (Authorization) Output Y (LED) Result
0 0 0 0 Fail
0 1 1 0 Fail (Lack of votes)
1 0 1 0 Fail (Lack of votes)
1 1 0 0 VETOED
1 1 1 1 Approved

Measurements and tests

  1. Supply Check: Before inserting the IC, verify 5 V between VCC and GND lines on your breadboard.
  2. Default State: Ensure all switches are OFF. Power on. LED should be OFF.
  3. Veto Test: Turn Switch A and Switch B ON (High). Keep Switch C OFF (Low).
    • Observation: LED must remain OFF. This confirms the Veto is active.
  4. Approval Test: With A and B still ON, turn Switch C ON.
    • Observation: LED should light up (Logic High, approx 3.5 V – 4.5 V).
  5. Single Vote Test: Turn Switch A OFF while B and C are ON.
    • Observation: LED should turn OFF.

SPICE netlist and simulation

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

* Practical case: Veto Voting System
.width out=256

* ==============================================================================
* Components and Models
* ==============================================================================

* LED Model
.model DLED D(IS=1e-22 RS=10 N=1.5 CJO=10p)

* 74HC00 Quad 2-Input NAND Gate Subcircuit
* Pins: 1=1A, 2=1B, 3=1Y, 4=2A, 5=2B, 6=2Y, 7=GND, 8=3Y, 9=3A, 10=3B, 11=4Y, 12=4A, 13=4B, 14=VCC
.subckt 74HC00 1 2 3 4 5 6 7 8 9 10 11 12 13 14
* Logic Threshold (2.5V) and Gain (20)
.param Vth=2.5
.param K=20
* Gate 1: Inputs 1,2 -> Output 3
B1 3 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(1,7)-Vth)))) * (1/(1+exp(-K*(V(2,7)-Vth)))))
* Gate 2: Inputs 4,5 -> Output 6
B2 6 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(4,7)-Vth)))) * (1/(1+exp(-K*(V(5,7)-Vth)))))
* ... (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: Veto Voting System
.width out=256

* ==============================================================================
* Components and Models
* ==============================================================================

* LED Model
.model DLED D(IS=1e-22 RS=10 N=1.5 CJO=10p)

* 74HC00 Quad 2-Input NAND Gate Subcircuit
* Pins: 1=1A, 2=1B, 3=1Y, 4=2A, 5=2B, 6=2Y, 7=GND, 8=3Y, 9=3A, 10=3B, 11=4Y, 12=4A, 13=4B, 14=VCC
.subckt 74HC00 1 2 3 4 5 6 7 8 9 10 11 12 13 14
* Logic Threshold (2.5V) and Gain (20)
.param Vth=2.5
.param K=20
* Gate 1: Inputs 1,2 -> Output 3
B1 3 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(1,7)-Vth)))) * (1/(1+exp(-K*(V(2,7)-Vth)))))
* Gate 2: Inputs 4,5 -> Output 6
B2 6 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(4,7)-Vth)))) * (1/(1+exp(-K*(V(5,7)-Vth)))))
* Gate 3: Inputs 9,10 -> Output 8
B3 8 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(9,7)-Vth)))) * (1/(1+exp(-K*(V(10,7)-Vth)))))
* Gate 4: Inputs 12,13 -> Output 11
B4 11 7 V = V(14,7) * (1 - (1/(1+exp(-K*(V(12,7)-Vth)))) * (1/(1+exp(-K*(V(13,7)-Vth)))))
.ends 74HC00

* ==============================================================================
* Main Circuit Wiring
* ==============================================================================

* Power Supply (V1)
V1 VCC 0 DC 5

* Inputs (Simulating Switches SW1 positions A, B, C)
* Generating dynamic pulses to test the truth table (000 to 111)
* Input A (LSB, Period 100us)
Va Input_A 0 PULSE(0 5 10u 1u 1u 50u 100u)
* Input B (Period 200us)
Vb Input_B 0 PULSE(0 5 10u 1u 1u 100u 200u)
* Input C (MSB, Period 400us)
Vc Input_C 0 PULSE(0 5 10u 1u 1u 200u 400u)

* Pull-down Resistors (R1, R2, R3)
R1 Input_A 0 10k
R2 Input_B 0 10k
R3 Input_C 0 10k

* Logic IC U1 (74HC00)
* Connectivity based on Wiring Guide:
* Pin 1 (In A) -> Input_A
* Pin 2 (In B) -> Input_B
* Pin 3 (Out 1) -> Node_NAND1
* Pin 4 (In 2A) -> Node_NAND1
* Pin 5 (In 2B) -> Node_NAND1
* Pin 6 (Out 2) -> Node_AND_AB
* Pin 7 (GND)   -> 0
* Pin 8 (Out 3) -> Node_NAND_FINAL
* Pin 9 (In 3A) -> Input_C
* Pin 10 (In 3B)-> Node_AND_AB
* Pin 11 (Out 4)-> Vout
* Pin 12 (In 4A)-> Node_NAND_FINAL
* Pin 13 (In 4B)-> Node_NAND_FINAL
* Pin 14 (VCC)  -> VCC
XU1 Input_A Input_B Node_NAND1 Node_NAND1 Node_NAND1 Node_AND_AB 0 Node_NAND_FINAL Input_C Node_AND_AB Vout Node_NAND_FINAL Node_NAND_FINAL VCC 74HC00

* Output Stage
R4 Vout LED_Anode 330
D1 LED_Anode 0 DLED

* ==============================================================================
* Simulation Commands
* ==============================================================================

.op
.tran 1u 500u

* Print Inputs and Output to check logic (Vout should be High only when A, B, C are High)
.print tran V(Input_A) V(Input_B) V(Input_C) V(Vout)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation successfully sweeps inputs A, B, and C. Vout is High (5V) only when A, B, and C are all High (e.g., around 33us, 301us, 559us). In all other states (000, 011, 101, 110, etc.), Vout remains Low (~0V). This matches the logic Y = A · B · C.
Show raw data table (671 rows)
Index   time            v(input_a)      v(input_b)      v(input_c)      v(vout)
0	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
1	1.000000e-08	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
2	2.000000e-08	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
3	4.000000e-08	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
4	8.000000e-08	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
5	1.600000e-07	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
6	3.200000e-07	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
7	6.400000e-07	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
8	1.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
9	2.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
10	3.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
11	4.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
12	5.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
13	6.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
14	7.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
15	8.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
16	9.280000e-06	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
17	1.000000e-05	0.000000e+00	0.000000e+00	0.000000e+00	-6.01853e-36
18	1.010000e-05	5.000000e-01	5.000000e-01	5.000000e-01	-6.01853e-36
19	1.030000e-05	1.500000e+00	1.500000e+00	1.500000e+00	-6.01853e-36
20	1.048757e-05	2.437858e+00	2.437858e+00	2.437858e+00	-6.01853e-36
21	1.071179e-05	3.558937e+00	3.558937e+00	3.558937e+00	5.000000e+00
22	1.085965e-05	4.298271e+00	4.298271e+00	4.298271e+00	5.000000e+00
23	1.099537e-05	4.976846e+00	4.976846e+00	4.976846e+00	5.000000e+00
... (647 more rows) ...

Common mistakes and how to avoid them

  1. Floating Inputs: Forgetting the pull-down resistors (R1, R2, R3). Without them, the CMOS inputs of the 74HC00 will float, causing erratic switching or oscillation.
  2. Confusing Pinout: The 74HC00 pinout is standard, but mixing up Input pins (e.g., 1 A/1B) with Output pins (e.g., 1Y) is common. Double-check the datasheet diagram.
  3. Misinterpreting «Veto»: In this design, Input C is an «Enable» line (Active High). If you think of Veto as «Switch ON to Block» (Active Low logic), the wiring of Switch C would need to be inverted. Here, Switch C ON means «Authorize».

Troubleshooting

  • LED never turns ON: Check that the LED polarity is correct (Anode to resistor, Cathode to GND). Verify U1 is powered (Pin 14 to 5 V, Pin 7 to GND).
  • LED stays ON even when switches are OFF: Check if R1, R2, or R3 are missing or disconnected. Ensure you are not using NC (Normally Closed) switches by mistake.
  • Circuit works for A and B but C has no effect: Check the wiring on Gate 3 (Pins 8, 9, 10). Ensure pin 9 connects to Switch C and pin 10 connects to the output of the previous stage (Pin 6).

Possible improvements and extensions

  1. Veto Indicator: Add a second LED (Green) driven by an unused gate (or a transistor) connected to Input C, indicating «Session Open» (Authorization Granted) or «Session Locked» (Veto).
  2. Majority Vote Modification: Redesign the circuit to allow the proposal to pass if any two of the three members (A, B, C) vote Yes, removing the specific veto power and making all members equal.

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 specific integrated circuit (IC) is specified to implement the logic?




Question 3: What exact condition is required for the proposal to pass (LED ON)?




Question 4: What role does Member C play in this circuit?




Question 5: In the 'Safety Interlocks' use case, what does Input C represent?




Question 6: Based on the required outcome (High only if A, B, and C are High), what logic function is this circuit implementing?




Question 7: What happens to the output if Member C sets their input to Logic Low?




Question 8: Which real-world scenario is NOT listed as a use case for this circuit?




Question 9: If Member A votes 'Yes' (High) and Member B votes 'No' (Low), what is the output state?




Question 10: Why is the 74HC00 IC suitable for this specific task?




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: Water tank level control

Water tank level control prototype (Maker Style)

Level: Basic. Implement a logic safety stop for a water pump using a NAND gate.

Objective and use case

In this session, you will build a digital safety circuit using a 74HC00 NAND gate. The circuit monitors two liquid level sensors and automatically cuts power to a «pump» (simulated by an LED) only when both sensors indicate the tank is dangerously full.

  • Industrial tank filling: Prevents chemical spills by ensuring redundant sensors must agree before triggering an emergency shutdown.
  • Sump pump systems: Prevents motor burnout or overflow by managing logic states between high-water and critical-overflow marks.
  • Home automation: Simple logic for reservoir management without needing a microcontroller.

Expected outcome:
* Normal Operation: The LED (pump) remains ON (Logic High, ~5 V) when the tank is empty or partially full.
* Emergency Stop: The LED turns OFF (Logic Low, ~0 V) immediately when both switch inputs are Logic High (simulating water detection).
* Validation: A specific Truth Table will be verified where only the input condition 1, 1 results in an output of 0.

Target audience: Basic level electronics students and hobbyists.

Materials

  • V1: 5 V DC power supply, function: Main circuit power.
  • U1: 74HC00, function: Quad 2-Input NAND Gate IC.
  • S1: SPST Toggle Switch, function: High Level Sensor simulator.
  • S2: SPST Toggle Switch, function: Safety Level Sensor simulator.
  • R1: 10 kΩ resistor, function: Pull-down for S1.
  • R2: 10 kΩ resistor, function: Pull-down for S2.
  • R3: 330 Ω resistor, function: Current limiting for the Pump Status LED.
  • D1: Green LED, function: Pump status indicator (ON = Running, OFF = Stopped).

Pin-out of the IC used

Chip: 74HC00 (Quad 2-Input NAND Gate)

Pin Name Logic function Connection in this case
1 1 A Input A Connected to Sensor S1
2 1B Input B Connected to Sensor S2
3 1Y Output Y Connected to LED (Pump)
7 GND Ground Connected to 0 V
14 VCC Power Connected to 5 V

Wiring guide

Construct the circuit following these node connections. Ensure the power supply is off while building.

  • Power Rail: Connect V1 positive terminal to node VCC and negative terminal to node 0 (GND).
  • IC Power: Connect U1 pin 14 to VCC and pin 7 to 0.
  • Sensor 1 (Input A):
    • Connect S1 between VCC and node SENSOR_HI.
    • Connect R1 between SENSOR_HI and 0 (Pull-down).
    • Connect U1 pin 1 to node SENSOR_HI.
  • Sensor 2 (Input B):
    • Connect S2 between VCC and node SENSOR_SAFE.
    • Connect R2 between SENSOR_SAFE and 0 (Pull-down).
    • Connect U1 pin 2 to node SENSOR_SAFE.
  • Pump Control (Output):
    • Connect U1 pin 3 to node PUMP_CTRL.
    • Connect D1 (Anode) to node PUMP_CTRL.
    • Connect D1 (Cathode) to node LED_NODE.
    • Connect R3 between LED_NODE and 0.

Conceptual block diagram

Conceptual block diagram — 74HC00 NAND gate
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

Title: Practical case: Water tank level control

      INPUTS (Sensors)                       PROCESSING (U1: 74HC00)                  OUTPUT (Pump Indicator)
   ======================                  ===========================              ===========================

   [ VCC ]
      |
   [ S1: High Sensor ]
      |
      +--(Node: SENSOR_HI)---------------> [ U1: Pin 1 (Input A) ]
      |                                             |
   [ R1: 10k Pull-Down ]                            |
      |                                             v
   [ GND ]                                     [ NAND Gate ] --(Node: PUMP_CTRL)--> [ D1: Green LED ]
                                                    ^                                       |
                                                    |                               (Node: LED_NODE)
   [ VCC ]                                          |                                       |
      |                                             |                                  [ R3: 330R ]
   [ S2: Safe Sensor ]                              |                                       |
      |                                             |                                    [ GND ]
      +--(Node: SENSOR_SAFE)-------------> [ U1: Pin 2 (Input B) ]
      |
   [ R2: 10k Pull-Down ]
      |
   [ GND ]

   (Note: U1 Power Connections -> Pin 14: VCC, Pin 7: GND)
Electrical Schematic

Truth table

The 74HC00 acts as a safety interlock. The pump runs (Output 1) by default and only stops (Output 0) when the specific danger condition (1, 1) is met.

S1 (High Level) S2 (Safety Level) Voltage at Pin 3 Pump Status (LED) Logic State
0 (Dry) 0 (Dry) High (~5 V) ON Safe
0 (Dry) 1 (Wet) High (~5 V) ON Sensor Error/Safe
1 (Wet) 0 (Dry) High (~5 V) ON Warning Level
1 (Wet) 1 (Wet) Low (~0 V) OFF STOP (Danger)

Measurements and tests

  1. Default State Check: Ensure S1 and S2 are open (OFF). Power on the circuit. Measure the voltage at node PUMP_CTRL relative to GND. It should read approximately 5 V. The Green LED should be lit.
  2. Single Sensor Test: Close S1 only. The LED should remain ON. Open S1 and close S2 only. The LED should remain ON.
  3. Safety Stop Test: Close both S1 and S2 simultaneously.
    • Measure the voltage at node PUMP_CTRL. It should drop to near 0 V (< 0.1 V).
    • Confirm the LED turns OFF immediately.
  4. Recovery: Open either switch; the LED should turn back ON.

SPICE netlist and simulation

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

* Practical case: Water tank level control
.width out=256

* --- Models ---
* Generic Green LED Model
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=10p)
* Ideal Voltage-Controlled Switch Model
.model SW_IDEAL sw(vt=2.5 vh=0 ron=1 roff=10Meg)

* --- Power Supply ---
* V1: 5 V DC power supply
V1 VCC 0 DC 5

* --- Input Sensors (Switches + Pull-downs) ---
* S1: High Level Sensor simulator
* Modeled as a switch connected to VCC, controlled by a pulse source (V_ACT1)
* Timing: Period 100us, covers logic states quickly
V_ACT1 ACT1 0 PULSE(0 5 0 1u 1u 50u 100u)
S1 VCC SENSOR_HI ACT1 0 SW_IDEAL
R1 SENSOR_HI 0 10k
* ... (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: Water tank level control
.width out=256

* --- Models ---
* Generic Green LED Model
.model DLED D(IS=1e-14 N=2 RS=10 BV=5 IBV=10u CJO=10p)
* Ideal Voltage-Controlled Switch Model
.model SW_IDEAL sw(vt=2.5 vh=0 ron=1 roff=10Meg)

* --- Power Supply ---
* V1: 5 V DC power supply
V1 VCC 0 DC 5

* --- Input Sensors (Switches + Pull-downs) ---
* S1: High Level Sensor simulator
* Modeled as a switch connected to VCC, controlled by a pulse source (V_ACT1)
* Timing: Period 100us, covers logic states quickly
V_ACT1 ACT1 0 PULSE(0 5 0 1u 1u 50u 100u)
S1 VCC SENSOR_HI ACT1 0 SW_IDEAL
R1 SENSOR_HI 0 10k

* S2: Safety Level Sensor simulator
* Modeled as a switch connected to VCC, controlled by a pulse source (V_ACT2)
* Timing: Period 200us, provides different state combinations with S1
V_ACT2 ACT2 0 PULSE(0 5 0 1u 1u 100u 200u)
S2 VCC SENSOR_SAFE ACT2 0 SW_IDEAL
R2 SENSOR_SAFE 0 10k

* --- Logic IC: U1 (74HC00 Quad 2-Input NAND Gate) ---
* Wiring Guide connections:
* Pin 1 (Input A) -> SENSOR_HI
* Pin 2 (Input B) -> SENSOR_SAFE
* Pin 3 (Output)  -> PUMP_CTRL
* Pin 7 (GND)     -> 0
* Pin 14 (VCC)    -> VCC

.subckt 74HC00 1 2 3 7 14
    * Behavioral NAND implementation using continuous sigmoid functions for convergence
    * V(3) = VCC * (1 - (Sigmoid(In1) * Sigmoid(In2)))
    * Threshold is set to VCC/2 (approx 2.5V)
    B_NAND 3 7 V = V(14) * (1 - ( (1/(1+exp(-50*(V(1)-0.5*V(14))))) * (1/(1+exp(-50*(V(2)-0.5*V(14))))) ))
.ends

XU1 SENSOR_HI SENSOR_SAFE PUMP_CTRL 0 VCC 74HC00

* --- Output Stage ---
* D1: Pump Status LED (Green)
* R3: Current limiting resistor
D1 PUMP_CTRL LED_NODE DLED
R3 LED_NODE 0 330

* --- Simulation Directives ---
.op
* Transient analysis for 500us to capture full truth table sequence
.tran 1u 500u

* --- Output Printing ---
* Required to generate simulation log
.print tran V(SENSOR_HI) V(SENSOR_SAFE) V(PUMP_CTRL) V(LED_NODE)

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation successfully cycles through all 4 logic states. When both inputs are High (~5V), the output PUMP_CTRL goes Low (~0V) and the LED voltage drops to ~0V (OFF). In all other states (00, 01, 10), the output is High (~5V) and the LED node is ~3.46V (ON).
Show raw data table (810 rows)
Index   time            v(sensor_hi)    v(sensor_safe)  v(pump_ctrl)    v(led_node)
0	0.000000e+00	4.995005e-03	4.995005e-03	5.000000e+00	3.463208e+00
1	1.000000e-08	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
2	2.000000e-08	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
3	4.000000e-08	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
4	8.000000e-08	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
5	1.600000e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
6	3.200000e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
7	3.562500e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
8	4.196875e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
9	4.372461e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
10	4.679736e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
11	4.795524e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
12	4.902290e-07	4.995005e-03	4.995005e-03	5.000000e+00	3.463209e+00
13	5.023412e-07	4.999500e+00	4.999500e+00	4.417025e-69	-7.81556e-01
14	5.138120e-07	4.999500e+00	4.999500e+00	4.417025e-69	1.002344e-01
15	5.170059e-07	4.999500e+00	4.999500e+00	4.417025e-69	3.466376e-02
16	5.182905e-07	4.999500e+00	4.999500e+00	4.417025e-69	2.349502e-02
17	5.201200e-07	4.999500e+00	4.999500e+00	4.417025e-69	1.345184e-02
18	5.222326e-07	4.999500e+00	4.999500e+00	4.417025e-69	7.036188e-03
19	5.244685e-07	4.999500e+00	4.999500e+00	4.417025e-69	3.539225e-03
20	5.268938e-07	4.999500e+00	4.999500e+00	4.417025e-69	1.673565e-03
21	5.291278e-07	4.999500e+00	4.999500e+00	4.417025e-69	8.446489e-04
22	5.313933e-07	4.999500e+00	4.999500e+00	4.417025e-69	4.221950e-04
23	5.337647e-07	4.999500e+00	4.999500e+00	4.417025e-69	2.037947e-04
... (786 more rows) ...

Common mistakes and how to avoid them

  1. Floating Inputs: Forgetting R1 or R2 results in erratic switching because the CMOS inputs pick up electrical noise when switches are open. Fix: Always ensure inputs are pulled to Ground via resistors when the switch is open.
  2. LED Polarity: Connecting the LED backwards prevents it from lighting even when logic is High. Fix: Ensure the longer leg (Anode) faces the IC output pin.
  3. Shorting Output to Ground: Connecting Pin 3 directly to Ground to «test» it will damage the IC when it tries to output High. Fix: Always measure voltage with a multimeter in parallel, never force a node to ground with a wire.

Troubleshooting

  • Symptom: LED is always ON, even when both switches are closed.
    • Cause: Resistors R1/R2 might be connected to VCC instead of GND, or the IC is bypassed.
    • Fix: Check that R1 and R2 connect to the negative rail (0) and switches connect to VCC.
  • Symptom: LED never turns ON.
    • Cause: LED reversed or R3 is too high value/open.
    • Fix: Check D1 orientation and continuity of R3.
  • Symptom: Circuit behaves erratically when touching wires.
    • Cause: Floating inputs (Missing pull-down resistors).
    • Fix: Verify R1 and R2 are securely connected to node 0.

Possible improvements and extensions

  1. Visual and Audible Alarm: Connect an additional active buzzer (via a transistor driver) to the output, but invert the signal first so the buzzer sounds when the pump stops.
  2. Motor Drive: Replace the LED with an NPN transistor (like 2N2222) and a relay to control a real DC water pump motor, adding a flyback diode for protection.

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 specific logic gate IC is used to build this safety circuit?




Question 3: In the 'Normal Operation' state, what is the status of the LED (pump)?




Question 4: What condition triggers the 'Emergency Stop' where the LED turns OFF?




Question 5: What component is used to simulate the 'Pump' in this circuit?




Question 6: According to the expected outcome, what is the only input condition that results in an output of 0?




Question 7: Which of the following is NOT listed as a use case for this circuit?




Question 8: What is the target audience for this specific electronics session?




Question 9: Why is this circuit useful for industrial tank filling?




Question 10: What logic voltage level represents the LED being ON in this circuit?




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: Window sensor security alarm

Window sensor security alarm prototype (Maker Style)

Level: Basic | Build a fail-safe alarm system using NAND logic to detect open windows.

Objective and use case

In this practical case, you will build a digital logic circuit that monitors two window sensors. The alarm will remain silent (LED OFF) only when both windows are securely closed. If either window is opened—or if a wire is cut—the alarm triggers (LED ON).

  • Home Security: Monitoring multiple entry points (windows/doors) where all must be closed to secure the perimeter.
  • Machine Safety: Ensuring all safety guards or maintenance hatches are closed before a machine can operate (or signaling a fault if opened).
  • Fail-Safe Design: Demonstrating how active-high loops detect broken wires or open switches as alarm conditions.

Expected outcome:
* Safe State: When both switches (windows) are closed (Logic 1), the Output is 0 V (LED OFF).
* Alarm State: If Switch 1 OR Switch 2 is opened (Logic 0), the Output rises to ≈ 5 V (LED ON).
* Logic Verification: Confirmation of the NAND truth table behavior where Output is LOW only if all inputs are HIGH.

Target audience: Electronics students and hobbyists learning basic digital logic gates.

Materials

  • V1: 5 V DC power supply, function: Main circuit power
  • U1: 74HC00, function: Quad 2-input NAND gate IC
  • SW1: SPST switch, function: Window 1 sensor (Closed = Window Closed)
  • SW2: SPST switch, function: Window 2 sensor (Closed = Window Closed)
  • R1: 10 kΩ resistor, function: Pull-down for SW1
  • R2: 10 kΩ resistor, function: Pull-down for SW2
  • R3: 330 Ω resistor, function: Current limiting for LED
  • D1: Red LED, function: Alarm indicator

Pin-out of the IC used

Chip Selected: 74HC00 (Quad 2-Input NAND Gate)

Pin Name Logic Function Connection in this case
1 1 A Input A Connected to Node SENS1
2 1B Input B Connected to Node SENS2
3 1Y Output Connected to Node ALARM_OUT
7 GND Ground Connected to Node 0 (GND)
14 VCC Power Supply Connected to Node VCC (5 V)

Wiring guide

This guide uses specific node names to help you visualize the connections on a breadboard.

  • Power Rail: Connect V1 positive terminal to node VCC and negative terminal to node 0.
  • IC Power: Connect U1 pin 14 to VCC and U1 pin 7 to 0.
  • Sensor 1: Connect SW1 between VCC and node SENS1.
  • Pull-down 1: Connect R1 between SENS1 and 0.
  • Sensor 2: Connect SW2 between VCC and node SENS2.
  • Pull-down 2: Connect R2 between SENS2 and 0.
  • Logic Input: Connect U1 pin 1 (1 A) to SENS1 and U1 pin 2 (1B) to SENS2.
  • Logic Output: Connect U1 pin 3 (1Y) to node ALARM_OUT.
  • Indicator: Connect R3 between ALARM_OUT and the Anode of D1.
  • LED Ground: Connect the Cathode of D1 to node 0.

Conceptual block diagram

Conceptual block diagram — 74HC00 NAND gate
Quick read: inputs → main block → output (actuator or measurement). This summarizes the ASCII schematic below.

Schematic

Title: Practical case: Window sensor security alarm

(Input Stage: Sensors)                  (Processing Stage: Logic)             (Output Stage: Alarm)

[ VCC ]
   |
[ SW1: Window 1 ]
   |
   +--(SENS1)-------+-----------------> [ U1: Pin 1 (Input A) ]
                    |                                         |
                 [ R1: 10k ]                                  v
                    |                                 [ U1: NAND Gate ] --(ALARM_OUT)--> [ R3: 330 Ω ] --> [ D1: LED ] --> GND
                 [ GND ]                                      ^
                                                              |
[ VCC ]             |                                         |
   |                |                                         |
[ SW2: Window 2 ]   |                                         |
   |                |                                         |
   +--(SENS2)-------+-----------------> [ U1: Pin 2 (Input B) ]
                    |
                 [ R2: 10k ]
                    |
                 [ GND ]
Electrical Schematic

Truth table

The 74HC00 implements the NAND function. In this security context, Logic 1 represents a «Closed Window» (Safe), and Logic 0 represents an «Open Window» (Breach).

Window 1 (SW1) Window 2 (SW2) Input A (Pin 1) Input B (Pin 2) Output Y (Pin 3) LED State Status
Closed Closed 1 (High) 1 (High) 0 (Low) OFF Secure
Open Closed 0 (Low) 1 (High) 1 (High) ON ALARM
Closed Open 1 (High) 0 (Low) 1 (High) ON ALARM
Open Open 0 (Low) 0 (Low) 1 (High) ON ALARM

Measurements and tests

Follow these steps to validate your alarm system:

  1. Initial Power-Up: Ensure both switches (SW1, SW2) are closed. Turn on the 5 V supply. The LED D1 should be OFF.
  2. Voltage Check (Secure): Use a multimeter to measure the voltage at node ALARM_OUT. It should be close to 0 V (< 0.2 V).
  3. Breach Test 1: Open SW1 while keeping SW2 closed. The LED should turn ON. Measure ALARM_OUT; it should read close to 5 V.
  4. Breach Test 2: Close SW1 and open SW2. The LED should turn ON.
  5. Total Breach: Open both switches. The LED should remain ON.

SPICE netlist and simulation

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

* Practical case: Window sensor security alarm
.width out=256
* ngspice netlist

* --- Component Models ---
* Switch model for SW1 and SW2 (Sensors)
* Vt=2.5V: Switch closes when control voltage > 2.5V
* Ron=1m: Low resistance when closed
* Roff=100Meg: High resistance when open
.model SW_MOD SW(Vt=2.5 Ron=1m Roff=100Meg)

* LED model for D1
.model LED_RED D(IS=1e-22 RS=5 N=1.5 CJO=10p BV=5)

* --- Power Supply ---
* V1: 5 V DC power supply connected to VCC and 0 (GND)
V1 VCC 0 DC 5

* --- Window Sensor 1 ---
* Control source V_ACT1 simulates the physical action of opening/closing Window 1
* ... (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: Window sensor security alarm
.width out=256
* ngspice netlist

* --- Component Models ---
* Switch model for SW1 and SW2 (Sensors)
* Vt=2.5V: Switch closes when control voltage > 2.5V
* Ron=1m: Low resistance when closed
* Roff=100Meg: High resistance when open
.model SW_MOD SW(Vt=2.5 Ron=1m Roff=100Meg)

* LED model for D1
.model LED_RED D(IS=1e-22 RS=5 N=1.5 CJO=10p BV=5)

* --- Power Supply ---
* V1: 5 V DC power supply connected to VCC and 0 (GND)
V1 VCC 0 DC 5

* --- Window Sensor 1 ---
* Control source V_ACT1 simulates the physical action of opening/closing Window 1
* Logic: High (5V) = Window Closed, Low (0V) = Window Open
* Timing: Toggles every 100us (Period 200us)
V_ACT1 ACT1 0 PULSE(0 5 0 1u 1u 100u 200u)

* SW1: Connects VCC to SENS1 when window is closed
S1 VCC SENS1 ACT1 0 SW_MOD

* R1: Pull-down resistor for SENS1 (10k)
R1 SENS1 0 10k

* --- Window Sensor 2 ---
* Control source V_ACT2 simulates Window 2
* Timing: Toggles every 200us (Period 400us) to test all truth table combinations
V_ACT2 ACT2 0 PULSE(0 5 0 1u 1u 200u 400u)

* SW2: Connects VCC to SENS2 when window is closed
S2 VCC SENS2 ACT2 0 SW_MOD

* R2: Pull-down resistor for SENS2 (10k)
R2 SENS2 0 10k

* --- Logic IC: U1 (74HC00) ---
* Quad 2-input NAND gate. We instantiate one gate.
* Pin mapping according to wiring guide:
* Pin 1 (Input A) -> SENS1
* Pin 2 (Input B) -> SENS2
* Pin 3 (Output Y) -> ALARM_OUT
* Pin 7 -> GND (0), Pin 14 -> VCC
XU1 SENS1 SENS2 ALARM_OUT 0 VCC 74HC00_GATE

* Subcircuit for NAND Gate using robust continuous functions
.subckt 74HC00_GATE A B Y GND VCC
* Logic: Y = NAND(A, B) = NOT(A AND B)
* Implemented using sigmoid functions for convergence:
* 1 / (1 + exp(-k*(V-Vth))) acts as a smooth logical comparator.
* Vth = 2.5V, k = 20
B_NAND Y GND V = V(VCC) * (1 - ( (1/(1+exp(-20*(V(A)-2.5)))) * (1/(1+exp(-20*(V(B)-2.5)))) ))
.ends

* --- Alarm Output Indicator ---
* R3: Current limiting resistor (330 Ohm)
R3 ALARM_OUT LED_ANODE 330

* D1: Red LED connected to Ground
D1 LED_ANODE 0 LED_RED

* --- Simulation Commands ---
.op
* Transient analysis for 500us to capture all logic states
.tran 1u 500u

* Output configuration
* We print the Sensor inputs and the Alarm output
.print tran V(SENS1) V(SENS2) V(ALARM_OUT) V(LED_ANODE)

.end

Simulation Results (Transient Analysis)

Simulation Results (Transient Analysis)

Analysis: The simulation correctly implements the NAND logic truth table. When both sensors are High (5V, Closed), the Output is Low (~0V). If either or both sensors are Low (Open), the Output goes High (5V), activating the LED (approx 1.83V at anode).
Show raw data table (657 rows)
Index   time            v(sens1)        v(sens2)        v(alarm_out)    v(led_anode)
0	0.000000e+00	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
1	1.000000e-08	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
2	2.000000e-08	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
3	4.000000e-08	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
4	8.000000e-08	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
5	1.600000e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
6	3.200000e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
7	3.562500e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
8	4.196875e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
9	4.372461e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
10	4.679736e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
11	4.795524e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
12	4.902290e-07	4.999500e-04	4.999500e-04	5.000000e+00	1.833072e+00
13	5.023412e-07	5.000000e+00	5.000000e+00	3.894872e-36	1.057689e+00
14	5.138120e-07	5.000000e+00	5.000000e+00	3.894872e-36	-7.61250e-02
15	5.160398e-07	5.000000e+00	5.000000e+00	3.894872e-36	-3.72798e-02
16	5.172425e-07	5.000000e+00	5.000000e+00	3.894872e-36	-2.57490e-02
17	5.188923e-07	5.000000e+00	5.000000e+00	3.894872e-36	-1.54585e-02
18	5.214063e-07	5.000000e+00	5.000000e+00	3.894872e-36	-6.97976e-03
19	5.238372e-07	5.000000e+00	5.000000e+00	3.894872e-36	-3.25627e-03
20	5.261078e-07	5.000000e+00	5.000000e+00	3.894872e-36	-1.60566e-03
21	5.281984e-07	5.000000e+00	5.000000e+00	3.894872e-36	-8.40881e-04
22	5.304310e-07	5.000000e+00	5.000000e+00	3.894872e-36	-4.20300e-04
23	5.328536e-07	5.000000e+00	5.000000e+00	3.894872e-36	-1.97001e-04
... (633 more rows) ...

Common mistakes and how to avoid them

  1. Floating Inputs: Forgetting R1 or R2 causes the inputs to «float» when switches are open, leading to erratic LED flickering. Solution: Always ensure inputs have a path to ground (via pull-down resistors) when the switch is open.
  2. LED Polarity: Connecting the LED backwards prevents it from lighting up even when the alarm is active. Solution: Ensure the longer leg (Anode) faces the resistor and IC output.
  3. Incorrect Switch Wiring: Placing the switch in parallel with the resistor instead of in series with the voltage source creates a short circuit. Solution: Follow the wiring guide: VCC -> Switch -> Node -> Resistor -> GND.

Troubleshooting

  • Symptom: LED is always ON, even when switches are closed.
    • Cause: One of the switches is not making contact, or an input wire is loose.
    • Fix: Check continuity across SW1 and SW2; ensure pin 1 and pin 2 actually receive 5 V.
  • Symptom: LED never turns ON.
    • Cause: LED is reversed, IC is not powered, or R3 is too high.
    • Fix: Check pin 14 for 5 V. Reverse the LED. Verify R3 is 330 Ω, not 330 kΩ.
  • Symptom: Logic works reversed (LED ON when safe, OFF when open).
    • Cause: You may be using an AND gate (74HC08) instead of NAND, or your switch/resistor logic is inverted (Pull-ups instead of Pull-downs).
    • Fix: Verify the chip number is 74HC00.

Possible improvements and extensions

  1. Audible Alarm: Connect the base of an NPN transistor to ALARM_OUT to drive a 5 V active buzzer, adding sound to the light.
  2. Latching Alarm: Use the remaining gates in the 74HC00 to build an SR Latch. This would keep the alarm sounding even if the burglar closes the window immediately after entering.

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




Question 2: Under which condition will the alarm remain silent (LED OFF)?




Question 3: What happens if a wire connected to one of the sensors is cut?




Question 4: What logic level represents a closed window in this circuit design?




Question 5: According to the NAND truth table behavior described, when is the Output LOW?




Question 6: This project is an example of what kind of design principle?




Question 7: What is the voltage output when the alarm is triggered (LED ON)?




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




Question 9: What is the state of the output when both switches (windows) are closed?




Question 10: Who is the 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: