Logic levels and basic gates
Objectives
Section titled “Objectives”By the end of this lesson you will:
- Read a chip’s input and output logic thresholds from its datasheet, and decide whether two devices can connect directly
- Write truth tables for AND, OR, NOT and XOR gates, and use them to solve simple conditional problems
Before you start
Section titled “Before you start”- You can already calculate a voltage divider (lesson Voltage dividers)
- For the lab: a TESAIoT Dev Kit flashed with the QWA309 Header I/O Test example, a multimeter, a breadboard, a 1 kΩ resistor, a 2.2 kΩ resistor, two 4.7 kΩ resistors, three 10 kΩ resistors (or one 10 kΩ potentiometer in place of the resistor set)
- If using the Eva Kit, write your own short program that sets a spare pin as an input and prints what it reads, in place of the header test program
See it work first
Section titled “See it work first”The TESAIoT Dev Kit has at least two “voltage worlds” living on a single board.
- The on-board sensor’s I2C bus runs at 1.8 V (SCB0, pin P8.0 = SCL, P8.1 = SDA, at 400 kHz, per the description in the SDK’s
cm33/sensors/01_i2c_bus_scan.cexample). The same example notes that MicroPython’smachine.I2Cis a different bus, at 3.3 V, meant for connecting an external sensor board. - The header pins run at 3.3 V — you can see this in the signal names the header test program prints, such as
P13.3_GPIO_PWM5+_3V3, where the trailing_3V3states the voltage domain.
Suppose you have a sensor board that runs at 3.3 V — which bus can it connect to, and what happens if you connect it to the wrong one? This lesson gives you the numbers to answer that.
Concepts
Section titled “Concepts”1. Which voltages count as 0 or 1
Section titled “1. Which voltages count as 0 or 1”A digital chip does not just see “power or no power.” It compares voltage against four thresholds guaranteed by the datasheet.
| Value | Meaning |
|---|---|
| V_OH(min) | An output driven to 1 will give at least this voltage |
| V_OL(max) | An output driven to 0 will give no more than this voltage |
| V_IH(min) | An input is guaranteed to read 1 when the voltage is at or above this |
| V_IL(max) | An input is guaranteed to read 0 when the voltage is no more than this |
Between V_IL and V_IH is an unguaranteed range — an input may read anything there, and may bounce around if there is any noise.
Many CMOS chips use thresholds close to 0.7 × VDD and 0.3 × VDD; the figure above uses this rule of thumb. At 3.3 V, that gives V_IH = 2.31 V, V_IL = 0.99 V. At 1.8 V, it gives V_IH = 1.26 V, V_IL = 0.54 V. But every chip model differs, and some let you choose the threshold — the value you actually decide with must always come from the datasheet.
Noise margin tells you how much room there is for noise before a 1 turns into an uncertain value.
NM_H = V_OH(min) − V_IH(min)NM_L = V_IL(max) − V_OL(max)If either comes out negative, the two devices cannot connect directly.
2. Can two devices connect to each other?
Section titled “2. Can two devices connect to each other?”Use these example values from two devices’ assumed datasheets.
| Value | Device A (1.8 V) | Device B (3.3 V) |
|---|---|---|
| V_OH(min) | 1.35 V | 2.9 V |
| V_OL(max) | 0.45 V | 0.4 V |
| V_IH(min) | 1.26 V | 2.31 V |
| V_IL(max) | 0.54 V | 0.99 V |
| Maximum tolerated input voltage (absolute maximum) | 2.1 V | 3.6 V |
A driving B: A’s level 1 can be as low as 1.35 V, but B needs at least 2.31 V. NM_H = 1.35 − 2.31 = −0.96 V, which is negative. They cannot connect directly — B would read A’s 1 as an uncertain value (in fact, even A’s full 1.8 V is still below B’s 2.31 V).
B driving A: logically, 2.9 V is above A’s V_IH (1.26 V), so it reads comfortably as 1. But 2.9 to 3.3 V exceeds the maximum voltage A’s pin can tolerate (2.1 V). Current will flow through A’s protection diode into A’s own 1.8 V supply rail (back-powering) — the pin can be damaged, or the chip can behave strangely even before it has been powered on itself. This is the most dangerous trap of all, because “it reads correctly” on day one.
B to B: NM_H = 2.9 − 2.31 = 0.59 V and NM_L = 0.99 − 0.4 = 0.59 V. Both positive — this connects directly, fine.
Fixes when direct connection is not possible:
- A level-translator chip. Works for fast signals, available in both directional and bidirectional versions.
- An open-drain bus, like I2C, uses a MOSFET-based level shifter or a dedicated chip, with a separate pull-up resistor on each side.
- A slow, one-direction, high-to-low signal can use a voltage divider — for example, 10 kΩ and 12 kΩ, converting 3.3 V to 1.80 V. But R_th = 10 kΩ ∥ 12 kΩ = 5.45 kΩ, together with a pin capacitance of about 10 pF, gives τ ≈ 55 ns — the edges slow down. This works for signals up to a few hundred kHz, but not for high-speed SPI.
Back to the opening question: a 3.3 V sensor board should connect to the 3.3 V bus, which needs no level shifting at all. If it must connect to the 1.8 V bus, a level-shifting circuit is required.
3. Basic gates and truth tables
Section titled “3. Basic gates and truth tables”Once a signal is clearly 0 or 1, we combine them with logic gates.
| A | B | NOT A | A AND B | A OR B | A XOR B |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 0 |
- AND is 1 when every input is 1
- OR is 1 when at least one input is 1
- NOT inverts a value
- XOR is 1 when the two inputs differ — used for change detection or bit flipping
- NAND and NOR are AND and OR each followed by a NOT
In C, use &&, ||, ! for conditions and &, |, ~, ^ for bits. In MicroPython, use and, or, not and &, |, ^.
A real case on the board: the header test program has a PWM Out button that drives pins P13.3 and P13.4 as a phase-inverted pair, then tells you to check for “no both-high fault.”
This fault condition is P13.3 AND P13.4 — whenever this is 1, something is wrong. The condition that the pair is working correctly is P13.3 XOR P13.4, which must be 1 throughout the time the signal is running.
An active-low button: its pin reads 0 when pressed. So “pressed” = NOT pin. We will actually wire this in the next lesson.
Worked example
Section titled “Worked example”Problem: a cooling fan must run when “temperature is high and the cabinet door is closed,” or when the user commands it manually. Let T = temperature high, D = door closed, M = manual command. Write the truth table, then turn it into code.
FAN = (T AND D) OR M| T | D | M | T AND D | FAN |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 |
Check the table: every row with M = 1 must run the fan (four rows), and when M = 0 there is exactly one row that runs it, T = 1 and D = 1 — five rows in total, matching the table.
If the door switch is wired active-low (closed reads as 0), the code must invert it before use.
bool door_closed = !door_pin; /* active-low: reads 0 when closed */bool fan_on = (temp_high && door_closed) || manual;Three inputs give 2³ = 8 combinations. Writing out every row is the simplest way to test this. Before turning it into code, try walking through every row in your head first.
Practice
Section titled “Practice”- An output has V_OH(min) = 2.4 V and V_OL(max) = 0.4 V, connected to a TTL input with V_IH(min) = 2.0 V and V_IL(max) = 0.8 V. Find NM_H and NM_L. Can they connect directly?
- Using the rule of thumb 0.7 × VDD and 0.3 × VDD, find V_IH and V_IL for an input powered at 2.5 V.
- A 5 V device sends a 5 V signal into a 3.3 V microcontroller pin whose datasheet does not state 5 V tolerance. Can they connect directly? Why or why not?
- Write the truth table of
NOT (A OR B)(that is, NOR), and compare it against(NOT A) AND (NOT B). Notice something? - A burglar alarm sounds when “the system is armed (ARM) and the door is open (OPEN) and it is not in maintenance mode (MAINT).” Write the expression, and say how many of the eight rows make it sound.
- A circuit checking a phase-inverted pair A and B needs a signal ERR that is 1 when A and B have the same value. Write ERR using the gates you have learned.
Solution
Section titled “Solution”- NM_H = 2.4 − 2.0 = 0.4 V, and NM_L = 0.8 − 0.4 = 0.4 V. Both positive, so they connect directly.
- V_IH = 0.7 × 2.5 = 1.75 V, and V_IL = 0.3 × 2.5 = 0.75 V.
- No. 5 V exceeds the maximum most 3.3 V pins tolerate (usually around VDD + 0.3 V). Current would flow through the protection diode into the supply rail. Use a level translator, or a voltage divider if the signal is slow and one-directional.
- Both expressions give 1, 0, 0, 0 for (A, B) = (0, 0), (0, 1), (1, 0), (1, 1) — identical on every row. This is De Morgan’s law.
ALARM = ARM AND OPEN AND (NOT MAINT). Exactly one row sounds it: ARM = 1, OPEN = 1, MAINT = 0.ERR = NOT (A XOR B)(also called XNOR). It is 1 when A and B are equal, which covers both being 1 together (both-high) and both being 0 together.
Check your understanding
Section titled “Check your understanding”Answer at least 4 of the 5 questions in quiz.yaml correctly. Questions 1 through 3 use the values table for devices A and B in section 2.
Part A: measure real output levels with a multimeter
- Run the QWA309 Header I/O Test program. Find pins P13.0 and GND on the header from the board’s pinout diagram.
- Set the meter to DC voltage mode, black lead at GND, red lead at P13.0, then press the GPIO Out button on screen. The program drives each pin in turn (P13.0, P13.3, P13.4, P13.5, P13.6, P13.7), about 0.7 s each, then turns everything off, and repeats this three times.
- Record the voltage while P13.0 is 1, and while it is 0 — these are the pin’s real V_OH and V_OL, unloaded.
- Use the table in section 2 to decide whether this pin can drive device A (1.8 V) and device B (3.3 V), with your reasoning.
Part B: find the real switching point of an input
- With USB unplugged, wire a voltage divider on the breadboard from 3V3, with its midpoint connected through a 1 kΩ resistor to pin P13.0 (the 1 kΩ resistor protects the pin in case GPIO Out is pressed accidentally while this circuit is connected).
- Press the GPIO In button. The program sets all six pins as high-Z inputs (no pull), reads every 100 ms for 8 s, and prints the mask value every time it changes. Bit 0 is P13.0. Other bits may bounce around because those pins are floating — that is next lesson’s topic. For now, watch only bit 0.
- Change the resistor pair (top / bottom) per the table, measure the midpoint voltage with the meter, then press GPIO In and read bit 0.
| Top / bottom | Calculated voltage | Measured voltage | Bit 0 read |
|---|---|---|---|
| 10 kΩ / 2.2 kΩ | 0.60 V | ||
| 10 kΩ / 4.7 kΩ | 1.06 V | ||
| 10 kΩ / 10 kΩ | 1.65 V | ||
| 4.7 kΩ / 10 kΩ | 2.24 V | ||
| 2.2 kΩ / 10 kΩ | 2.70 V |
If you have a potentiometer, use it instead of the resistor set, and slowly turn it to find the voltage where bit 0 changes, in both directions (up and down). If these two values differ, the input has hysteresis.
Interpreting the results: the datasheet only guarantees that below V_IL reads 0 and above V_IH reads 1. The real switching point on your specific board sits somewhere in between, and may vary with temperature or between individual chips. Never design a circuit relying on a switching point measured from a single board — always design from the V_IL and V_IH values in the datasheet.
Going further
Section titled “Going further”The next lesson, Pull-ups, pull-downs and push buttons, looks at why a floating input pin reads garbage, wires an active-low button, and uses a logic analyzer for the first time to see a button’s bounce.
Reflect
Section titled “Reflect”If a friend told you “I connected 3.3 V to a 1.8 V pin and it read fine,” how would you explain the risk to them, using the numbers from this lesson?
References
Section titled “References”- Logic gate (Wikipedia)
- Lessons In Electric Circuits by Tony R. Kuphaldt (open book)
- Logic level (Wikipedia)
- SDK: cm33/sensors/01_i2c_bus_scan.c (the 1.8 V sensor bus and machine.I2C’s 3.3 V bus)
- README of QWA309 Header I/O Test (Developer Hub, commit 372d0d8)
Review questions
Answer on your own first, then open the answer.
-
A 1.8 V device output (V_OH(min) = 1.35 V) drives a 3.3 V input (V_IH(min) = 2.31 V). Can they connect directly? (Objective 1)
- ได้ เพราะ 1.35 V มากกว่าศูนย์
- ไม่ได้ ระดับ 1 ของขาออกต่ำกว่า V_IH จึงไม่รับประกันว่าจะอ่านเป็น 1
- ได้ ถ้าใส่ตัวต้านทาน 1 kΩ อนุกรม
- ไม่ได้ เพราะ V_OL สูงเกินไป
Show answer
Answer: B. ไม่ได้ ระดับ 1 ของขาออกต่ำกว่า V_IH จึงไม่รับประกันว่าจะอ่านเป็น 1
NM_H = 1.35 − 2.31 = −0.96 V ติดลบ ขาเข้าอาจอ่านเป็นอะไรก็ได้ ต้องใช้ตัวแปลงระดับ ตัวต้านทานอนุกรมไม่ได้ช่วยยกแรงดัน
-
A 3.3 V output drives a pin of a 1.8 V chip whose absolute maximum input is 2.1 V. Which is true? (Objective 1)
- ใช้ได้ปลอดภัย เพราะ 3.3 V สูงกว่า V_IH จึงอ่านเป็น 1 ได้ถูก
- อ่านค่าได้ถูกก็จริง แต่แรงดันเกินค่าสูงสุดที่ขาทน อาจเสียหายหรือจ่ายไฟย้อนเข้าชิป ต้องแปลงระดับ
- อ่านไม่ได้เลยเพราะแรงดันสูงเกิน
- ปลอดภัยถ้าชิป 1.8 V ปิดไฟอยู่
Show answer
Answer: B. อ่านค่าได้ถูกก็จริง แต่แรงดันเกินค่าสูงสุดที่ขาทน อาจเสียหายหรือจ่ายไฟย้อนเข้าชิป ต้องแปลงระดับ
ในแง่ลอจิกอ่านเป็น 1 ได้ แต่ 3.3 V เกิน 2.1 V กระแสจะไหลผ่านไดโอดป้องกันเข้าไฟเลี้ยง 1.8 V ถ้าชิปปิดไฟอยู่ยิ่งแย่ เพราะมันจะถูกเลี้ยงผ่านขานั้นแทน
-
An output has V_OH(min) = 2.9 V and an input has V_IH(min) = 2.31 V. What is the high noise margin? (Objective 1)
- 0.59 V
- 5.21 V
- 0.99 V
- −0.59 V
Show answer
Answer: A. 0.59 V
NM_H = V_OH(min) − V_IH(min) = 2.9 − 2.31 = 0.59 V สัญญาณรบกวนที่กดแรงดันลงไม่เกินนี้ยังอ่านเป็น 1 ได้
-
An XOR gate has A = 1 and B = 1. What is the output? (Objective 2)
- 0
- 1
- ไม่แน่นอน
- เท่ากับ A AND B
Show answer
Answer: A. 0
XOR เป็น 1 เมื่อขาเข้าต่างกันเท่านั้น A และ B เท่ากันจึงได้ 0 ส่วน A AND B ในกรณีนี้ได้ 1
-
ALARM = ARM AND OPEN AND (NOT MAINT). Which input combination sounds the alarm? (Objective 2)
- ARM = 1, OPEN = 1, MAINT = 1
- ARM = 1, OPEN = 1, MAINT = 0
- ARM = 0, OPEN = 1, MAINT = 0
- ARM = 1, OPEN = 0, MAINT = 0
Show answer
Answer: B. ARM = 1, OPEN = 1, MAINT = 0
AND ต้องการให้ทุกเทอมเป็น 1 เทอม NOT MAINT เป็น 1 เมื่อ MAINT = 0 จึงมีเพียงแถวเดียวคือ ARM = 1, OPEN = 1, MAINT = 0
Cite this lesson
If you teach from this lesson or reuse it in slides or documents, credit it with the text below. If you changed it, add (adapted) after the title.
"Logic levels and basic gates" from TESA Open Knowledge by the Thai Embedded Systems Association (TESA), https://github.com/tesaiot/tesa-qualification-program, licensed under CC BY-NC 4.0
Thai attribution: "ระดับลอจิกและเกตพื้นฐาน" จาก TESA Open Knowledge โดยสมาคมสมองกลฝังตัวไทย (Thai Embedded Systems Association: TESA) https://github.com/tesaiot/tesa-qualification-program สัญญาอนุญาต CC BY-NC 4.0
TESA Open Knowledge · © 2026 สมาคมสมองกลฝังตัวไทย (TESA) · CC BY-NC 4.0
Content is licensed CC BY-NC 4.0. Reuse it non-commercially and credit the Thai Embedded Systems Association (TESA) every time. · How to cite TESA