Using Python With The Raspberry Pi Pico

New Toys

For my birthday Lisa took me up to Micro Center to get my hand on the new Raspberry Pi Pico. A $4 microcontroller that can be programmed using Python! I have played with Arduino and it's interesting and powerful but the bottom line is that I am not a C developer. I just don't have the energy to learn it. So obviously I was pretty thrilled to hear about the board and even more excited to find out they had some at Micro Center, on sale, for $2 each. I got 2, the last two they told me.

RPi Pico

Options for Development

So as of writing the official options for the Pico are C, C++, and MicroPython. Not currently interested in C/C++, but MicroPython is a whole different story. MicroPython is a port of Python designed specifically for embedded micro's like the Pico. I used it on a ESP8266 based board and it was pretty cool but the board I was using it on was pretty limited in terms of IO. The Pico is packed with very flexible IO as well as generous memory to make it easy to develop on. Raspberry Pi foundation has documented the process for getting MicroPython working very well right here.

Development Environment

The documentation to get things going helps in a basic way to get started with the pico and walks through setting up an IDE with Thonny. That is probably great for some, but not all of us. I really like Vim, not using Vim for writing Python just isn't an option for me. The documentation does not cover this scenario and it was a little more difficult then I expected to get a workflow. MicroPython creates a file system on the controller that you then save your Python programs to. You can save a that gets run on each boot. Also you can save arbitrary python programs and custom modules just like regular Python on this file system. MicroPython does not offer a mechanism for getting these files on the device other then the typical Python OS module for file management. Trying to use Python for this is painful to say the least. We need a good way to copy a file from our local machine to the Pico.


After digging around the MicroPython forum I did find out about rshell.

This is a simple shell which runs on the host and uses MicroPython's raw-REPL to send python snippets to the pyboard in order to get file system information, and to copy files to and from MicroPython's file system.

Getting rshell was simple, there are multiple options depending on your distro. The Arch Linux AUR has a package available so that is what I used. You can use pip as well. Once it was installed I did have a little trouble using it according to the documentation.

Using rshell

After rooting around the MicroPython forum again I found the developer of rshell there discussing the issue. I was able to connect to the Pico as expected with '''rshell -p /dev/ttyACM0''' The device would connect and spit out it's status:

Using buffer-size of 32
Connecting to /dev/ttyACM0 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /Hello_World/ / /
Setting time ... Feb 04, 2021 15:30:31
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 1970
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.

It could see the files and the device but when I issued the list command. ls /flash according to the documentation, I got an error.

/home/pat/dev/rpi_pico/micropython> ls /flash
Cannot access '/flash': No such file or directory

So apprently some device are a little diffrent including the Pico. The Pico mounts it's file system in a little diffrent place then other MicroPython boards it mounts at the root of the filesystem or '/'. So because of this all we have to do is refrence the 'board_name' that is noted when you connect, as you see above my board was called 'pyboard'. So instead of ls /flash we run ls /pyboard.

/home/pat/dev/rpi_pico/micropython> ls /pyboard     Hello_World

You can change the board name by creating the file with name='coolnewname' in the file. I chose rpico as my board name. So now copying files to and from the device is easy. cp /rpico

Joy and Pain of Using a New Device

It's a little frustrating that the topic of saving a file to the device is only covered in the context of using a specific "IDE" for the device. I guess it's the only officially supported process but it I was a little suprised at how much research it took. Hopefully this post will save someone else some time.