Set up Raspberry Pi Pico for MicroPython

 Posted by:   Posted on:   Updated on:  2021-02-20T17:41:40Z

Install Micropython firmware on Raspberry Pi Pico and set up Thonny IDE for programming the board

Raspberry Pi Pico is a newly released microcontroller board from Raspberry Pi Foundation. Since it is quite powerful and versatile, it may prove an excellent alternative to Arduino Nano or STM32 Blue Pill. Having a similar form factor to those two development boards, the Raspberry Pi Pico can be bought for about 4 EUR (5 USD). Keep in mind that this is the price of the original product, from most approved resellers, much lower than an official Arduino Nano or the retired Maple Mini from Leaflabs (the “blue pill” is based on it).

But there is more to it than the lower cost. The microcontroller has a dual core CPU running at a frequency up to 133 MHz with 264 kB of SRAM and 2 MB of flash. With 26 GPIO pins and native USB device and host support, Raspberry Pi Pico seems better than Arduino Nano and STM32 Blue Pill. It is definitely missing the kind of connectivity you get with ESP chips (WiFi and/or bluetooth), that’s why I’m not comparing it to the ESP8266 or ESP32. Even though, it is a good choice for projects that do not require such connectivity. Nevertheless, a LAN or Bluetooth connectivity module may be interfaced to Raspberry Pi Pico when needed.

Set up Raspberry Pi Pico for MicroPython

Let’s make a quick comparison of these development boards.

  Raspberry Pi Pico
Raspberry Pi Pico
Arduino Nano
Arduino Nano
STM32 Blue Pill
STM32 Blue Pill
Microcontroller RP2040 ATmega328p STM32F103CBT6
I/O Voltage Levels 3.3 V 5 V 3.3 V
Max. current draw/pin 12 mA 20 mA (abs. max. 40 mA) 8 mA (abs. max. 20 mA)
Board pins 2 x 20 2 x 15 2 x 20
CPU 32-bit ARM Cortex M0+ 8-bit RISC 32-bit ARM Cortex M3
Number of cores 2 1 1
CPU Clock Frequency up to 133 MHz 16 MHz up to 72 MHz
CPU Speed (D/MIPS) 360 (unofficial estimation) 20 90
SRAM size 264 kB 2 kB 20 kB
Flash size 2 MB 32 kB 128 kB
Number of UARTs 2 1 3
Number of SPI ports 2 2 2
Number of I2C ports 2 1 2
USB interface 1.1 native device/host no native interface 2.0 native device
ADC 12-bit 3 channels 10-bit 8 channels 12-bit 10 channels
PWM 16-bit 16 channels 8-bit 6 channels 16-bit 15 channels
RTC Yes No Yes
Number of GPIOs 26 20 32
Debug port 3-pin ARM SWD 6-pin ICSP 4-pin ARM SWD

Overall, the Pi Pico seems better than the other two. Let's set up the programming environment. When you first plug it into the USB port, a mass storage device will appear. Although an apparent 128 MB FAT storage device will be mounted, do not attempt to put your files on it. That is not the real storage capacity (remember the flash is only 2 MB?). The only thing you can copy there are compiled binaries (with UF2 extension).

At this time, setting up a development environment for C/C++ requires installing multiple software packages. The fastest way to start programming your Pi Pico is to use MicroPython. There is an official precompiled binary available from here. Download the latest version and copy it to the RPI-RP2 storage device. You won't get to unmount or safe remove this storage device because as soon as the file is copied, the Pi Pico will restart.

From now on, the board USB emulates a serial port. To get back the mass storage where you can put compiled binaries, hold BOOTSEL button while plugging into USB port. Are you having troubles with the USB-Serial driver? Well, on recent Windows and Linux versions it should be recognized automatically. Otherwise, on Windows, you may use Zadig to install the driver. Select Board CDC device and USB Serial (CDC) driver.

Install USB CDC driver with Rufus

Install USB CDC driver with Rufus

Once connected to the computer, you have a Python interpreter over the serial port. You may use any serial terminal (like PuTTY) to write commands. But there is an IDE capable of more. It is recommended to use Thonny, a cross-platform IDE for Python programming. Download and install it (for Linux it is available in the repositories of most distributions). Launch it with standard settings.

Go to Run - Select interpreter and choose MicroPython (Raspberry Pi Pico). You may leave the serial port empty, for automatic detection. If you don't have the interpreter for Pi Pico, make sure you are using at least version 3.3.3 of Thonny. For older versions, there is a plug-in which you can download from here and install from Tools menu, Manage plug-ins.

You are now ready. The shell should confirm this. You may write Python commands there.

Thonny Python Shell

Thonny Python Shell

Here is the equivalent of the Blink sketch:

from machine import Pin
import utime

led = Pin(25, Pin.OUT)

while True:
    led.toggle();
    utime.sleep_ms(400);

Write this in Thonny, click Run and it will ask you where to save the Python file. Choose the Pi Pico and name it main.py. You may name it anything else, but this name ensures it gets executed when you power the board. Because Pi Pico is busy doing nothing during LED toggles, serial communication will sometimes fail. There are better ways to blink an LED from Pi Pico and it is usually recommended to use a timer for this purpose. The example from official documentation looks like this:

from machine import Pin, Timer

led = Pin(25, Pin.OUT)
tim = Timer()

def tick(timer):
    global led
    led.toggle()

tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)

Note that every file you run and every library you install gets copied to Pi Pico! Use File - Open and choose the board to see what is on it (you can also delete unneeded stuff).

Until now, the experience of setting up Raspberry Pi Pico and writing the first lines of code was smooth. I was also very happy to find this board in a local store at a great price. The board looks promising and I expect various device libraries to be ported to Micropython. I have very little experience with Python programming, I'm willing to learn, yet I'm still waiting for an all-in-one, easy to install development kit with IDE for C/C++.

2 comments :

  1. As far as I can tell you cannot output byte parallel (i.e. simultaneously output 8 bits) data in one instruction on the Pico GPIO as you can with the the STM32F103CBT6. I do a lot of 8/16 bit I/O data transfers on my blue pill projects, the latest being a fairly complex DDS project, still in progress. I did buy a Pico to play around with, but haven't done much with it yet.

    ReplyDelete
    Replies
    1. It is probably possible. Have a look at the programmable input/output block which, according to datasheet, can emulate various parallel protocols.

      Delete

Please read the comments policy before publishing your comment.