Getting Started with Python on the Arduino RP2040 Connect
2022-01-12 | By Maker.io Staff
Arduino’s take on an RP2040-enabled development board yielded a fantastic IoT development platform suitable for all sorts of DIY projects. A recent article investigated the main features of the Arduino RP2040 Connect, and this article introduces you to one way of programming the development board. This should get you up and running so that you can get started writing Python programs for the Arduino RP2040 Connect in practically no time at all.
An overview of the Arduino RP2040 Connect development board.
Python on the Arduino RP2040 Connect
The Arduino RP2040 Connect is one of currently four Arduino development boards that support MicroPython — a subset of the popular Python programming language specifically designed to run on microcontrollers. Python differs greatly from C++, the programming language you typically use to program an Arduino board through the Arduino IDE.
Apart from the programming language, it is unlikely that you will notice any differences between Python and C++ when programming the RP2040 Connect. You should, however, remember that the Arduino IDE compiles your C++ code before sending it to the Arduino board. Simply speaking, compiling the code means translating it into a bit pattern that the microcontroller natively understands. MicroPython, on the other hand, doesn’t use a compiler. Instead, it’s the MCU’s task to interpret the Python commands. The Python IDE doesn’t translate your source code but rather uploads it to a compatible Arduino as is. You have to install a compatible interpreter on the Arduino RP2040 Connect to program it is using MicroPython.
How to Install the MicroPython Interpreter on the Arduino RP2040 Connect
You need to install the OpenMV firmware before you can upload MicroPython programs to the Arduino RP2040 Connect. Before you begin, make sure that you’ve downloaded and installed the most recent version of the OpenMV IDE on your computer. Then, make the Arduino switch to its bootloader mode. To do that, plug the development board into your computer and then double-tap the reset button on the Arduino. You should see that your computer now detects the RP2040 Connect as a mass-storage device. If that didn’t work, unplug the Arduino from your computer, place a small jumper wire between the REC and GND pins, and then reconnect the development board to your computer. It should then show up as a mass storage device. Remove the jumper wire once your computer correctly detects the Arduino.
Then, start the OpenMV IDE and click the “Connect” button in the bottom left corner. If your PC correctly detected the Arduino, you should now see a pop-up window. Select “Install the latest firmware” and then click “OK”:
You can install the OpenMV firmware on your Arduino RP2040 Connect when you first use the IDE to establish a connection to the development board.
The IDE then installs the OpenMV firmware on your Arduino RP2040 Connect. It informs you once the installation process finishes. Note that the Arduino IDE won’t detect your RP2040 Connect board once you install the OpenMV firmware, and the process erases all data previously stored on the development board. If you want to upload sketches to your Arduino using the Arduino IDE, you’ll have to put the board back into bootloader mode as described above before uploading a sketch.
Write Your First MicroPython Script for the Arduino RP2040 Connect
Once you’ve flashed the OpenMV firmware on the RP2040 Connect, the IDE on your PC should detect the board and display a green play button in place of the connect button you used earlier. If that’s not the case, try to re-connect the Arduino to your computer.
Once you flashed the firmware and re-connected the Arduino to your computer, you should see these two buttons that allow you to disconnect the development board and upload a MicroPython script to it.
You can then write your Python script in the editor window and use the green play button to upload the script to your Arduino board. If you need a quick primer on how the Python programming language works, I recommend reading this series of articles. Besides the articles, I highly recommend you have a look at the official Arduino Python API. It explains the standard modules and Arduino-specific features in great detail. For now, you can upload the following MicroPython script to your Arduino RP2040 Connect to test whether the board and the previously installed firmware work as intended:
import time from machine import Pin # This short script serves as a sanity check. # It makes the onboard LED blink led = Pin(6, Pin.OUT) while (True): led.on() time.sleep_ms(250) led.off() time.sleep_ms(250)
This short script is the Python equivalent of the blink example sketch you can find in the Arduino IDE, and that comes pre-loaded on every Arduino board. It turns the Arduino's built-in LED on, waits for 250 milliseconds, then turns the LED off before repeating the cycle after another 250 milliseconds.
Apart from this simple sanity-check script, you can also find a few example programs specifically developed for the Arduino RP2040 Connect:
The OpenMV IDE comes with a few test programs specifically designed for supported Arduino boards.
Summary
Getting started with MicroPython on the Arduino RP2040 Connect is a user-friendly experience. First, you need to install the OpenMV firmware on the board. It enables the Arduino to interpret your custom MicroPython scripts. Before you can install the firmware, put the development board into bootloader mode after plugging it into your computer. To do that, double-tap the reset button. The OpenMV IDE automatically detects the Arduino and lets you flash the firmware with the click of a single button.
Once the IDE installs the firmware, you can use it to write and upload MicroPython scripts to your Arduino RP2040 Connect development board. Refer to the official Arduino MicroPython API for additional information and a description of the supported modules.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum