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

Lights and the first program

PracticalPresent ▶

What this lesson covers

Topic 1 What a computer is

KS3 Computing · A computer takes information in, follows instructions to change it, gives a result out, and stores things for later. It does all of this with billions of tiny on/off switches.

Computer Systems ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • Explain current, voltage and resistance, and why current needs a complete loop.
  • Build a circuit in Tinkercad where a button turns an LED on.
  • Calculate the resistor an LED needs with Ohm's law.
  • Write Blink in blocks and in C++, and explain setup(), loop(), digitalWrite and delay.

Success criteria

  • I can build the button circuit with the LED the right way round.
  • I can work out (5 − 2) ÷ 0.02 = 150 Ω, and say why 220 Ω is also safe.
  • I can change Blink so the LED flashes twice as fast.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Input / output
Anything that gets information into the computer. Keyboard, mouse, touchscreen, microphone, camera, a temperature sensor.
Process
The computer follows instructions to change that information. Add two numbers. Move a character. Sort a list. The CPU does this.
Transistor
An electronic switch with no moving parts.
Hardware
The physical parts of a computer.
Software
Programs: instructions that tell hardware what to do.
Embedded system
A small computer built into a device for one job.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Electricity flows in a loop

Electricity only flows round a complete loop, called a circuit. Break the loop anywhere and it stops.

WordWhat it meansUnit
Voltage (V)The push that drives electricity round the circuitvolts (V)
Current (I)How much electricity flows past a point each secondamps (A). 1000 milliamps (mA) = 1 A.
Resistance (R)How hard it is for current to flowohms (Ω)

On an Arduino, 5V is the + side. GND (ground) is the − side, at 0 volts.

★Remember

Current flows from +, round the loop, back to −. Every part must sit in that loop.

The parts

  • Arduino Uno: a small board with a microcontroller, a tiny computer on one chip. Wires join it at its numbered pins.
  • Breadboard: a board for building circuits without solder. The long + and − rails carry power along its edges.
  • LED (light-emitting diode): a small light that lets current through one way only.
  • Resistor: limits the current. It works either way round.
  • Pushbutton: a switch. Pressed, it closes the loop. Let go, it breaks the loop.

An LED's anode (+) must face the + side. Its cathode (−) faces the − side.

On a real LED the anode is the longer leg. In Tinkercad it is the bent leg.

!Watch out

The 5 holes in one short column of the breadboard are joined inside. If a part has both legs in one column, the current skips it.

Why an LED needs a resistor

With no resistor, too much current flows. The LED burns out, and the Arduino pin can be damaged.

Ohm's law links the three words: V = I × R. Turned round, it tells you the resistor you need.

SymbolMeansFor a red LED
VsThe supply voltage5 V
VfThe voltage the LED uses upabout 2 V
IThe current you want, in amps20 mA = 0.02 A
RThe resistor you need: R = (Vs − Vf) ÷ I?
  1. Take the LED's share away from the supply: 5 − 2 = 3 V.
  2. Divide by the current: 3 ÷ 0.02 = 150 Ω.
  3. Use 150 Ω, or the next size up. Kits use 220 Ω.
?Why?

Why is 220 Ω also safe?

A bigger resistor lets less current through: 3 ÷ 220 ≈ 0.014 A, or 14 mA. The LED is a little dimmer, and safer.

★Remember

Keep the current from each Arduino pin at about 20 mA or less.

Blink: the first program

Wire pin 13 to the resistor instead of the button. Now the Arduino flips the switch.

const int LED_PIN = 13;        // the LED is wired to pin 13

void setup() {
  pinMode(LED_PIN, OUTPUT);    // pin 13 will send electricity out
}

void loop() {
  digitalWrite(LED_PIN, HIGH); // switch ON: 5 volts, a 1
  delay(500);                  // wait 500 milliseconds
  digitalWrite(LED_PIN, LOW);  // switch OFF: 0 volts, a 0
  delay(500);                  // wait again, then loop() starts over
}
  1. setup() runs once, when the board switches on. pinMode makes pin 13 an output.
  2. loop() runs again and again, for ever.
  3. digitalWrite(LED_PIN, HIGH) puts 5 V on the pin. That is a 1, so the LED lights.
  4. delay(500) waits 500 milliseconds: half a second.
  5. LOW puts 0 V on the pin. That is a 0, so the LED goes off.

With a micro:bit, the same idea in MicroPython uses the screen of lights:

from microbit import *

while True:
    display.show(Image.HEART)
    sleep(500)
    display.clear()
    sleep(500)
e.g.Worked example

A blue LED uses about 3 V. You want 20 mA through it from the Arduino's 5 V. Which resistor do you need?

  1. Write what you know: Vs = 5 V, Vf = 3 V, I = 20 mA.
  2. Change the current to amps: 20 ÷ 1000 = 0.02 A.
  3. Take the LED's share away: 5 − 3 = 2 V.
  4. Divide by the current: 2 ÷ 0.02 = 100 Ω.
  5. 100 Ω is a standard size, so use it.
  6. A 220 Ω resistor also works: 2 ÷ 220 ≈ 0.009 A, or 9 mA. The LED is just dimmer.

Study it

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

The steps for this lesson

The maths

Symbols first, then the words, then the equation. Change the numbers to try your own.

Build it

Check yourself

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

Saved on this device.