User Tools

Site Tools


using_a_breakout_button

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
using_a_breakout_button [2026/07/15 19:30] – [Introduction] walkeradminusing_a_breakout_button [2026/07/15 20:02] (current) walkeradmin
Line 17: Line 17:
 {{ :traffic-light-breakout-button.png?400 |}} {{ :traffic-light-breakout-button.png?400 |}}
 \\  \\ 
 +This is our original traffic light build, but with an additional push button on pins GP16 and GND.
 +\\ 
 +\\ 
 +----
 +==== The Code ====
 +\\ 
 +Below is the code, there are two blocks that have been added. The are:
 +\\ 
 +  * Block1 - Sets up GP16 as an input that is pulled up (so we need to set it to GND for it to work)
 +  * Block2 - Monitors the button to see if it is pressed (looking for a zero on Pin16)
 +
 +  * Block1 is at the program start, and is only run once.
 +  * Block2 is in the loop, and is checked every time the code loops.
 +
 +The Micro Python Code we are using.
 +\\ 
 +<code:python | download>
 +#Traffic light LEDs using an array
 +
 +from machine import Pin
 +import time
 +
 +#######################    ADD BREAKOUT BUTTON #############################
 +# Configure the button on GPIO16
 +# PULL_UP means the pin reads HIGH (1) until the button connects it to GND
 +button = Pin(16, Pin.IN, Pin.PULL_UP)
 +#######################    ADD BREAKOUT BUTTON #############################
 +
 +
 +# Define GPIO pins for the LEDs
 +RED = Pin(2, Pin.OUT)
 +AMBER = Pin(6, Pin.OUT)
 +GREEN = Pin(10, Pin.OUT)
 +
 +# Put them in an array
 +lights = [RED, AMBER, GREEN]
 +
 +# Helper function to turn all lights off
 +def all_off():
 +    for light in lights:
 +        light.off()
 +
 +while True:
 +
 +    # Red
 +    all_off()
 +    RED.on()
 +    time.sleep(3)
 +
 +    # Red + Amber
 +    AMBER.on()
 +    time.sleep(1)
 +
 +    # Green
 +    all_off()
 +    GREEN.on()
 +    time.sleep(3)
 +
 +    # Amber
 +    all_off()
 +    AMBER.on()
 +    time.sleep(1)
 +
 +####################### CHECK BREAKOUT BUTTON ##########################
 +    # Read the button
 +    if button.value() == 0:   # Button pressed (LOW)
 +        print("Button pressed — stopping program.")
 +        all_off()
 +        break                 # Exit the loop and end main.py
 +####################### CHECK BREAKOUT BUTTON ##########################
 +
 +# Code End
 +</code>
 +
 +There is on limitation on pressing the button, we need to look at this part of the code to understand it.
 +\\ 
 +<code:python | download>
 +    # Red
 +    all_off()
 +    RED.on()
 +    time.sleep(3)
 +
 +    # Red + Amber
 +    AMBER.on()
 +    time.sleep(1)
 +
 +    # Green
 +    all_off()
 +    GREEN.on()
 +    time.sleep(3)
 +
 +    # Amber
 +    all_off()
 +    AMBER.on()
 +    time.sleep(1)
 +</code>
 +
 +In our code, to delay the LEDs so they cycle in time like a set of traffic lights would, we have four <color #ed1c24>time.sleep</color> statements, the total delay of these is 8 seconds. This means, when we press the button, depending on where we are in the loop, we might have to hold the button for up to 9 seconds before the button value is read.
 +\\ 
 +\\ 
 +However, if you read the code, you can see the button value is read as the <color #ffc90e>Amber</color> LED is turned on after the <color #22b14c>Green</color> LED>. So it is quite easy to time right if you don't want to hold the button for 8 seconds.
 +\\ 
 +\\ 
 +=== Example ===
 +\\ 
 +<color #ed1c24>***Remember*** - We must save this code to our Raspberry Pi Pico as a file called main.py.</color>
 +\\ 
 +\\ 
 +This will enable to code to run on the Pico boot up.
 +\\ 
 +\\ 
 +Here is the button in action.
 +\\ 
 +\\ 
 +{{ :traffic-light-breakout-button-sim.mp4?900x634 |}}
 +\\ 
 +Using a button like this means you can never get you Pico stuck in a software loop with little to no way of recovery (without wiping it completely).
 +\\ 
 +\\ 
 +----
  
using_a_breakout_button.1784143808.txt.gz · Last modified: by walkeradmin