Skip to the page
Year 9 · Summer 1 · Physical Computing

Signals between devices

PracticalPresent ▶

What this lesson covers

Topic 9 Logic gates

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.

Computer Systems ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • 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.

LevelVoltage on an Arduino UnoMeans
HIGH5 V1, true, on
LOW0 V0, 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.

ABAND (green, pin 10)OR (blue, pin 11)XOR (yellow, pin 12)
00000
01011
10011
11110
  • 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 asGate
a && ba and bAND
a || ba or bOR
a != ba is not equal to bXOR

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()
  1. Put the same program on both boards.
  2. Press A on one board. It sends the message "A".
  3. The other board checks for messages every time round the loop.
  4. 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?

  1. Write the inputs: A = 1, B = 0.
  2. AND needs both to be 1. B is 0, so AND = 0. Green is off.
  3. OR needs at least one 1. A is 1, so OR = 1. Blue is on.
  4. XOR needs them to be different. 1 and 0 are different, so XOR = 1. Yellow is on.
  5. Check on the board: hold A only. Blue and yellow light.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Try it

Build it

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.