Neon Realm

Day 2: Crossing the Threshold – Using a Button to Control the LED

Somebody Is Listening

The beacon worked, and that is the problem. It has been blinking since last night, steadily, in a city where Sintech pays people to notice patterns.

"Orion, how do we ensure our signals aren't detected by Sintech?"

Orion's answer is not to make the light dimmer or cleverer. It is to make it occasional. A beacon that runs all night is a broadcast; a beacon that fires only when someone chooses is a message. So today you take the light off its automatic loop and put it under your thumb — literally — with a button.

Leave yesterday's circuit exactly where it is. You are adding to it, not starting again.

The Spider With Four Legs

Find a push button in the kit. Orion describes it as a small spider, which is about right — a square body with four legs bent underneath it. There are coloured caps in the kit you can press on top if you want one.

The obvious question is why a button that does one job needs four legs when two would do. Part of it is simply stability: two legs would leave it wobbling every time you pressed it. But the four are not four separate connections. The two legs on the left are permanently joined to each other, and so are the two on the right. Pressing the button is what bridges the left pair to the right pair, letting electricity cross from one side to the other. Let go and that bridge disappears.

The button goes over the gap in the middle. That is not decoration.

The channel running down the centre of the breadboard separates the top half from the bottom half. Components whose legs must not be joined together — buttons, switches, chips — straddle it, so each leg gets a whole column of its own to work with. Put the button anywhere else and you risk shorting its two sides together through the board itself.

Adding the Button

Yesterday's LED is untouched. Everything new is on the right-hand side.

  1. Press the button into the centre of the breadboard, straddling the gap, so two legs land on one side of the channel and two on the other.
  2. Run a jumper wire from one leg to physical pin 36 on the Pico. That pin is a 3.3 volt power output — unlike the GP pins, it is not something your code can switch on and off. It simply supplies 3.3 volts whenever the board has power, which is exactly what you want on one side of a button.
  3. Run a second jumper wire from a leg on the opposite side to physical pin 21, which your code will call GP16.

Now the logic of the thing: pressing the button connects 3.3 volts through to GP16. So GP16 sees voltage when the button is down, and sees nothing when it is up. "Something" and "nothing" is all a digital input needs in order to have an opinion.

A Tidier Way to Write It

Before the new code, Orion offers a small trick to make everything from here on shorter. Yesterday you typed machine.Pin and machine.Pin.OUT, spelling out the package every time. You can pull Pin out of the package once, at the top, and then use it on its own.

import machine
from machine import Pin

led = Pin(15, Pin.OUT)

Same pin, same LED, less typing. You will also notice there is no import time today — nothing in this program waits for anything, because the button decides the timing now instead of a delay.

The button gets a line of its own, and it needs one more detail than the LED did:

button = Pin(16, Pin.IN, Pin.PULL_DOWN)

Pin.IN is the opposite of yesterday's Pin.OUT — this pin listens rather than pushes. Pin.PULL_DOWN switches on a small resistor built into the Pico that gently ties the pin to zero volts when nothing else is driving it. Without it, an unpressed button leaves GP16 floating and picking up electrical noise from the air, and your LED will flicker at shadows. With it, unpressed reliably means zero.

Then the decision itself. if runs the indented lines beneath it only when its condition is true, and else covers every other case:

import machine
from machine import Pin

led = Pin(15, Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_DOWN)

while True:
    if button.value() == 1:
        led.on()
    else:
        led.off()

Read the loop out loud and it is almost English: forever, if the button's value is 1, turn the LED on, otherwise turn it off. The double equals sign is Python asking a question — is this the same as that? — where a single equals sign would be giving an order.

Press It

Send the code across and watch nothing happen.