The Raspberry Pi Pico is an intriguing board. Rather than be another Linux single board computer like every other Raspberry Pi, the Pico is a low-cost Arm based microcontroller which we can program using C/C++ and MicroPython. In this tutorial we will introduce how to get started with the Raspberry Pi Pico, If you would like to know more technical details about the board, then take a look at our review.
How to Set Up the Raspberry Pi Pico
1. Download the MicroPython UF2 file from the MicroPython tab.
2. Push and hold the BOOTSEL button on the Pico, then connect to your computer using a micro USB cable. Release BOOTSEL once the drive RPI-RP2 appears on your computer.
3. Drag and drop the UF2 file on to the RPI-RP2 drive. The Raspberry Pi Pico will reboot and will now run MicroPython.
Pico Python is MicroPython for the Raspberry Pi Pico. If you have never used MicroPython, it is a version of Python 3 developed for microcontrollers. If you can write Python, then you can write MicroPython. To write MicroPython code, we need to use a dedicated editor and the default, basic editor is Thonny which is what we shall use for this tutorial.
1. Download and install Thonny for your OS, if you don’t already have it. You can grab it for free from the Thonny website. In our case, it is v 3.3.2 for Windows.
2. Connect the Raspberry Pi Pico to your computer and in Thonny go to Tools > Options and click on the Interpreter tab. From the interpreter dropdown list select MicroPython (Raspberry Pi Pico). The port dropdown menu can be left to automatically detect the Pico. Click Ok to close.
The Python Shell (also called REPL, Read, Eval, Print, Loop) will now update to show that the Pico is connected and working.
3. To test we can write a quick print function to say “Hello World.” Press Enter to run the code.
How to Blink an LED Light on Raspberry Pi Pico
To further test that we can successfully program the Raspberry Pi Pico, we shall write the “Hello World” equivalent for hardware projects, flashing an LED. This quick test ensures that our hardware is working, and it will introduce the MicroPython language and syntax in the simplest form.
Before we start writing any code, we first need to wire up our test circuit. This will require header pins to be soldered to the Raspberry Pi Pico. To build this project you will need:
- A half size breadboard
- An LED
- A 330 Ohm resistor
1. Insert the Raspberry Pi Pico into the breadboard so that it sits over the central channel. Make sure that the Micro USB port is at one end of the breadboard.
2. Insert a 330 Ohm resistor into the breadboard, one leg should be inline with GND, which is pin 38. The other leg should be inserted into the - rail of the breadboard. This provides us with a GND rail where all pins in that rail are connected to GND.
3. Insert an LED, with the long leg (the anode) inserted into the breadboard at pin 34, and the short leg inserted into the GND rail. The circuit is now built.
With the circuit built we can now start writing the code to flash (blink) the LED.
4. Import the necessary libraries. Our code is written in the large blank space above the REPL and we start by importing two MicroPython libraries. The first is the Pin class from the Machine library, the second is utime, used to control the pace of our code.
from machine import Pin import utime
5. Create an object, “led” which is used to create a link between the physical GPIO pin and our code. In this case, it will set GPIO 28 (which maps to physical pin 34 on the board) as an output pin, where current will flow from the Raspberry Pi Pico GPIO to the LED. We then use the object to instruct the GPIO pin to pull low.n other words this will ensure that the GPIO pin is turned off at the start of our project.
led = Pin(28, Pin.OUT) led.low()
6. Inside of a while True loop, a loop with no end, we toggle the LED on and off, and print a message to the Python Shell (REPL) to prove that the loop is working. Lastly, we add a sleep to pause the code for one second between each iteration of the loop.
while True: led.toggle() print("Toggle") utime.sleep(1)
7. Click on Save and choose to save the code to the MicroPython device (Raspberry Pi Pico). Name the file blink.py and click Ok to save. Your code should look like this.
from machine import Pin import utime led = Pin(28, Pin.OUT) led.low() while True: led.toggle() print("Toggle") utime.sleep(1)
8. To run the code, click on the Green play / arrow button and the Python Shell will update to say TOGGLE every second, and the LED will flash on and off.
We have successfully tested our Raspberry Pi Pico and we can now move on to another project. Such as learning how to use sensors with the Raspberry Pi Pico.