Maker.io main logo

How To Get Started with MicroPython on Arduino

2023-07-19 | By Maker.io Staff

Arduino

Arduino is one of the most popular platforms for DIY electronics enthusiasts and makers. However, makers historically were often limited to using C or C++ when developing sketches for official Arduino boards. While third-party, Arduino-compatible devices have supported other programming languages for quite some time, that wasn’t the case for most official Arduino development boards. That is until recently, as Arduino started adding Python support to its boards. This article explores how anyone familiar with writing C/C++ sketches can quickly get started with deploying Python programs on their supported official Arduino boards.

How To Get Started with MicroPython on Arduino

A Short Summary of Python and MicroPython

Python is a high-level programming language that supports multiple coding styles and is best known for its simplicity, reliability, and how beginner-friendly it is. The language is commonly used in maker and DIY projects, data analysis, web development, and machine learning applications. However, as Python is an interpreted language, it is relatively slow and bulky compared to compiled languages, such as C and C++.

MicroPython is a specialized lightweight implementation of Python specifically designed to run efficiently on scaled-down hardware, such as microcontrollers and embedded systems, which is why Arduino has added support for MicroPython.

Supported Boards and Development Environments

Note that, at this point in time, only a handful of official boards support using MicroPython. Additionally, a manual firmware update might be necessary. The Nano 33 BLE and Nano 33 BLE Sense, Nano RP2040, GIGA R1, Nicla Vision, and Portenta H7 devices support MicroPython, however, which is excellent news if you are interested in machine vision and machine learning applications.

You have the option to use Arduino Lab for MicroPython or OpenMV to flash a MicroPython program onto supported Arduino devices. Arduino Lab serves as a lightweight editor well-suited for the majority of makers who seek to write MicroPython code. In contrast, OpenMV IDE specializes in more advanced capabilities, providing a more in-depth focus on machine vision and machine learning.

Getting Started with a Nano RP2040 and OpenMV

I chose the RP2040 for this segment, as it is one of my favorite newer Arduino boards due to its flexibility, versatility, and well-established nano footprint. Note that the firmware flashing process varies slightly depending on the development board. However, the official Arduino website provides details for each device. With the RP2040 powered off, bridge the GND and REC connections on the board’s GPIO pins to force the device into bootloader mode:

How To Get Started with MicroPython on Arduino Bridge the highlighted pins using a jumper wire to force the RP2040 into bootloader mode.

Next, connect the RP2040 to a computer, which should display the development board as a standard USB mass-storage device. Once connected, you can remove the jumper.

In OpenMV, click the connect button located in the IDE’s bottom-left corner:

How To Get Started with MicroPython on Arduino Click the highlighted button to establish a connection to the RP2040.

If the above steps have been executed correctly, doing so should open an additional dialogue window prompting how the IDE should proceed with a device connected in DFU mode. Within this dialogue, select the “install the latest release firmware” option and do not check the option for deleting the internal file system:

How To Get Started with MicroPython on Arduino Use the dialogue window to flash the latest OpenMV firmware onto the Arduino board.

Once the process finishes, the IDE will display a confirmation dialogue, and it should automatically reconnect the RP2040. This connection will be indicated by the play button below the connect button turning green. Please note that flashing the OpenMV firmware onto the RP2040 overwrites any existing sketches in the device’s internal memory.

How To Get Started with MicroPython on Arduino Once done, OpenMV displays a success message.

Uploading a Basic MicroPython Blink Example

The following straightforward MicroPython program mimics the behavior of the standard Arduino blink sketch, often used to test whether a board functions as intended:

Copy Code
import time
from machine import Pin

led = Pin(6, Pin.OUT)

while (True):
    led.on()
    time.sleep_ms(500)
    led.off()
    time.sleep_ms(500)

The first two lines of code import additional MicroPython modules that allow the code to access the board’s GPIO pins and call additional functions, such as sleep_ms. Next, the led variable stores a reference to GPIO pin number six, which connects to the RP2040’s built-in LED.

By default, there are no setup or loop functions as would be present in a standard Arduino sketch. However, using an endless while loop emulates the behavior of the loop function, and any lines of code above that will run exactly once, thereby emulating the setup function.

In this example, the led declaration is the only action that occurs during setup, and the loop turns the LED on, waits half a second, turns the LED off, and waits again before repeating these four instructions endlessly.

Uploading a MicroPython Script to the RP2040 using OpenMV

As mentioned, Python is not a compiled language. Instead, the language relies on an interpreter running on the target hardware that receives the commands encoded in the Python program. Therefore, MicroPython is also not a compiled language — as a result, it’s necessary to upload the OpenMV firmware to the board before uploading a MicroPython program.

At this stage, uploading the MicroPython code to the board requires only a single click of the green play button in the IDE window's bottom-left corner:

How To Get Started with MicroPython on Arduino Click the highlighted button to run the Python code on the Arduino Nano RP2040 Connect.

Doing so will upload the code to the board, and the RP2040’s LED should blink periodically. If the button doesn’t turn green, try double-tapping the button on the RP2040 board until the onboard LED lights up orange. Then, re-flash the firmware.

Summary

Python is a popular high-level programming language known for its simplicity and versatility, widely used in various fields such as data analysis, web development, and machine learning. However, Python is less performant compared to compiled languages like C/C++. MicroPython, a lightweight implementation of Python, is designed explicitly for microcontrollers and embedded systems, making it suitable for Arduino. Currently, only a few official Arduino boards support MicroPython, such as Nano 33 BLE, Nano RP2040, GIGA R1, Nicla Vision, and Portenta H7.

To use MicroPython with a supported Arduino, you must first update the device’s firmware and upload a Python interpreter. This can be achieved by using one of the supported development environments. This article outlines the process using an Arduino Nano RP2040 connect and OpenMV.

制造商零件编号 ABX00030
ARDUINO NANO 33 BLE
Arduino
制造商零件编号 ABX00034
ARDUINO NANO 33 BLE WITH HEADERS
Arduino
制造商零件编号 ABX00052
ARDUINO NANO RP2040 CONNECT
Arduino
制造商零件编号 ABX00063
ARDUINO GIGA R1 WIFI
Arduino
制造商零件编号 ABX00051
NICLA VISION
Arduino
制造商零件编号 ABX00045
PORTENTA H7 LITE
Arduino
制造商零件编号 ABX00046
PORTENTA H7 LITE CONNECTED
Arduino
制造商零件编号 ABX00042
PORTENTA H7
Arduino
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