MicroPython
A lean Python implementation for microcontrollers with hardware access.
MicroPython is a software implementation of the Python 3 programming language, written in C and optimized for microcontrollers. It includes a Python compiler that produces bytecode and a runtime interpreter for that bytecode, along with an interactive prompt called the REPL. The project is available on GitHub under the MIT License.
Quick Facts
- Developer
- Damien P. George
- Released
- 3 May 2014
- Platform
- ARM Cortex-M
- STM32
- ESP8266
- ESP32
- 16-bit PIC
- Unix
- Microsoft Windows
- Zephyr
- JavaScript
- RP2040
- License
- MIT license
Facts from the source article.
Did You Know?
- MicroPython includes a cross compiler that generates bytecode with the file extension .mpy.
- MicroPython firmware can be built without the compiler, leaving only the virtual machine to run precompiled mpy programs.
- The main uses of MicroPython are for educational purposes, and for developing and testing device and sensor designs.
History
MicroPython was created by Australian programmer Damien George after a successful Kickstarter campaign in 2013, which released the pyboard development board powered by an STM32F4. The project has since expanded to support many ARM-based architectures, including ARM Cortex-M, RP2040, TI CC3200, Teensy, Nordic nRF series, SAMD21, and SAMD51, as well as ESP8266, ESP32, 16-bit PIC, Unix, Windows, Zephyr, and JavaScript. In 2016, a version for the BBC Micro Bit was produced as part of the Python Software Foundation's contribution to the Micro Bit partnership. In July 2017, MicroPython was forked to create CircuitPython, which emphasizes education and ease of use; as of version 4.0, CircuitPython is based on MicroPython 1.9.4. In 2017, Microsemi created a port for RISC-V architectures. A version for the Lego Mindstorms EV3 appeared in April 2019, and a port for the RP2040 was made in January 2021.
Features
MicroPython allows users to run Python code that can directly access and interact with microcontroller hardware, offering capabilities not available in normal Python applications on an operating system. It supports more than 80% of the features of Python's most used standard libraries. The use of a hardware abstraction layer (HAL) enables code to be portable among different microcontrollers within the same family or platform. Developers can create frozen modules from tested code, which are saved to a microcontroller's modules directory and made available via the import command, avoiding repetitive downloads. The read–eval–print loop (REPL) lets developers enter individual lines of code and run them immediately over a serial USB connection. Helpful REPL commands include CTRL for keyboard interrupt, CTRL for reload, a help message, a command to list built-in modules, and ENTER to list all available pins on the microcontroller board.
Syntax and semantics
MicroPython adopts Python's syntax, which uses less punctuation and fewer syntactical machinations to prioritize readability. Code blocks for functions, conditions, or loops are indicated by indentation rather than symbols or keywords, so misused indentation can cause errors or incorrect execution. A colon marks the end of a condition statement, and the indent size is one tab or four spaces. MicroPython includes three types of libraries: those derived from standard Python libraries, specific MicroPython libraries, and libraries for hardware functionality. Some standard Python libraries have renamed equivalents in MicroPython, with less popular features removed or modified to save memory. The language is highly configurable, and library availability may differ between boards. Files ending with .py take precedence over other library aliases, allowing users to extend existing libraries.
Supporting hardware
MicroPython runs on a growing number of microcontroller boards, with two types: boards that have MicroPython loaded when manufactured, and boards whose firmware allows MicroPython to be installed. To run a program, a file is created and copied onto the microcontroller's flash drive, which appears when the board is connected to a computer. The board typically contains boot.py and main.py files; main.py can be modified to run a program on every boot, otherwise programs are run via the REPL. The official pyboard includes a microcontroller with CPU, flash ROM, and RAM, a microUSB connector, a micro-SD card slot, IO pins, switches, LEDs, servo ports, a real time clock, and an accelerometer. Its internal flash filesystem is named /flash, and an inserted microSD card is accessible as /sd. By default, the pyboard boots from /sd if an SD card is present, otherwise from /flash; creating a file /flash/SKIPSD forces booting from flash. When powered up or reset, the pyboard executes boot.py, configures USB, and then runs the Python program.
More in Python
Spotted an error? Know more?
Reader corrections go straight into our review queue. Suggest an edit · How this site is sourced
