Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Programming the Arduino NANO RP2040 using MicroPython

This video is about Python on the Arduino NANO RP2040 Connect. When first released Arduino promised support for MicroPython on the Arduino Nano RP2040, it's taken a while but it's now available.

Arduino have made some unusual choices with MicroPython, in particular the use of OpenMV for programming the Arduino boards. This is unusual as OpenMV is normally used with cameras and machine vision, and not for generic microcontroller boards. It does however provide a way to install MicroPython and upload new programs.

OpenMV programming MicroPython on the Arduino Nano RP2040 connect

OpenMV IDE on Ubuntu

The official Arduino instructions explain how to install MicroPython using the OpenMV IDE. This can be downloaded from the OpenMV website.

After installing the IDE double-press the button on the top of the Arduino to get into bootsel mode. This should mount a new drive on you computer called RPI-RP2. Note this is a bit of an art and sometimes doesn't work, if you cannot get this to work you can instead connect the reset pin to ground and reconnect the Arduino which should always work.

OpenMV will then ask you to install the MicroPython interpretter. You can now create your program and run it on the Arduino by pressing the play button (also in the bottom left). There are some example programs under the file menu if you want some inspiration.

Adding Your Own Libraries with OpenMV

I wanted to use my Arduino with NeoPixels. I've already programmed a Wireless NeoPixels / Pixel Strip Server using the Arduino RP2040, but that was programmed in C++. I haven't repeated that, instead creating a simple example of how to change the colours one strip at a time.

To do this I needed a library for controlling the NeoPixels. There don't appear to be any specifically targetting the Arduino Nano RP2040 but there is one for the Raspberry Pi Pico, which uses the same RP2040 microcontroller. The library is available from GitHub by blaz-r. The file that is needed is just the file neopixel.py.

The file needs to be transferred to the Arduino. The easiest way is that there will be a virtual drive created representing the code area. This was mounted on my system as a 8MB Volume.

The code that uses this library is called testpixels-omv.py (available in the zip file towards the bottom of this page).

OpenMV on the Raspberry Pi

Unfortunately I wasn't able to get OpenMV working on Raspberry Pi OS Bullsye. This is because the code relies on outdated libraries, including libpng12, which has been replaced by libpng16. To install the older library could potentially break other programs so I didn't continue trying to get it to work with Bullsye.

I did however get it to work with Raspberry Pi OS Buster, which was the current version prior to November 2021. After installing I did however have a problem installing the MicroPython interpretter, so I had to do that manually as root. The command to do that is:

sudo /opt/openmvide/share/qtcreator/picotool/arm/picotool load -x /home/pi/.config/OpenMV/qtcreator/firmware/ARDUINO_NANO_RP2040_CONNECT/firmware.bin

This assumes that OpenMV IDE has been installed in the /opt directory.

One that is complete then you can use OpenMVE IDE normally.

Installing Upstream MicroPython.org version

An alternative to using the Arduino version of MicroPython is to use the upstream version from MicroPython.org. This provides a version for the Arduino NANO RP2040 connect. Some modifications have been made to the Arduino version, the most noticeable is that the Arduino version makes the code area available as a USB flash drive, whereas the MicroPython.org version does not. If using MicroPython.org version then you'll need to transfer files using a different method (such as Ampy which is explained later). I'm not sure if there are additional libraries included in the Arduino version (installed using OpenMV) so you may need to take care if using this version, but it does work with the example I used. I did also come across one problem in that any custom modules you want to import need to be in a different directory to the top level one. It's easy to get around this. I created a lib directory and stored the neopixel.py file in there (see the testpixels-mpy.py file for how to reference that).

To install the MicroPython.org version, then download it to your PC, enter bootsel mode on the Arduino (eg. double press the button the Arduino) and then drag and drop the uf2 file to the virtual disk RPI-RP2.

Thonny Editor on Ubuntu or Raspberry Pi

After installing either of the MicroPython versions you can program using the Thonny editor. The Thonny editor allows you to store files directly onto the Arduino. This is particularly useful if using the MicroPython.org interpreter but you need to be careful you also keep a copy on your computer as well.

Thonny Editor programming MicroPython on the Arduino Nano RP2040 connect

To use the Thonny editor Go to the Run Menu and choose Set Interpreter. You should set the interpreter to MicroPython (generic) and the board to Board in FS mode.

Using Command Line Tools

There are a number of command line tools which can be used for uploading / downloading files from the Arduino and for interacting with the Python interpreter. I'll show two examples: minicom for connecting to the python interpreter (eg. REPL) and Adafruit Ampy for transferring files.

Minicom - terminal software

To install minicom use apt install minicom. You can then connect using:

minicom -b 115200 -o -D /dev/ttyACM0

You may need to change the device if you have a different device name.

If there is nothing running in the interpreter then this will give you the REPL (Python shell). If there is any code running then it will give you the serial console for the Arduino (any print statements etc.).

To perform a directory listing use:
import os
os.listdir()

To run a program then use:
exec(open("testpixels-mpy.py").read())

To exit out of minicom use:
CTRL A - Q

Adafruit Ampy - file transfers

To install Ampy then use sudo pip3 install adafruit-ampy.

You can transfer a file using:
ampy --port /dev/ttyACM0 put testpixels-mpy.py

You should change the port device name and filename as appropriate.

Potential Conflicts

Be aware of potential conflicts if using multiple editors or the command line. If you leave one connected to the serial device then it will block the others from accessing the Arduino. This caused a problem with me when I'd forgotten a minimised window running minicom and I couldn't work out why OpenMV wouldn't transfer the MicroPython interpreter.

Download Example Source Code

Download the example source code below

Summary

In summary I’ve shown how you can install MicroPython on the device and program it using either OpenMV (as recommended by Arduino) or the upstream version of MicroPython with the Thonny editor.

I think MicroPython is really good, but one thing to be aware of is that the documentation is perhaps not as good as the C++ documentation. Sources of documentation are available below:

Personally I prefer the Thonny editor to OpenMV, but if you want the official Arduino version of MicroPython then that is best installed through OpenMV (which also provides the code examples) and then switch to Thonny for editing the code.

More information

Blog links and videos on programming

Previous Bitmap Sprites on Raspberry Pi Pico
Bitmap Sprites on Raspberry Pi Pico
Next Access Flash Memory on Arduino RP2040
Access Flash Memory on Arduino RP2040