GoalComplete truth tables, trace circuits and use and, or and not in a MicroPython condition.
New words
logic gate · a part of a circuit that takes one or two inputs of 1 or 0 and gives one outputtruth table · a table that lists every combination of inputs and the output for eachBoolean operator · a word such as and, or and not that joins or reverses true/false conditionslogic circuit · several logic gates joined together, with the output of one gate used as an input of the next
Explanation
A logic gate follows a rule. An AND gate gives 1 only when both inputs are 1. An OR gate gives 1 when at least one input is 1. A NOT gate reverses its input, so 1 becomes 0 and 0 becomes 1. A truth table lists every combination of inputs, so a circuit with two inputs has four rows and a circuit with three inputs has eight. To work out a circuit, find the answer of the inner gate first and then use it in the next gate. In MicroPython the same ideas are written as and, or and not inside an if line. A test plan for a circuit takes its expected outcomes from the truth table. The word and makes a rule stricter, because both checks must be true, while or makes it looser.
A
B
A AND B
A OR B
NOT A
0
0
0
0
1
0
1
0
1
1
1
0
0
1
0
1
1
1
1
0
Worked examples
A: collar signal
L: night lock on
NOT L
Z: flap unlocks
0
0
1
0
0
1
0
0
1
0
1
1
1
1
0
0
A pet flap unlocks when the collar signal is received (A = 1) and the night lock is not on (L = 1 means the lock is on). The NOT gate turns L round first, and then the AND gate combines the two. Only one row gives Z = 1: the signal is received and the lock is off.
The same idea in MicroPython. Both checks must be true to show the smile. Here temp is 22, so temp > 25 is false, and with and the whole condition is false. The else branch runs and the board shows the square. Even with sound = 40 the board would still show the square.
A
B
C
B OR C
Z = A AND (B OR C)
0
0
0
0
0
0
0
1
1
0
0
1
0
1
0
0
1
1
1
0
1
0
0
0
0
1
0
1
1
1
1
1
0
1
1
1
1
1
1
1
A circuit with three inputs. A desk lamp comes on if the desk is in use (A) and either the room is dark (B) or the day is cloudy (C). The OR gate works first, and its answer goes into the AND gate. When A is 0 the lamp is always off, whatever B and C are.
Class activity
Stand-up circuit (no computer): three players are A, B and C. A player stands up for 1 and sits down for 0. A fourth player is the OR gate for B and C: this player stands up if at least one of them is standing. A fifth player is the AND gate for A and the OR player: this player stands up only if both are standing. Try all eight combinations and record Z in a truth table. Then change the circuit so that the fifth player is an OR gate and see which rows change. If you have a computer or tablet, you can draw your truth table in a spreadsheet.
Practice
1
A school gate beeps when the card is not valid (V = 0) or the door is open (O = 1). The circuit is Z = NOT V OR O. Complete the table.
V: card valid
O: door open
NOT V
Z: gate beeps
0
0
?
?
0
1
?
?
1
0
?
?
1
1
?
?
V = 0, O = 0: NOT V = 1, Z = 1; V = 0, O = 1: NOT V = 1, Z = 1; V = 1, O = 0: NOT V = 0, Z = 0; V = 1, O = 1: NOT V = 0, Z = 1. The gate stays quiet only when the card is valid and the door is shut.
2
What does the board show? Which one change to the condition would make the board show the heart, and why?
lives > 0 is false (lives is 0), so with and the whole condition is false and the board shows the cross (Image.NO). Changing and to or would show the heart, because score >= 5 is true and only one check is needed. Changing lives to 1 would also work.
3
Write the if line for each rule. (a) A tick shows when score is from 20 to 29, including both. (b) A cross shows when button A is not pressed. (c) A heart shows when button A or button B is pressed.
(a) if score >= 20 and score <= 29: (b) if not button_a.is_pressed(): (c) if button_a.is_pressed() or button_b.is_pressed():
4
A greenhouse fan should run only when it is hot (H = 1) and the window is closed (W = 1). A faulty circuit used the wrong gate. Complete the Pass/fail column and say which gate was used by mistake.
Test
H: hot
W: window closed
Expected Z
Actual Z
Pass/fail
1
0
0
0
0
?
2
0
1
0
1
?
3
1
0
0
1
?
4
1
1
1
1
?
Test 1 passes (0 and 0). Test 2 fails (expected 0, actual 1). Test 3 fails (expected 0, actual 1). Test 4 passes (1 and 1). The actual outcomes follow an OR gate, but the plan needs an AND gate.