User Tools

Site Tools


using_led_flash

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_led_flash [2026/07/15 16:19] – [Raspberry Pi Pico 1 / Pico 2] walkeradminusing_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 for the Pico1 and Pico2
 \\  \\ 
 <code:python | download> <code:python | download>
Line 66: Line 66:
 </code> </code>
  
-What we eed 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).+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.
 +\\ 
 +<code:python | download>
 +#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('booting... Press ctrl-c to stop')
 +
 +# 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)
 +####################################################
 +
 +# 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)
 +</code>
 +\\ 
 +----
 +==== 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.
 +\\ 
 +<code:python | download>
 +####################################################
 +# visual Thonny breakout.
 +
 +#use this declaration for Pico1W & Pico2W
 +
 +#use this declaration for Pico 1W and Pico 2W
 +OBLED = Pin('LEDW',Pin.OUT)
 +
 +# Print a message in Thonny to remind user to break out of code now.
 +print('booting... Press ctrl-c to stop')
 +
 +# 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)
 +####################################################
 +</code>
 +
 +Once again, let us look at this code block embedded in our original Traffic Light code.
 +\\ 
 +\\ 
 +<code:python | download>
 +#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('LEDW',Pin.OUT)
 +
 +# Print a message in Thonny to remind user to break out of code now.
 +print('booting... Press ctrl-c to stop')
 +
 +# 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)
 +</code>
 +\\ 
 +----
 +==== 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 #ed1c24>*** IMPORTANT ***</color>
 +\\ 
 +First thing I need to do, because we want this code to run at Pico boot, is to save the file as <color #ed1c24>main.py</color>. The Pico understands that a file called main.py is to be run at boot.
 +\\ 
 +\\ 
 +Please watch the video below to see this code in operation.
 +\\ 
 +\\ 
 +{{ :traffic-light-breakout-example-simulated.mp4?894x886 |}}
 +\\ 
 +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 #ed1c24>booting... Press ctrl-c to stop</color>.
 +\\ 
 +\\ 
 +----
using_led_flash.1784132386.txt.gz · Last modified: by walkeradmin