Maker.io main logo

Raspberry Pi Pico and RP2040 - MicroPython Part 1: Blink

2021-04-12 | By ShawnHymel

License: Attribution

Raspberry Pi officially supports C/C++ and MicroPython for their new Pico board. Other languages (such as CircuitPython) will work, but as of right now, these are the two with support listed on the Raspberry Pi site.

In this series, I’ll walk you through the process of installing Thonny and writing your first MicroPython program on the Raspberry Pi Pico.

Here is a video of these steps, if you would like to see them in action:

 

Update Firmware

To start, we’ll need to load a special firmware onto the Pico. This will allow us to run MicroPython. Head to the Raspberry Pi Pico (Getting Started with MicroPython) page and click Download UF2 file

Connect one end of a USB micro B cable into your computer. Push and hold the BOOTSEL button on your Pico. While holding the button, plug the other end of the USB cable into the Pico board. This will cause the Pico to load its bootloader.

Pico bootloader button

You should see RPI-RP2 appear as a new drive on your computer when you do this. Copy the .uf2 file you downloaded to the top-level directory of this drive.

Copy MicroPython uf2 to Raspberry Pi Pico

Just after copying, the Pico should reset. You’re all set to run MicroPython!

Install Thonny

While there are a number of editors that will allow you to write MicroPython code for the Pico, Raspberry Pi recommends Thonny.

Head to thonny.org and download the installer for your operating system (accept all defaults).

REPL

The firmware on the Pico runs a lightweight interpreter known as the Read-Evaluate-Print Loop (REPL). It allows us to execute MicroPython commands in real-time without having to write a full program. We can use Thonny to see how the REPL works (it’s essentially a command line interface).

Open Thonny. In the bottom-right corner, click the button that’s likely labeled The same interpreter which runs Thonny (default). Note that if we can use Thonny to develop Python programs on our computer. However, we want to run things on the Pico. So, select MicroPython (Raspberry Pi Pico) from the drop-down list.

Select board in Thonny

We can use the top pane to write programs or scripts--it’s just a text editor. The bottom pane gives us access to the Python interpreter (or in our case, REPL). In the bottom pane, enter the following command:

Copy Code
print(“hello”)

Press enter, and you should see “hello” echoed back to you. What’s interesting is that this is running on the Pico. We send a command over the USB cable, the REPL on the Pico interprets it and sends back the result over the USB cable.

Hello world on Raspberry Pi Pico

Hello, World!

Let’s turn our REPL command into a program! In the top pane, enter the following code:

Copy Code
import utime

while True:
print("Hello, World!")
utime.sleep(1.0)

utime is a common module in MicroPython. It’s similar to the time module in Python, but it has been pared down to work with MicroPython. We will use it to delay the processor between print() commands. Note that we need to give utime.sleep() a floating-point value (in seconds).

Click the Run button in Thonny. You will be asked to save the program on either your computer or the Raspberry Pi Pico. 

Save program to Pico

Click Raspberry Pi Pico. Save it as “main.py” in the top-level directory of the Pico.

Saving a file on Raspberry Pi Pico

The program should start running, printing “Hello, World!” to the console once per second.

Hello World running on the Raspberry Pi Pico

Note that you can save the program with any name you like in the Pico. However, if there is a file named main.py, the Pico will run that automatically on boot.

I also highly recommend saving file somewhere on your computer in addition to the Pico (File > Save As). If you accidentally overwrite main.py on the Pico, you can’t get it back!

Blink

To control hardware, such as blinking a pin, we need to use a special module: machine. In a new file, enter the following code:

Copy Code
import machine
import utime

led = machine.Pin(25, machine.Pin.OUT)

while True:
led.value(1)
utime.sleep(1.0)
led.value(0)
utime.sleep(1.0)

The machine library is a MicroPython library that’s common on many boards. It allows us to communicate with various pins and peripherals. We don’t need to explicitly include it on the Pico, but I wrote it out for clarity anyway.

We declare a pin object by calling machine.Pin(). The first parameter is the pin number (GPIO number). Page 5 of the Raspberry Pi datasheet has a great pinout diagram. There, you’ll see that the LED is connected to pin 25 (GP25).

The second parameter is the data direction. We want to control an LED, so we need to be able to set the pin high (3.3V) and low (GND). So, we set it as an output with machine.Pin.OUT.

In the while loop, we set the pin to 3.3V by calling led.value(1), and we set it to low by calling led.value(0). We put a sleep in between each call so that the LED will turn on for 1 second and turn off for 1 second!

Run this code (feel free to save it as “main.py” on your Pico). You should see the Pico’s onboard LED blinking on and off each second.

Raspberry Pi Pico LED

Recommended Reading

Hopefully, this guide should give you a head start to making your own MicroPython programs on the Raspberry Pi Pico. If you would like to dig deeper, I recommend checking out the following documentation:

制造商零件编号 SC0915
RASPBERRY PI PICO RP2040
Raspberry Pi
¥32.56
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum