beginner

Digital Logic and Logic Levels

Bridge analog electronics and digital systems: logic levels and noise margins, 3.3 V and 5 V families, gates and truth tables, binary numbers, pull-up and pull-down resistors, switch debouncing, level shifting, and open-drain outputs.

60 min

1. Two levels, with a gap

Digital circuits treat a voltage as one of two values: high (1) and low (0). Real signals are analog, so each logic family defines thresholds:

Family Supply Reads as LOW Reads as HIGH Typical output
5 V CMOS (74HC at 5 V) 5 V below about 1.5 V (0.3 × VCC) above about 3.5 V (0.7 × VCC) near 0 V or 5 V
3.3 V CMOS 3.3 V below about 1.0 V above about 2.3 V near 0 V or 3.3 V
5 V TTL (74LS) 5 V below 0.8 V above 2.0 V low 0.4 V, high 2.4 V

Between the thresholds is the undefined zone: the chip may read either value, and may waste power. The difference between what a gate outputs and what the next gate needs is the noise margin, which lets small noise pass harmlessly.

2. Logic gates and truth tables

Gates are the basic building blocks. A truth table lists the output for each input combination (inputs A, B listed as 00, 01, 10, 11):

Gate Rule Output for 00, 01, 10, 11
AND 1 only if both inputs are 1 0, 0, 0, 1
OR 1 if either input is 1 0, 1, 1, 1
NOT The opposite of the input 1 for 0, 0 for 1
NAND NOT of AND 1, 1, 1, 0
NOR NOT of OR 1, 0, 0, 0
XOR 1 if the inputs differ 0, 1, 1, 0

NAND (or NOR) alone can build any other logic function, so it is called a universal gate. Typical chips: 74HC00 (four NANDs), 74HC08 (AND), 74HC32 (OR), 74HC04 (six inverters). Unused inputs must be tied high or low and never left floating.

3. Binary numbers

Computers represent numbers with bits. Each position is a power of two:

Bit position 7 6 5 4 3 2 1 0
Value 128 64 32 16 8 4 2 1
  • 1010 = 8 + 2 = 10. 0101 = 4 + 1 = 5. 11111111 = 255.
  • n bits give 2ⁿ values: 8 bits = 256 (0 to 255), 10 bits = 1,024, 12 bits = 4,096.
  • Hexadecimal groups four bits: 0xFF = 255, 0x0A = 10. It is a compact way to write binary.
  • Converting decimal to binary: repeatedly divide by 2 and read the remainders, or subtract powers of two.

4. Pull-ups, pull-downs and buttons

A switch connects a pin to ground or the supply, but when it is open the pin is floating and reads noise. Add a resistor to set the idle level:

        +3.3 V                         Pin ---+---- [button] ---- GND
          |                                    |
       [10 kΩ]  pull-up               [10 kΩ] to +3.3 V
          |
 Pin -----+-----[button]---- GND
  • Pull-up: idle high, button pulls the pin low when pressed. This is the most common wiring, and most microcontrollers have internal pull-ups you can enable in software.
  • Pull-down: idle low, button pulls the pin high.
  • Value: 10 kΩ is typical. When the button is pressed the current is V / R = 3.3 / 10,000 = 0.33 mA. Too large a resistor makes the line slow and noise-prone; too small wastes current.
  • Debouncing: a mechanical switch bounces for a few milliseconds, creating several edges for one press. Fix it with an RC filter (10 kΩ with 100 nF has tau = 1 ms), a Schmitt-trigger input, or software that ignores changes for 10 to 50 ms.

