MicroPython implements the Python 3 programming language for microcontrollers and microcomputers. It is a firmware solution designed to implement high-level language features of Python into low-level hardware platforms. The firmware is optimized to run in constrained environments while allowing a small subset of Python 3 standard libraries into embedded programming.
MicroPython firmware can run in a footprint of 256 Kilobytes and 16 Kilobytes of RAM. MicroPython can offer several high-level programming features like arbitrary precision integers, object-oriented programming, generators, closures, exception handling, and list comprehension, even with such limited code space and memory. Usually, such features are not available in typical microcontroller programming. All these features are available with access to low-level hardware via the machine module of the MicroPython firmware.
Plus, MicroPython compiler and runtime code includes an interactive prompt called REPL (Read-Eval-Print-Loop) that allows executing commands from a desktop computer directly on an embedded platform. The commands using REPL are executed via UART, where the target microcontroller board is connected to the host PC over USB. This is useful in quick and real-time testing and debugging of embedded systems.
One of the notable high-level language features allowed in MicroPython is multithreading. Typically, bare metals are void of multithreading capabilities. The multithreading feature of MicroPython firmware allows speeding up embedded applications by handling many embedded tasks simultaneously from a single microcontroller and lets you think out of the typical programming loops.
MicroPython is open source
MicroPython is available for general use under MIT License. Many of its modules and libraries are also available under MIT license or similar open source license. MicroPython is free to use for both educational and commercial purposes. Anybody can use and modify the firmware. It is an open-source project with source code available on Github. Being an open-source project, MicroPython is open for community contributions. If you are looking for a firmware version targeting a specific hardware platform, microcontroller, port, or feature, check out the official MicroPython download link.
MicroPython vs. Python
MicroPython is largely an implementation of Python 3.4 with some select Python 3.5 and above features. Many standard CPython libraries have been rewritten in C99 to run within constraint environments of bare metals (microcontrollers and microcomputers without any operating system) as part of MicroPython. These micro-fied standard Python libraries form the core of MicroPython firmware. Not all standard Python libraries have been rewritten to form MicroPython; MicroPython is a subset of standard Python. It includes the re-implementation of only those Python libraries that may be useful in programming embedded systems or programmatically enhancing embedded applications.
MicroPython may differ in syntax and behavior with equivalent operators, functions, and modules of standard Python at many places. A lot of these conflicts are listed at the following link. Except for a few conflicts listed in the link mentioned above, most of the MicroPython syntax and programming is compliant with standard Python documentation.
A microcontroller board, RTOS, or operating system supported by MicroPython is called a MicroPython Port. It is important to know that many features of core MicroPython may be unavailable for a particular port, i.e., it may not be supported in a given microcontroller, RTOS, or operating system. Plus, a particular port may have additional modules and functionalities of MicroPython. For example, the MicroPython version for ESP32 and ESP8266 has additional libraries and functions available to handle network connections and supports WebREPL (REPL over web sockets).
Supported Boards and MicroPython ports
MicroPython is available for several common platforms, including x86, x86-64, ARM-Thumb, and Xtensa. It is remarkably fast, with code execution possible at speeds up to 168 MHz. The official microcontroller board supported by MicroPython is pyboard. The pyboard has the following salient features.
-> STM32F405RG microcontroller
-> 168 MHz Cortex M4 CPU with hardware floating point
-> 1024KiB flash ROM
-> 192KiB RAM
-> Micro SD card slot
-> Micro-USB port
-> On-board 3-axis accelerometer and RTC
-> 29 GPIO; 3x 12-bit ADC available on 16 pins 4 with analog ground shielding and 2x 12-bit DAC available on pins X5and X6.
Not just the pyboard, MicroPython can be used on many other microcontroller boards of STMicroelectronics. Some of the microcontrollers of STMicroelectronics having full-support of MicroPython firmware are listed below.
-> Espruino Pico (STM32F401CD)
-> BLACK STM32F407VET6 (STM32F407VE)
-> BLACK STM32F407ZET6 (STM32F407ZE)
-> BLACK STM32F407ZGT6 (STM32F407ZG)
-> FEZ Cerb40 II (STM32F405RG)
-> G30HDR Module (STM32F401RE)
-> FEZ Lemur (STM32F401RE)
-> Netduino Plus 2 (STM32F405RG)
-> OpenMV (STM32F765)
-> HY-STM32F4xxCore144 Core/Dev Board (STM32F407ZGT6)
-> HydraBus V1.0 (STM32F405RG)
-> Olimex STM32-405STK (STM32F405RG)
-> Olimex STM32-E407 (STM32F407ZG)
-> NUCLEO F401RE (STM32F401RE)
-> STM Discovery STM32F401 (STM32F401VC)
-> STM Discovery STM32F407 (STM32F407VG)
-> STM Discovery STM32F429 (STM32F429ZI)
-> STM Discovery STM32F746G (STM32F746NG)
-> STM32F407VET6 Mini (STM32F407VE)
Apart from the above-listed microcontrollers of STMicroelectronics, MicroPython supports the following notable embedded platforms.
-> WiPy and CC3200
-> Pyboard D-Series
-> Teensy 3.X
-> Adafruit Circuit Playground Express
-> Arduino Due
-> Cypress FreeSOC
-> Raspberry Pi Pico
Zephyr is one of the real-time operating systems supported by MicroPython. MicroPython can run as a thread in other RTOS like pyRTOS and FreeRTOS.
The core MicroPython includes implementation of several standard Python 3.4 libraries, which consists of an array, binascii, builtins, cmath, collections, errno, gc, hashlib, heapq, io, json, math, os, random, re, select, socket, ssl, struct, sys, time, uasyncio, zlib, and _thread. Other libraries included in MicroPython are hardware-specific. These are called micro-libraries, including Bluetooth, btree, cryptolib, framebuf, machine, micropython, neopixel, network, and uctypes.
Different microcontroller boards and RTOS offer additional features and core microcontroller operations. MicroPython includes libraries specific to particular microcontroller boards and real-time operating systems to exploit their exclusive features. For example, the port-specific version of MicroPython for pyboard includes libraries like pyb, stm, and lcd160cr. The port-specific version of MicroPython for WiPy includes wipy library and additional classes like ADCChannel, TimerChannel, TimerWiPy, and ADCWiPy. The port-specific version of MicroPython for ESP8266 and ESP32 boards includes esp and esp32 packages. The rp2 package is additionally included in the MicroPython version for Raspberry Pi Pico. An additional module, zephyr, comes packaged in the MicroPython version for Zephyr RTOS.
Additional Python libraries from micropython-lib
The core MicroPython modules are called umodules in the MicroPython terminology. Apart from the select standard Python libraries rewritten to form core MicroPython, many other standard Python libraries have been rewritten in MicroPython projects. These libraries can be accessed as a separate distribution in the form of micropython-lib. Most of these libraries/modules use FFI for OS-specific functionalities. That is why micropython-lib can be generally used only on hosts with POSIX-compatible operating systems like Linux, FreeBSD, Solaris, MacOS, etc. It has limited support for Windows. Unlike standard MicroPython modules, micropython-lib modules need to be installed either using upip or copying unzipped packages manually.
Extending MicroPython libraries
It is also possible to extend the implementation of built-in MicroPython libraries. Any user-defined module named module.py overwrites the standard MicroPython umodule within a package. The package can still use the built-in functionalities by importing the u-module directly. Most of the libraries in micropython-lib have been written in the same manner.
MicroPython can be the next revolution in the microcontroller world after the Arduino. First of all, MicroPython has accomplished the major task of introducing Python to microcontrollers and MCU-based embedded systems. Python is the one-stop for developing networked and AI-backed applications. The use of multithreading in embedded applications will be another important milestone set by the MicroPython initiative.
You may also like:
Filed Under: Python, Tutorials