Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040, a brand new microcontroller chip designed by Raspberry Pi in the UK.Don’t be confused by the name Raspberry Pi Pico. It is quite different from other Raspberry Pi products such as Raspberry Pi 2, 3 and 4 pocket computers which have Linux OS.Pico does not have any OS, it is actually a low-cost, high-performance micro-controller board with flexible digital interfaces. In fact, Pico’s functions are more like its competitor Arduino MCU boards.
You can program Pico with C++ and MicroPython. In this tutorial, we focus on MicroPython which is more friendly to beginners. Step 1 Download and install MicroPython firmware to Pico board:
Push and hold the BOOTSEL button and plug your Pico into the USB port of your Raspberry Pi or other computer. Release the BOOTSEL button after your Pico is connected.
It will mount as a Mass Storage Device called RPI-RP2.
Drag and drop the MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython.
Step2 Download and Install Thonny Python IDE
Download Thonny Python IDE for your PC from following link(make sure you select correct OS version for your PC): https://thonny.org/
Run the installation software from the download file.
Now open the Thonny Python IDE, click Run to select MicroPython for Rapsberry Pi Pico as Interpreter :
Also please select the COM port which your Pico board is connected as photo.After that, click OK to save the setting.
copy following code and paste them to the coding window:
from machine import Pin
from time import sleep
led = Pin(25,Pin.OUT)
n=0
while True:
led.toggle()
print("counter is {}".format(n))
n=n+1
sleep(1)
You will see the code in IDE as following
Click the little ► button to run the Python code
Click File ->Save As
Then select Raspberry Pi Pico as destination
Name the file as main.py, then click OK
Now the main.py code will automatically run when Pico board is connected to USB power.
Remember, only main.py can be automatically executed. If your python code is saved to other name, it can not be executed automatically.
You will see the LED in Pico board will flash constantly.Congratulations! You have made your first Python program running in Pico!
(see the # comments in each line):
from machine import Pin #import machine library to control GPIO pins
from time import sleep #import time library to use sleep function
led = Pin(25,Pin.OUT) #Set Pin 25 in OUTPUT mode, Pin 25 is connected an internal LED integrated in the board
n=0
while True:
led.toggle() #make pin 25 led flash once
print("counter is {}".format(n)) # display flash counter in the terminal
n=n+1
sleep(1) #delay one second and do next flash