Project 02: Onboard LED flashing
Introduction
Raspberry Pi Pico has an onboard LED, which is a GP25 pin attached to the Raspberry Pi Pico. In this project, we will learn the effect of making the onboard LED blink.
Components
|
|
|---|---|
Raspberry Pi Pico*1 |
USB Cable*1 |
Wiring Up
In this project, we use a USB cable to connect the Raspberry Pi Pico to the computer. Please refer to the documentation for the connection methods: Preparation for Python(Important)
4. Test Code
The code used in this tutorial is saved in the file …\Python_Codes. You can move the code to anywhere,for example,we can save the Python_Codes file in the Disk(D), the route is D:\Python_Codes..
Code running online:
Open“Thonny”,click“This computer”→“D:”→“Python_Codes”→“Project 02:Onboard LED flashing”.

Enter the file“Project 02:Onboard LED flashing”,double left-click the file“Project_02_Onboard_LED_flashing.py”, open it, as follows :

from machine import Pin
import time
led = Pin(25, Pin.OUT) # create LED object from Pin 25, Set Pin 25 to output
try:
while True:
led.value(1) # Set led turn on
time.sleep(0.5) # Sleep 0.5s
led.value(0) # Set led turn off
time.sleep(0.5) # Sleep 0.5s
except:
pass
Ensure that the Raspberry Pi Pico is connected to the computer, click“
Stop/Restart backend”,then see what the “Shell” window displays.

Click“
Run current script”, the code starts to execute, we will see that the LED on the Raspberry Pi Pico begins flashing. Press“Ctrl+C”or click“
Stop/Restart backend”to exit the program.


Note:This is the code that runs online. If you disconnect the USB cable and restart the Raspberry Pi Pico, the LEDS on the Raspberry Pi Pico will stop flashing. The following information will be displayed in the “Shell” window of Thonny software:

Code running offline(upload the code to the Raspberry Pi Pico):
Ensure that the Raspberry Pi Pico is connected to the computer, click“
Stop/Restart backend”.

As shown in the following figure, right-click the file “Project_02_Onboard_LED_flashing.py” and select“Upload to /” to upload the code to the Raspberry Pi Pico.

Upload main.py in the same way.

Disconnect the USB cable from the Raspberry Pi Pico and reconnect, and the LED will flash repeatedly.

Note: The code here runs offline. If you want to stop it and display the information in the “Shell” window, simply click
“Stop/Restart Backend” in Thonny software.


