KS3 Computing · A logic gate is a tiny circuit. Bits go in, one bit comes out, by a fixed rule. Join millions of gates and a CPU can decide things and do maths.
Explain that a digital signal is a voltage that is either high (1) or low (0).
Build AND, OR and XOR with two buttons and three LEDs, and test all four input pairs.
Fill in a truth table from what the LEDs show.
Explain how the same logic works in code (&&, ||, !=) and in a real gate chip.
Success criteria
I can test all four button combinations and record each LED.
I can say which LEDs light when only button A is pressed.
I can match each line of the code to its gate.
Key words
Say each word, then read what it means. Every one is in the glossary too.
Boolean
A value that is only True or False.
Logic gate
A tiny circuit that takes bits in and gives one bit out by a fixed rule.
Truth table
Lists every input combination and its output.
Boolean expression
A rule written with AND, OR and NOT that gives True or False, like A AND NOT B.
Half adder
An XOR gate plus an AND gate: adds two bits.
Transistor
An electronic switch with no moving parts.
The explanation
Read it here, or open the study page below to go one step at a time with things to try.
Signals: high or low
A signal is a message carried by electricity. A digital signal has only two levels.
Level
Voltage on an Arduino Uno
Means
HIGH
5 V
1, true, on
LOW
0 V
0, false, off
An input pin listens for a signal. digitalRead(2) gives 1 if pin 2 is HIGH, and 0 if it is LOW.
?Why?
Why does each button need a 10 kΩ resistor?
With the button up, the pin is joined to nothing. It can drift between 0 and 1. The resistor gently pulls it down to 0. Pressing the button joins the pin to 5 V, which wins. This is a pull-down resistor.
Three gates, two buttons
Button A goes to pin 2. Button B goes to pin 3. Each LED shows one gate's answer.
A
B
AND (green, pin 10)
OR (blue, pin 11)
XOR (yellow, pin 12)
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0
AND: on only when both buttons are pressed.
OR: on when at least one button is pressed.
XOR: on when the buttons are different.
★Remember
Two inputs make 2² = 4 rows. Test all four, or you have not tested the gate.
The same logic in code, and in a chip
void loop() {
int a = digitalRead(A_PIN); // 1 = pressed, 0 = not pressed
int b = digitalRead(B_PIN);
digitalWrite(AND_LED, a && b); // AND: both
digitalWrite(OR_LED, a || b); // OR: at least one
digitalWrite(XOR_LED, a != b); // XOR: different
delay(200);
}
C++
Read it as
Gate
a && b
a and b
AND
a || b
a or b
OR
a != b
a is not equal to b
XOR
Stage 2 of the build uses no code at all. A 74HC08 chip holds four AND gates, made of transistors.
A 74HC32 chip holds OR gates. A 74HC86 chip holds XOR gates. Their pins are the same, so you can swap them.
!Watch out
A chip needs power too: pin 14 to the + rail and pin 7 to the − rail. Without them it does nothing.
Messages between two boards (micro:bit)
Two micro:bits can send signals to each other by radio. Both must use the same group number, like a channel.
from microbit import *
import radio
radio.on()
radio.config(group=7) # both boards use group 7
while True:
if button_a.was_pressed():
radio.send("A") # send a message
message = radio.receive()
if message == "A": # a message arrived
display.show(Image.YES)
sleep(500)
display.clear()
Put the same program on both boards.
Press A on one board. It sends the message "A".
The other board checks for messages every time round the loop.
When "A" arrives, it shows a tick for half a second.
★Remember
Both boards follow the same rules: the same group and the same messages. That is a protocol.
!Watch out
These radio messages are not encrypted. Any micro:bit on the same group can read them.
e.g.Worked example
Button A is pressed and button B is not. Which LEDs light?
Write the inputs: A = 1, B = 0.
AND needs both to be 1. B is 0, so AND = 0. Green is off.
OR needs at least one 1. A is 1, so OR = 1. Blue is on.
XOR needs them to be different. 1 and 0 are different, so XOR = 1. Yellow is on.
Check on the board: hold A only. Blue and yellow light.