Neon Realm

Day 4: Expanding Horizons – Multiple LEDs and Buttons

Too Much Light

It worked. The factions are talking to each other for the first time in years, coordinated by three lights in an abandoned shop window.

And the lights have not stopped since you switched them on. Orion does the arithmetic out loud: a signal that repeats forever is a signal somebody can sit and study, and Sintech has the patience and the equipment to do exactly that. Sooner or later a constant pattern stops being a message and becomes an address.

"We need to control these signals and send them only when necessary."

You already know the fix, because you built it on Day 2. The difference is that today each light gets its own button — so instead of one message on demand, you can send any of them, independently, at the moment you choose.

Two pairs is enough to prove the idea. Two LEDs, two buttons.

Why Two Resistors Now

Yesterday all three LEDs shared a single resistor, and the price of that was the one-at-a-time rule. Today that rule stops being acceptable: the entire point of independent buttons is that you might hold both at once, and if both LEDs come on through a shared resistor they will split the current and dim each other.

A signal that fades whenever you send two of them is a signal that gets misread. So each LED gets its own 220Ω resistor and its own path to ground, and they stop competing.

Wiring Two Pairs

This is the busiest circuit you have built, and space on the breadboard is genuinely tight, so read the whole list before you start pushing things in. Physical pin numbers and their GP names are both given — keep the pinout below open while you work.

  1. Put both buttons into the centre of the breadboard, straddling the gap, the same way as Day 2.
  2. Put the first LED's longer leg directly into physical pin 21 (GP16).
  3. There is no room to do that twice, so place the second LED anywhere convenient on the board and run a jumper wire from its longer leg to physical pin 22 (GP17).
  4. Connect each LED's shorter leg to the ground rail through its own 220Ω resistor — two LEDs, two resistors. Make sure the ground rail still reaches a GND pin on the Pico.
  5. Wire one leg of each button to physical pins 24 and 25 (GP18 and GP19) using jumper wires.
  6. Wire the opposite leg of both buttons to the breadboard's power rail, and connect that rail to the Pico's 3.3 volt pin — physical pin 36, the same one you used on Day 2. Both buttons can share it.

Pins 21, 22, 24, 25 and 36 all live on the same side of the board.

Four pins for four components, plus power and ground. Count them off against the diagram before you plug the cable back in — it is much easier to find a mistake now than to work backwards from an LED that will not light.

Two Conversations at Once

The imports are the full set: machine, Pin pulled out of it for brevity, and time for a delay you will add at the end.

import machine
from machine import Pin
import time

Then four names instead of two — two outputs and two inputs, each with the same pull-down you used on Day 2 to stop an unpressed button from floating.

led1 = Pin(16, Pin.OUT)
led2 = Pin(17, Pin.OUT)

button1 = Pin(18, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(19, Pin.IN, Pin.PULL_DOWN)

The loop is Day 2's if/else, written out twice. That is not a shortcut or a trick — it is genuinely how you do it. Copy the first block, paste it underneath, and change the 1s to 2s.

import machine
from machine import Pin
import time

led1 = Pin(16, Pin.OUT)
led2 = Pin(17, Pin.OUT)

button1 = Pin(18, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(19, Pin.IN, Pin.PULL_DOWN)

while True:
    if button1.value() == 1:
        led1.on()
    else:
        led1.off()

    if button2.value() == 1:
        led2.on()
    else:
        led2.off()

    time.sleep(0.1)

The time.sleep(0.1) at the bottom is worth a moment. Nothing about the circuit needs it — the program works without it — but running the loop flat out means re-reading both buttons tens of thousands of times a second, and a mechanical button does not close cleanly. Its contacts bounce for a few thousandths of a second on the way in. Checking a tenth of a second apart steps over all of that, and a tenth of a second is far too short for you to notice.

Note that the two if