using_led_flash
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| using_led_flash [2026/07/15 16:14] – [Raspberry Pi Pico 1 / Pico 2] walkeradmin | using_led_flash [2026/07/15 17:15] (current) – walkeradmin | ||
|---|---|---|---|
| Line 38: | Line 38: | ||
| ==== Raspberry Pi Pico 1 / Pico 2 ==== | ==== Raspberry Pi Pico 1 / Pico 2 ==== | ||
| \\ | \\ | ||
| - | Below is the code for the Pico1 and Pico2 | + | Below is the (breakout) |
| \\ | \\ | ||
| - | <code | download> | + | <code: |
| #################################################### | #################################################### | ||
| # visual Thonny breakout. | # visual Thonny breakout. | ||
| - | #use this declaration for Pico 2W | + | #use this declaration for Pico1 & Pico2 |
| - | #OBLED = Pin(' | + | |
| - | #use this declaration for non pico W | + | # Set Pin25 as output (Pin 25 is where the onboard LED is connected) |
| LED = Pin(25, Pin.OUT) | LED = Pin(25, Pin.OUT) | ||
| + | # Print a message in Thonny to remind user to break out of code now. | ||
| print(' | print(' | ||
| + | # loop 20 times | ||
| + | for i in range (20): | ||
| + | |||
| + | # turn on LED, wait half a second. | ||
| + | #for Pico1 & Pico2 | ||
| + | LED.on() | ||
| + | time.sleep(0.5) | ||
| + | |||
| + | # turn off LED, wait half a second. | ||
| + | LED.off() | ||
| + | time.sleep(0.5) | ||
| + | #################################################### | ||
| + | </ | ||
| + | |||
| + | What we need to do now is to place this code, inside of our main program that we want to run on boot. In this example I am using the traffic light example, so when I power on the Pico, the traffic light sequence will start (after the initial 20 second delay). | ||
| + | \\ | ||
| + | \\ | ||
| + | Below, you can see where we have inserted this code block in to our Traffic Light code. | ||
| + | \\ | ||
| + | < | ||
| + | #Traffic light LEDs using an array | ||
| + | |||
| + | from machine import Pin | ||
| + | import time | ||
| + | |||
| + | # 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] | ||
| + | |||
| + | #################################################### | ||
| + | # visual Thonny breakout. | ||
| + | |||
| + | #use this declaration for Pico1 & Pico2 | ||
| + | |||
| + | # Set Pin25 as output (Pin 25 is where the onboard LED is connected) | ||
| + | LED = Pin(25, Pin.OUT) | ||
| + | |||
| + | # Print a message in Thonny to remind user to break out of code now. | ||
| + | print(' | ||
| + | # loop 20 times | ||
| for i in range (20): | for i in range (20): | ||
| - | #for Pico2 W use this | + | # turn on LED, wait half a second. |
| - | #OBLED.on() | + | #for Pico1 & Pico2 |
| - | #time.sleep(0.5) | + | |
| - | # | + | |
| - | #for no pico2 w | + | |
| LED.on() | LED.on() | ||
| time.sleep(0.5) | time.sleep(0.5) | ||
| - | | + | |
| - | | + | # turn off LED, wait half a second. |
| + | | ||
| time.sleep(0.5) | time.sleep(0.5) | ||
| #################################################### | #################################################### | ||
| + | # 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(5) | ||
| + | |||
| + | # Amber | ||
| + | all_off() | ||
| + | AMBER.on() | ||
| + | time.sleep(1) | ||
| </ | </ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== Raspberry Pi Pico W and Pico 2W ==== | ||
| + | \\ | ||
| + | For the wireless based Pico, we need to use slightly different code, as the LED is not on Pin25, it is accessed via a pin in the Wifi chip. | ||
| + | \\ | ||
| + | < | ||
| + | #################################################### | ||
| + | # visual Thonny breakout. | ||
| + | |||
| + | #use this declaration for Pico1W & Pico2W | ||
| + | |||
| + | #use this declaration for Pico 1W and Pico 2W | ||
| + | OBLED = Pin(' | ||
| + | |||
| + | # Print a message in Thonny to remind user to break out of code now. | ||
| + | print(' | ||
| + | |||
| + | # loop 20 times | ||
| + | for i in range (20): | ||
| + | |||
| + | |||
| + | #for Pico1W and Pico2W use this | ||
| + | # turn on LED, wait half a second. | ||
| + | OBLED.on() | ||
| + | time.sleep(0.5) | ||
| + | |||
| + | # turn off LED, wait half a second. | ||
| + | OBLED.off() | ||
| + | time.sleep(0.5) | ||
| + | #################################################### | ||
| + | </ | ||
| + | |||
| + | Once again, let us look at this code block embedded in our original Traffic Light code. | ||
| + | \\ | ||
| + | \\ | ||
| + | < | ||
| + | #Traffic light LEDs using an array | ||
| + | |||
| + | from machine import Pin | ||
| + | import time | ||
| + | |||
| + | # 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] | ||
| + | |||
| + | |||
| + | #################################################### | ||
| + | # visual Thonny breakout. | ||
| + | |||
| + | #use this declaration for Pico1W & Pico2W | ||
| + | |||
| + | #use this declaration for Pico 1W and Pico 2W | ||
| + | OBLED = Pin(' | ||
| + | |||
| + | # Print a message in Thonny to remind user to break out of code now. | ||
| + | print(' | ||
| + | |||
| + | # loop 20 times | ||
| + | for i in range (20): | ||
| + | |||
| + | |||
| + | #for Pico1W and Pico2W use this | ||
| + | # turn on LED, wait half a second. | ||
| + | OBLED.on() | ||
| + | time.sleep(0.5) | ||
| + | |||
| + | # turn off LED, wait half a second. | ||
| + | OBLED.off() | ||
| + | time.sleep(0.5) | ||
| + | #################################################### | ||
| + | |||
| + | |||
| + | |||
| + | # 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) | ||
| + | </ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== In Action ==== | ||
| + | \\ | ||
| + | From Thonny, I am going to start our traffic light code with the new breakout code block. Please bear in mind that in this example I am NOT using a Pico W variant. | ||
| + | \\ | ||
| + | \\ | ||
| + | <color # | ||
| + | \\ | ||
| + | First thing I need to do, because we want this code to run at Pico boot, is to save the file as <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | Please watch the video below to see this code in operation. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | When the start button is pressed (so when the Pico boots) we see the Pico Onboard LED flashing for 20 seconds. Then, the traffic light sequence will start. In the output window we can see the message <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
using_led_flash.1784132046.txt.gz · Last modified: by walkeradmin
