using_the_pico_run_button
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| using_the_pico_run_button [2026/07/16 10:04] – [Introduction] walkeradmin | using_the_pico_run_button [2026/07/16 16:31] (current) – walkeradmin | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| ===== Introduction ===== | ===== Introduction ===== | ||
| \\ | \\ | ||
| - | The Raspberry Pi Pico has a RUN pin, see below. | + | The method to reset the Pico here is the same as in the previous simple version of the code. What has changed here is that we are using a more complex Micro Python program to show a better use case. |
| \\ | \\ | ||
| \\ | \\ | ||
| Line 13: | Line 13: | ||
| \\ | \\ | ||
| Connecting this Pin to GND starts (or if it is already running restarts) the Raspberry Pi Pico. For this example I am going connect a button from the RUN pin (30) to the GND Pin (28). | Connecting this Pin to GND starts (or if it is already running restarts) the Raspberry Pi Pico. For this example I am going connect a button from the RUN pin (30) to the GND Pin (28). | ||
| + | \\ | ||
| + | \\ | ||
| + | Connect the other button and LEDs as show below. | ||
| \\ | \\ | ||
| \\ | \\ | ||
| {{ : | {{ : | ||
| \\ | \\ | ||
| - | The code I am going to use | + | The code I am going to use is our traffic light code, with the breakout button. And I am using this code for a very specific reason. |
| + | \\ | ||
| + | * 1. The code runs as main.py so when the Pico starts (boots) the code runs. | ||
| + | * 2. There is a button to ' | ||
| + | * 3. Once the breakout button has been used, the cable has to be removed and plugged back in, this causes unnecessary wear. | ||
| + | * By using the RUN pin, I can start/ | ||
| + | * The RUN button operates at a hardware level, it requires no user generated code. | ||
| + | \\ | ||
| + | Download the following Micro Python code and save it to your Pico as <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | < | ||
| + | #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() | ||
| + | |||
| + | # checks to see if the button has been pressed. | ||
| + | def check_breakout(): | ||
| + | if button.value() == 0: # Button pressed | ||
| + | print(" | ||
| + | all_off() | ||
| + | return True | ||
| + | return False | ||
| + | |||
| + | # define a function wait_with_break. This function waits 1 second in 0.1s slices. If you pass | ||
| + | # it the value 3, it will wait 3 seconds in 30 x .1s slices. This is so it can check the | ||
| + | # button state every 0.1s rather than waiting 8 whole seconds. | ||
| + | def wait_with_break(seconds): | ||
| + | for _ in range(int(seconds * 10)): # 0.1s slices in a loop | ||
| + | if check_breakout(): | ||
| + | return True | ||
| + | time.sleep(0.1) | ||
| + | return False | ||
| + | |||
| + | while True: | ||
| + | |||
| + | # Red | ||
| + | all_off() | ||
| + | RED.on() | ||
| + | # passes the value 3 to the function above causing a 3s second delay in 0.1s steps | ||
| + | if wait_with_break(3): | ||
| + | |||
| + | # Red + Amber | ||
| + | AMBER.on() | ||
| + | # passes the value 1 to the function above causing a 1s second delay in 0.1s steps | ||
| + | if wait_with_break(1): | ||
| + | |||
| + | # Green | ||
| + | all_off() | ||
| + | GREEN.on() | ||
| + | # passes the value 3 to the function above causing a 3s second delay in 0.1s steps | ||
| + | if wait_with_break(3): | ||
| + | |||
| + | # Amber | ||
| + | all_off() | ||
| + | AMBER.on() | ||
| + | # passes the value 1 to the function above causing a 1s second delay in 0.1s steps | ||
| + | if wait_with_break(1): | ||
| + | |||
| + | # Code End | ||
| + | </ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== How it Works ==== | ||
| + | \\ | ||
| + | When you boot up the Pico, the file main.py you just created will run automatically. Pushing the lower button will stop (breakout) of the main.py. This is exactly how this was programmed, this is so that you can't get stuck in a software look with no way out. | ||
| + | \\ | ||
| + | \\ | ||
| + | However, once you push the breakout button, the only way to restart the main.py is to unplug the power, and plug it back in. That causes wear on the connected, and (the real reason is that) I am to lazy to keep doing this when testing a project. | ||
| + | \\ | ||
| + | \\ | ||
| + | If you now press the RUN button, the Pico will restart (you can restart the Pico with the RUN button even if it is not stopped). | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== Running Example ==== | ||
| + | \\ | ||
| + | In the video below we can see the RUN button in operation. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | To start with the traffic light sequence is running, I then press the following buttons: | ||
| + | \\ | ||
| + | * 1. The breakout button. This stops the code (main.py). | ||
| + | * 2. The RUN button, this resets the Pico. | ||
| + | * 3. The breakout button. This stops the code again. | ||
| + | * 4. The RUN button, this resets the Pico. | ||
| + | |||
| + | This is a really nice way to start the Pico, or to restart the Pico, when you are doing a lot of testing, rather than plugging and unplugging cables all the time | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
using_the_pico_run_button.1784196259.txt.gz · Last modified: by walkeradmin
