using_a_breakout_button
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| using_a_breakout_button [2026/07/15 19:33] – [Introduction] walkeradmin | using_a_breakout_button [2026/07/15 20:02] (current) – walkeradmin | ||
|---|---|---|---|
| Line 31: | Line 31: | ||
| * Block2 is in the loop, and is checked every time the code loops. | * Block2 is in the loop, and is checked every time the code loops. | ||
| + | The Micro Python Code we are using. | ||
| + | \\ | ||
| + | < | ||
| + | #Traffic light LEDs using an array | ||
| + | |||
| + | from machine import Pin | ||
| + | import time | ||
| + | |||
| + | ####################### | ||
| + | # 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) | ||
| + | ####################### | ||
| + | |||
| + | |||
| + | # 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) | ||
| + | |||
| + | ####################### | ||
| + | # Read the button | ||
| + | if button.value() == 0: # Button pressed (LOW) | ||
| + | print(" | ||
| + | all_off() | ||
| + | break # Exit the loop and end main.py | ||
| + | ####################### | ||
| + | |||
| + | # Code End | ||
| + | </ | ||
| + | |||
| + | There is on limitation on pressing the button, we need to look at this part of the code to understand it. | ||
| + | \\ | ||
| + | < | ||
| + | # 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) | ||
| + | </ | ||
| + | |||
| + | In our code, to delay the LEDs so they cycle in time like a set of traffic lights would, we have four <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | However, if you read the code, you can see the button value is read as the <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | === Example === | ||
| + | \\ | ||
| + | <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | This will enable to code to run on the Pico boot up. | ||
| + | \\ | ||
| + | \\ | ||
| + | Here is the button in action. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | 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.1784144021.txt.gz · Last modified: by walkeradmin
