AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Term 3 round-up

Week 30 · Review: Term 3 · 3 lessons
2

Review: gates and conditions

Computing 7: Units 7.5–7.6, pp. 150–224
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.

ABA AND BA OR BNOT A
00001
01011
10010
11110
Worked examples
A: collar signalL: night lock onNOT LZ: flap unlocks
0010
0100
1011
1100
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.
Python
1from microbit import *
2temp = 22
3sound = 70
4while True:
5    if temp > 25 and sound < 50:
6        display.show(Image.SMILE)
7    else:
8        display.show(Image.SQUARE)
Output
a square, because temp > 25 is false
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.
ABCB OR CZ = A AND (B OR C)
00000
00110
01010
01110
10000
10111
11011
11111
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 validO: door openNOT VZ: gate beeps
00??
01??
10??
11??
2
What does the board show? Which one change to the condition would make the board show the heart, and why?
Python
1from microbit import *
2score = 6
3lives = 0
4while True:
5    if score >= 5 and lives > 0:
6        display.show(Image.HEART)
7    else:
8        display.show(Image.NO)
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.
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.
TestH: hotW: window closedExpected ZActual ZPass/fail
10000?
20101?
31001?
41111?