Year 9 · Summer 1

Physical Computing · Lesson 3 of 5

Signals between devices

PracticalTopic 9 Logic gates

Year 9 · Physical Computing · Lesson 3 of 5

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.

Year 9 · Physical Computing · Lesson 3 of 5

Success criteria

You have got it when you can say:

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

Year 9 · Physical Computing · Lesson 3 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    A CPU runs at 2 GHz. How long is one tick?

    A 0.5 nsB 2 nsC 0.2 ns

    A. 0.5 ns

  2. Last unit

    A 10 × 10 black-and-white image needs…

    A 100 bitsB 10 bitsC 20 bits

    A. 100 bits

  3. Earlier this year

    To show change over time, use a…

    A Scatter graphB Pie chartC Line chart

    C. Line chart

Year 9 · Physical Computing · Lesson 3 of 5

Key words (1 of 2)

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.

Year 9 · Physical Computing · Lesson 3 of 5

Key words (2 of 2)

Half adder
An XOR gate plus an AND gate: adds two bits.
Transistor
An electronic switch with no moving parts.

The explanation · step 1 of 4 · part 1 of 2

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.

The explanation · step 1 of 4 · part 2 of 2

Signals: high or low (continued)

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

The explanation · step 2 of 4 · part 1 of 2

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

The explanation · step 2 of 4 · part 2 of 2

Three gates, two buttons (continued)

  • 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 explanation · step 3 of 4 · part 1 of 3

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);
}

The explanation · step 3 of 4 · part 2 of 3

The same logic in code, and in a chip (continued)

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.

The explanation · step 3 of 4 · part 3 of 3

The same logic in code, and in a chip (continued)

!Watch out

A chip needs power too: pin 14 to the + rail and pin 7 to the − rail. Without them it does nothing.

The explanation · step 4 of 4 · part 1 of 4

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.

The explanation · step 4 of 4 · part 2 of 4

Messages between two boards (micro:bit) (continued)

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()

The explanation · step 4 of 4 · part 3 of 4

Messages between two boards (micro:bit) (continued)

  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.

The explanation · step 4 of 4 · part 4 of 4

Messages between two boards (micro:bit) (continued)

!Watch out

These radio messages are not encrypted. Any micro:bit on the same group can read them.

Year 9 · Physical Computing · Lesson 3 of 5

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.

Year 9 · Physical Computing · Lesson 3 of 5

Try it: Play with logic gates

Year 9 · Physical Computing · Lesson 3 of 5

Quick quiz · 1 of 3

How many rows in a truth table with 3 inputs?

  1. A 8
  2. B 6
  3. C 3

A. 8

2³ = 2 × 2 × 2 = 8.

Year 9 · Physical Computing · Lesson 3 of 5

Quick quiz · 2 of 3

Which gate gives 1 only when its two inputs are different?

  1. A NAND
  2. B OR
  3. C XOR

C. XOR

XOR = exclusive OR. 0,1 and 1,0 give 1; 0,0 and 1,1 give 0.

Year 9 · Physical Computing · Lesson 3 of 5

Quick quiz · 3 of 3

In AQA notation, what does A + B mean?

  1. A A add B
  2. B A AND B
  3. C A OR B

C. A OR B

AQA uses + for OR, . for AND, ⊕ for XOR and an overbar for NOT.

Year 9 · Physical Computing · Lesson 3 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A half adder is made from…

    A an XOR gate and an AND gateB a NOT gate and an OR gateC two OR gates

    A. an XOR gate and an AND gate XOR gives the sum bit; AND gives the carry bit.

  2. Q = (A OR B) AND NOT C. With A = 0, B = 1, C = 1, Q is…

    A 0B 1

    A. 0 A OR B = 1. NOT C = 0. 1 AND 0 = 0.