5. Connecting different logic levels

  • 3.3 V to 5 V input: a 3.3 V output (about 3.3 V) usually meets a 5 V CMOS input’s V_IH of 3.5 V poorly. Use a level shifter or a 74HCT part, whose inputs accept TTL levels.
  • 5 V to 3.3 V input: do not connect directly unless the pin is 5 V tolerant. Use a resistor divider (for example 1.8 kΩ and 3.3 kΩ in series, tap in the middle) for slow one-way signals, or a level shifter chip (for I²C use a bidirectional MOSFET shifter).
  • Open-drain and open-collector outputs only pull low and need an external pull-up resistor to a rail. That lets several devices share one line (I²C) and lets you pull up to a different voltage for level shifting.
  • Always share a common ground between the devices.

6. On your bench

  1. Wire a push button to a microcontroller pin with and without a pull-up. Read the pin in a simple sketch and watch the floating input flicker.
  2. Build an AND and a NAND function from a 74HC00, and write out the truth table by testing every input combination with switches and an LED.
  3. Count in binary on four LEDs. Convert numbers between binary and decimal on paper and check with your results.
  4. Measure the bounce of a real button with an oscilloscope (the next lesson) and see how an RC filter changes it.
  5. Measure the actual logic levels of a 3.3 V pin driving high and low with a multimeter.

7. Hands-on lab: logic and levels

Gates, binary and logic levels — Python 3
WHAT TO DO
gate(name, a, b): the output (0 or 1) of the named gate for inputs a and b. Names: "AND", "OR", "NAND", "NOR", "XOR" and "NOT" (NOT uses only a). truth_table(name): the outputs of a two-input gate for the inputs (0,0), (0,1), (1,0), (1,1) in that order, as a list of four numbers. logic_level(v, vcc): "high" when v is at least 0.7 times vcc, "low" when v is at most 0.3 times vcc, otherwise "undefined". to_binary(n, bits): n as a binary string padded with zeros to bits digits. from_binary(s): the whole number for a binary string. pullup_current_ma(vcc, r_ohm): the current drawn through the pull-up when the button is pressed, in milliamps, rounded to 2 decimal places.
YOUR CODE (edit here, then press Run)
Ctrl/⌘ + Enter
OUTPUT

8. Common mistakes

Mistake Better
Leaving an input floating Tie it high or low, or use a pull resistor
Connecting 5 V outputs to 3.3 V inputs Level shift, or check for 5 V tolerance
No debounce on mechanical switches RC filter, Schmitt trigger or software
Pull-up resistor far too small or large 4.7 to 10 kΩ is typical
Ignoring the undefined zone Respect V_IL and V_IH from the datasheet
Forgetting the common ground Connect grounds between all devices

9. Key takeaways

  • Digital circuits read high or low against thresholds (about 0.7 and 0.3 of the supply for CMOS); the gap is undefined.
  • AND, OR, NOT, NAND, NOR, XOR are described by truth tables; NAND and NOR are universal.
  • Binary uses powers of two; n bits give 2ⁿ values.
  • Use pull-ups or pull-downs (about 10 kΩ) for inputs and debounce mechanical switches.
  • Level-shift between 3.3 V and 5 V parts, and use pull-ups on open-drain lines.
Your 3.3 V microcontroller reads an input from a 5 V sensor. What can go wrong and how do you fix it?

The 5 V signal exceeds the 3.3 V pin’s maximum rating, which can damage the pin over time. Fix it with a level shifter, or for a slow one-way signal a divider such as 1.8 kΩ over 3.3 kΩ (giving 5 × 3.3 / 5.1 = 3.2 V), or choose a 5 V tolerant pin if the datasheet allows it.

Can You Explain It?

Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.

  • Explain logic thresholds, the undefined zone and noise margin.
  • Write the truth tables for AND, OR, NAND, NOR and XOR and explain why NAND is universal.
  • Describe pull-up resistors and debouncing for a push button.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Digital Logic and Logic Levels. What would you like to know?
📝

Knowledge Check

1 of 4

Which input is guaranteed to be read as a logic high by a 3.3 V CMOS input that requires at least 0.7 × VCC?

Official References

Report a problem

About: /lessons/efc-digital-logic-and-logic-levels