Digital Push Button

🎮 The Mystery of the Backwards Button

Microproject Image

Module Goal: become a digital input expert. In Module 2 you learned that a digital pin can speak (output). Today it listens (input) and you will discover your button talks backwards.

Goal: 

read the raw signal and solve the button's mystery yourself.

Concepts: 

Digital Input Recap · Raw Values · Making Predictions 

Before running: write your prediction "when I press, the number will go UP to 1 or DOWN to 0?" Most people guess 1. Run it and see.

import machine  # Library that controls the board's pins
import time  # Library for pauses
# Sets up pin26 as an input for the push button
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
while True:  # Repeat forever
    state = pin26.value()  # Read the pin: the answer is always 1 or 0
    print("Button signal:", state)  # Send the raw value to the PC
    time.sleep_ms(300)  # Pause so the screen stays readable

Serial Monitor: 

Button signal: 1 

Button signal: 1 

Button signal: 0 ← you are pressing RIGHT NOW 

Button signal: 0

 Button signal: 1 ← you let go 

💡 Note the mystery solved: your button is inverted (engineers say active LOW). Inside the module, a resistor holds the signal at 1, and pressing connects it to ground, dropping it to 0. Nothing is broken many real buttons work like this! This is why the READ step always comes first: raw values reveal the component's personality before you build anything on it. If a teammate's button is NOT inverted (0 resting, 1 pressed), just swap the 0s and 1s in every exercise below.

 Normal buttonYOUR button
Resting01
Pressed10

🎮 Speak Human

Microproject Image

Goal: 

translate the backwards signal into words everyone understands. 

Concepts: 

Conditional (if / else) · Comparison · Translating Data

import machine  # Library that controls the board's pins
import time  # Library for pauses

# Sets up pin26 as an input for the push button
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)

while True:  # Repeat forever
    state = pin26.value()  # Read the raw button signal
    if state == 0:  # INVERTED button: 0 means someone is pressing
        print("PRESSED")  # Human message for pressed
    else:  # Any other value (1)
        print("RELEASED")  # Human message for released
    time.sleep_ms(300)  # Small pause between readings

Serial Monitor: 

RELEASED 

PRESSED ← finger down 

PRESSED 

RELEASED ← finger up 

💡 Note: the sensor only ever says "0" or "1" it is YOUR program that turns that into meaning. The if is the translator between machine-language and human-language. Detective challenge: hold the button down and watch the screen floods with PRESSED. The button never gets tired of repeating itself. Annoying? Yes. Fixable? That's the next exercise.

🎮 Catch Every Single Press

Microproject Image

Goal:

count each press exactly once never twice, never while held. 

Concepts: 

Edge Detection · Memory Variable · Counter

import machine  # Library that controls the board's pins
import time  # Library for pauses

# Sets up pin26 as an input for the push button
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)

prev = 1  # Remembers the PREVIOUS reading (button rests at 1)
count = 0  # Counts how many new presses have happened

while True:  # Repeat forever
    now = pin26.value()  # Take a fresh reading
    if now == 0 and prev == 1:  # NEW PRESS: now it is 0 AND a moment ago it was 1
        count += 1  # One more press in the counter
        print("New press! Total:", count)  # Report the press and the total
    prev = now  # Save this reading to compare with the next one
    time.sleep_ms(50)  # Check fast so no press is missed

Serial Monitor (three slow presses and NOTHING while holding): 

New press! Total: 1 

New press! Total: 2 

New press! Total: 3 

💡 Note: the secret is the word AND. A press is not "the signal is 0" it is "the signal is 0 NOW but was 1 A MOMENT AGO". That instant of change is called an edge, and it is the trick behind every click counter, every keyboard, every game controller on Earth. The variable prev is your program's short-term memory: it never forgets what it just saw.

🎮 Tap or Hold?

Microproject Image

Goal: 

measure how long the button is held and classify it as a short tap or a long hold. 

Concepts: 

Time Measurement · Waiting Loops · Classification

import machine  # Library that controls the board's pins
import time  # Library for pauses and time

# Sets up pin26 as an input for the push button
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)

while True:  # Each round of the game
    print("Press the button now!")  # Invite the player
    while pin26.value() == 1:  # Wait here WHILE it is resting (1)...
        time.sleep_ms(10)  # ...checking every 10 ms
    start = time.ticks_ms()  # Freeze the exact ms when the press began
    while pin26.value() == 0:  # Now wait WHILE it is held (0)...
        time.sleep_ms(10)  # ...checking every 10 ms
    duration = time.ticks_diff(time.ticks_ms(), start)  # End minus start = hold time
    print("You held it for:", duration, "ms")  # Show the measurement
    if duration < 500:  # Less than half a second?
        print("That was a SHORT press (tap)")  # Classify as a tap
    else:  # Half a second or more
        print("That was a LONG press (hold)")  # Classify as a hold

Serial Monitor: 

Press the button now! 

You held it for: 120 ms 

That was a SHORT press (tap) 

Press the button now! 

You held it for: 1830 ms 

That was a LONG press (hold) 

💡 Note: your program now has two waiting rooms. The first waits for the press to begin (stuck while signal is 1), the second waits for it to end (stuck while signal is 0). Between them, the clock is captured. Notice the inverted button changed the wait conditions resting = 1, held = 0 but the structure is the same for ANY button in the world. Time measurement turns a simple switch into a rich input: short/long is how your phone detects a tap vs. a long-press menu!

🎮 Reflex Game

Microproject Image

Goal: 

the board says "GO!" after a secret random wait and scores your reaction time in milliseconds. Write your best score on the class whiteboard. 

Concepts: 

Random Timing · Reaction Measurement · Competition Logic

import machine  # Library that controls the board's pins
import time  # Library for time measurement
import random  # Library for random numbers

# Sets up pin26 as an input for the push button
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)

print("=== REFLEX TEST ===")  # Game title
while True:  # Play round after round
    print("Get ready... do NOT press yet")  # Warn the player
    time.sleep(random.randint(2, 5))  # SECRET random wait between 2 and 5 s
    print("GO!!! PRESS!")  # The signal!
    start = time.ticks_ms()  # Freeze the exact ms of the GO signal
    while pin26.value() == 1:  # Wait until the signal falls to 0 (press)
        time.sleep_ms(5)  # Ultra-fast checking for a precise score
    reaction = time.ticks_diff(time.ticks_ms(), start)  # End minus start = reaction
    print("Your reaction:", reaction, "ms")  # The score on the PC
    if reaction < 300:  # Faster than 300 ms?
        print("SUPER HUMAN REFLEXES!")  # Big praise
    else:  # Otherwise
        print("Good try go again!")  # Encouragement
    time.sleep(2)  # 2-second breather before the next round

Serial Monitor: 

=== REFLEX TEST === 

Get ready... do NOT press yet 

GO!!! PRESS! 

Your reaction: 247 ms 

SUPER HUMAN REFLEXES! 

Get ready... do NOT press yet 

GO!!! PRESS! 

Your reaction: 512 ms 

Good try go again! 

💡 Note anti-cheat secret: if you ever score 0 ms or a tiny impossible number... you were already holding the button when GO appeared. The program measured nothing you were waiting pre-pressed. Real reflex games detect exactly this trick. Average human reaction is around 250 ms: who in your class is fastest? (If your score looks negative, you pressed before GO the program caught you cheating!)