Back

Activity 2.4: Rocket Launch Countdown

divider

Activity 2.4

Rocket Launch Countdown

Key Concepts

One Pin per LED

A Countdown Only Adds

A Loop Repeats What You'd Copy

One Pin Was One Switch

A breadboardOne LED on GPIO 28: its wire in a16, the LED in e16 and e17, and its resistor from a17 straight up into the minus railTwo more LEDs built the same way in columns 19 and 22, on GPIO 29 and GPIO 30

A countdown needs three switches: three pins, three LEDs.

Three Loops, One Ground

Each LED gets its own pin and its own resistor. All three share GND.

Schematic: GPIO 28, 29, and 30 each run through a 330 ohm resistor and an LED to one shared GND

Three pins means three LEDs your code can control separately.

A Countdown Only Adds

led1.value(1)
time.sleep(1)
led2.value(1)
time.sleep(1)
led3.value(1)

What's lit at 0 s, 1 s, and 2 s?

A Countdown Only Adds

At 0 seconds: red lit
At 1 second: red and yellow lit
At 2 seconds: red, yellow, and green lit

Each LED joins the ones already lit. Nothing turns off.

Not a Stoplight

led1.value(1)
time.sleep(1)
led1.value(0)
led2.value(1)
time.sleep(1)
led2.value(0)
led3.value(1)
At 0 seconds: red lit
At 1 second: only yellow lit
At 2 seconds: only green lit

Two extra lines turn a countdown into a stoplight.

Liftoff: Flash Five Times

One flash is eight lines. Five flashes would be forty — unless you repeat them.

Liftoff: Flash Five Times

for i in range(5):
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.2)
led1.value(1)
led2.value(1)
led3.value(1)
time.sleep(0.2)

A for loop runs its indented lines once per number in range(5): five times.

i is never used. The loop is only counting.

Three Lines That Say One Thing

for i in range(5):
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.2)
led1.value(1)
led2.value(1)
led3.value(1)
time.sleep(0.2)

"All off" and "all on" take three lines each. Activity 2.5 gives each one a name.

Today's Objectives

  • Wiring three LEDs, each to their own GPIO pin
  • Building a cumulative sequence with Pin() objects
  • Using a for loop purely to repeat an action

Key Terms

Cumulative
Building up: each step adds to what is already there.
for loop
A loop that runs its indented lines once for each value it counts through.
range(5)
The numbers 0 through 4: five values, so five passes.
Indentation
The spaces that show which lines belong inside the loop.

'F' → Fullscreen

divider

Build

Today the board gets three LEDs, and by Activity 2.6 it will have five. Yesterday's layout took six columns and a jumper for one LED, so you will rebuild it smaller first. The GND wire to the top − rail stays.

Every code block is your whole program, from line 1. The green + lines are what you add for that task. Don't type the + sign.

Unplug the USB cable whenever you change the wiring.


Task 1: Two More Channels

With the USB cable unplugged, take yesterday's LED, resistor, jumper, and GPIO 28 wire off the board. Then build three LEDs, each in two neighboring columns:

PartRed LED (pin 28)Yellow LED (pin 29)Green LED (pin 30)
Wire from the RedBoardGPIO 28 → a16GPIO 29 → a19GPIO 30 → a22
LED (long leg first)e16, e17e19, e20e22, e23
Resistora17 → top − raila20 → top − raila23 → top − rail
A breadboardThree LEDs, each in two neighboring columns: wires from GPIO 28, 29, and 30 land in a16, a19, and a22; the LEDs sit in e16 and e17, e19 and e20, and e22 and e23; and each resistor runs from a17, a20, or a23 straight up into the top minus rail, which the GND wire feeds.

Each LED's whole loop is two columns, with no jumper. The wire feeds the first column, the LED carries the current into the second, and the resistor takes it from there straight up into the − rail, reaching over the + rail without touching it. The resistor comes after the LED this time. That is fine: in a single loop the same current flows through every part, so the resistor limits it from either side.

The RedBoard with GPIO 28, 29, and 30 highlighted together on the top header

28, 29, and 30 sit side by side on the top header.

  • Create a new program, 2-4-launch-countdown, and run it. All three LEDs light at once. If one doesn't, fix its channel before you go on.
2-4-launch-countdown
from machine import Pin
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
led1.value(1)
led2.value(1)
led3.value(1)

Task 2: The Countdown

  • Before you run it, write down which LEDs will be lit at 0 s, 1 s, and 2 s.
  • Add the four new lines and run it. Check your prediction.
2-4-launch-countdown
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
led1.value(1)
time.sleep(1)
led2.value(1)
time.sleep(1)
led3.value(1)
time.sleep(1)

Task 3: Liftoff

  • Add the loop and the last three lines. Run it and count the flashes.
  • Change range(5) to range(10). Predict, then count. Put it back to 5.
2-4-launch-countdown
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
led1.value(1)
time.sleep(1)
led2.value(1)
time.sleep(1)
led3.value(1)
time.sleep(1)
for i in range(5):
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.2)
led1.value(1)
led2.value(1)
led3.value(1)
time.sleep(0.2)
led1.value(0)
led2.value(0)
led3.value(0)

Task 4: Break It on Purpose

Three changes, one at a time. Record what happens, and undo each one before the next.

  1. Make it a stoplight. Add led1.value(0) right before led2.value(1), and led2.value(0) right before led3.value(1). What's lit at 2 s now?
  2. Swap two pin numbers. Make led2 use pin 30 and led3 use pin 29. Which color lights second?
  3. Move one line out of the loop. Delete the four spaces in front of the loop's last time.sleep(0.2), so it looks like this. Count the flashes.
2-4-launch-countdown — the loop, with its last line moved out
for i in range(5):
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.2)
led1.value(1)
led2.value(1)
led3.value(1)
time.sleep(0.2)

None of the three is an error. Each program runs to the end. For each one, say what the code actually told the board to do.


Challenge (Optional): Faster and Faster

  • Make each liftoff flash shorter than the one before. i counts 0, 1, 2, 3, 4 — use it to work out the sleep time.
  • Every sleep has to stay above zero. Check the last pass.
divider

Checkpoint

  • Red lights at 0 s, yellow joins at 1 s, green at 2 s, and none of them turns off
  • All three flash together five times, then everything goes off
  • Your Task 2 prediction is written down next to what you saw
  • All three Task 4 results are recorded
divider

Reflection

Answer the following questions before submitting your work.

  1. Two added lines turned your countdown into a stoplight. Explain what those lines changed, and why a countdown never needs them.
  2. In for i in range(5):, the variable i is never used. Explain what the loop is controlling.
  3. Moving one time.sleep(0.2) out of the loop made the flashes disappear. Explain what the LEDs were actually doing during the loop.
divider

Submit

Submit your 2-4-launch-countdown program file and your completed Activity 2.4 sheet.

Activity Complete