Getting Started With Raspberry Pi Pico
Getting Started With Raspberry Pi Pico
Projects
Step 1 Introduction
In this project, you will connect a Raspberry Pi Pico to another computer and learn how to program it using
MicroPython.
A Raspberry Pi Pico is a low-cost microcontroller device. Microcontrollers are tiny computers, but they tend to lack
large volume storage and peripheral devices that you can plug in (for example, keyboards or monitors).
A Raspberry Pi Pico has GPIO pins, much like a Raspberry Pi computer, which means it can be used to control and
receive input from a variety of electronic devices.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 1/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
If you are completing this project in a school or other setting with a managed network, then you should make
sure that you have the appropriate permissions to mount a USB drive and install software.
If you need to print this project, please use the printer-friendly version (https://projects.raspberrypi.org/e
n/projects/getting-started-with-the-pico/print).
Here is a link to the completed scripts for this project (https://rpf.io/p/en/getting-started-with-the-pico
-get).
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 2/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
This is a Raspberry Pi Pico. Hopefully your device has already had the header pins soldered on, but if not, you might
like to have a look at our Getting started with soldering resource (https://projects.raspberrypi.org/en/projec
ts/getting-started-with-soldering).
Plug your micro USB cable into the port on the left-hand side of the board.
If you need to know the pin numbers for a Raspberry Pi Pico, you can refer to the following diagram.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 3/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 4/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
In this step, you will install Thonny or make sure you have the latest version. Then you will connect to a Raspberry
Pi Pico and run some simple Python code using the Shell.
Thonny on Raspberry Pi
Thonny is already installed on Raspberry Pi OS, but may need to be updated to the latest version
Open a terminal window, either by clicking the icon in the top left-hand corner of the screen or by pressing
the Ctrl+Alt+T keys at the same time
In the window, type the following to update your OS and Thonny
Open Thonny from your application launcher. It should look something like this:
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 5/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
You can use Thonny to write standard Python code. Type the following in the main window, and then
click the Run button (you will be asked to save the file).
print('Hello World!')
You’re now ready to move on to the next step and connect your Raspberry Pi Pico.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 6/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
If you have never used MicroPython on your Raspberry Pi Pico, you will need to add the MicroPython firmware.
Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable to your
computer. A Raspberry Pi is shown in the image below, but the same applies to any computer.
This puts your Raspberry Pi Pico into USB mass storage device mode.
In the bottom right-hand corner of the Thonny window, you will see the version of Python that you are
currently using.
If you don’t see this option, then check that you have plugged in your Raspberry Pi Pico.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 7/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
A dialog box will pop up to install the latest version of the MicroPython firmware on your Raspberry Pi
Pico.
Click the Install button to copy the firmware to your Raspberry Pi Pico.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 8/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 9/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
In this step, you will use the Thonny Shell to run some simple Python code on your Raspberry Pi Pico.
Make sure that your Raspberry Pi Pico is connected to your computer and you have selected the
MicroPython (Raspberry Pi Pico) interpreter.
Thonny is now able to communicate with your Raspberry Pi Pico using the REPL (read–eval–print loop),
which allows you to type Python code into the Shell and see the output.
Now you can type commands directly into the Shell and they will run on your Raspberry Pi Pico.
print("Hello")
Tap the Enter key and you will see the output:
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 10/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
MicroPython adds hardware-specific modules, such as machine, that you can use to program your
Raspberry Pi Pico.
Let’s create a machine.Pin object to correspond with the onboard LED, which can be accessed using
GPIO pin 25.
If you set the value of the LED to 1, it turns on.
Enter the following code, make sure you tap Enter after each line.
led.value(1)
Type the code to set the value to 0 to turn the LED off.
led.value(0)
If you want to write a longer program, then it’s best to save it in a file. You’ll do this in the next step.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 11/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
The Shell is useful to make sure everything is working and try out quick commands. However, it’s better to put
longer programs in a file.
Thonny can save and run MicroPython programs directly on your Raspberry Pi Pico.
In this step, you will create a MicroPython program to blink the onboard LED on and off in a loop.
led.toggle()
Tip: You need to enter the .py file extension so that Thonny recognises the file as a Python file.
Thonny can save your program to your Raspberry Pi Pico and run it.
You should see the onboard LED switch between on and off each time you click the Run button.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 12/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
You can use the Timer module to set a timer that runs a function at regular intervals.
timer = Timer()
def blink(timer):
led.toggle()
Click Run and your program will blink the LED on and off until you click the Stop button.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 13/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
Now you know the basics, you can learn to control an external LED with your Raspberry Pi Pico, and get it to read
input from a button.
Use a resistor between about 50 and 330 ohms, an LED, and a pair of M-M jumper leads to connect up
your Raspberry Pi Pico as shown in the image below.
In this example, the LED is connected to pin 15. If you use a different pin, remember to look up the number in the
pinout diagram in the Meet Raspberry Pi Pico section (1.html).
Use the same code as you did to blink the onboard LED, but change the pin number to 15.
timer = Timer()
def blink(timer):
led.toggle()
Run your program and your LED should start to blink. If it’s not working, check your wiring to be sure that the LED is
connected.
Next, let’s try and control the LED using a button.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 14/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
The button is on pin 14, and is connected to the 3.3V pin on your Raspberry Pi Pico. This means when you set up
the pin, you need to tell MicroPython that it is an input pin and needs to be pulled down.
import time
while True:
if button.value():
led.toggle()
time.sleep(0.5)
Run your code and then when you press the button, the LED should toggle on or off. If you hold the
button down, it will flash.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 15/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
For this activity, you can use the circuit from the last step.
pwm = PWM(Pin(15))
pwm.freq(1000)
while True:
pwm.duty_u16(duty)
sleep(0.0001)
pwm.duty_u16(duty)
sleep(0.0001)
Save and run the file. You should see the LED pulse bright and dim, in a continuous cycle.
The frequency (pwm.freq) tells Raspberry Pi Pico how often to switch the power between on and off for the LED.
The duty cycle tells the LED for how long it should be on each time. For Raspberry Pi Pico in MicroPython, this can
range from 0 to 65025. 65025 would be 100% of the time, so the LED would stay bright. A value of around 32512
would indicate that it should be on for half the time.
Have a play with the pwm.freq() values and the pwm.duty_u16 values, as well as the length of time
for the sleep, to get a feel for how you can adjust the brightness and pace of the pulsing LED.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 16/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
Your Raspberry Pi Pico has input pins that can receive analogue signals. This means that instead of only reading
the values of 1 and 0 (on and off), it can read values in between.
Replace the button in your circuit with a potentiometer. Follow the wiring diagram below to connect it to
an analogue pin.
In a new file in Thonny, you can first read the resistance of the potentiometer.
Add this code to a new file, and then run it.
import time
adc = ADC(Pin(26))
while True:
print(adc.read_u16())
time.sleep(1)
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 17/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
You can now use this value to control the duty cycle for PWM on the LED.
Change the code to the following. Once you have run it, tune the dial on the potentiometer to control the
LED’s brightness.
pwm = PWM(Pin(15))
adc = ADC(Pin(26))
pwm.freq(1000)
while True:
duty = adc.read_u16()
pwm.duty_u16(duty)
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 18/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
If you want to run your Raspberry Pi Pico without it being attached to a computer, you need to use a USB power
supply.
Safe operating voltages are between 1.8V and 5.5V.
To automatically run a MicroPython program, simply save it to the device with the name main.py
In Thonny, click on the File menu and then Save as for the last program you wrote.
You can now disconnect your Raspberry Pi Pico from your computer and use a micro USB cable to
connect it to a mobile power source, such as a battery pack.
Once connected, the main.py file should run automatically so you can interact with the components attached to
your Raspberry Pi Pico.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 19/20
11/18/22, 11:57 AM Getting started with Raspberry Pi Pico
https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/print 20/20