MicroPython
v1.27.0Features
-
Implements Python 3.4 syntax including exceptions, ‘with’, and ‘yield from’ statements.
-
Supports ‘async’ and ‘await’ keywords from Python 3.5 for asynchronous programming.
-
Includes a core Python compiler and runtime that can execute scripts directly on-device.
-
Provides a cross-compiler (mpy-cross) to turn scripts into precompiled bytecode (.mpy) for faster execution.
-
Supports freezing Python scripts into the firmware executable to significantly reduce memory usage.
-
Offers hardware-specific modules for low-level control of GPIO, Timers, ADC, DAC, and PWM.
-
Includes built-in support for communication protocols including SPI, I2C, UART, CAN, and I2S.
-
Provides networking capabilities through dedicated socket, ssl, and network modules.
-
Supports multithreading on select hardware ports via the _thread module.
-
Includes a comprehensive asyncio implementation for cooperative multitasking.
-
Features an interactive REPL (Read-Eval-Print Loop) for real-time programming and debugging.
-
Supports on-device filesystems for script storage and data logging.
-
Provides specialized modules for Bluetooth (BLE) and USB Device functionality.
-
Allows extending the language with custom modules implemented in C via the extmod system.
-
Includes advanced hardware support for Pulse Counters and Quadrature Encoders on specific architectures.
-
Targets devices with as little as 256kiB flash and 16kiB RAM.
Architecture
MicroPython is designed with a highly modular architecture that separates the core Python language implementation from platform-specific hardware logic. The system is centered around the py/ directory, which contains the core compiler, runtime, and virtual machine (VM). This core is written in highly portable C and is responsible for parsing Python source code, generating bytecode, and managing the object model and garbage collection.
To support diverse hardware, MicroPython utilizes a Ports system. Each port (found in the ports/ directory) contains the glue code necessary to interface the core VM with a specific microcontroller’s Hardware Abstraction Layer (HAL) or an operating system. Additionally, the extmod/ directory provides optional C-based modules that implement non-core functionality, such as filesystem support, networking stacks, and specialized hardware protocols, ensuring that the core remains lean while allowing for feature-rich builds.
Core Components
- py/: The core Python implementation, including the compiler, runtime, and core library.
- mpy-cross/: A cross-compiler tool used to pre-compile Python scripts into bytecode for embedded execution.
- ports/: Platform-specific code for various architectures (e.g., STM32, ESP32, RP2).
- extmod/: Non-core modules implemented in C for performance and hardware access.
- lib/: Submodules for external dependencies and vendor-provided HALs.
Use Cases
This library is ideal for:
- IoT Development: Rapidly creating connected devices using high-level networking and security modules like
socket,ssl, andmqtt. - Rapid Prototyping: Using the interactive REPL to test hardware peripherals and logic in real-time without the need for a full compile-flash-reboot cycle.
- Education: Teaching Python programming and electronics simultaneously on affordable, physical hardware.
- Industrial Automation: Implementing complex control logic, sensor data processing, and communication protocols (CAN, Modbus) on robust microcontroller platforms.
- Embedded Scripting: Adding a user-accessible scripting interface to existing C/C++ projects by embedding the MicroPython engine.
- Low-Power Monitoring: Utilizing deep-sleep modes and hardware interrupts to create battery-operated sensor nodes.
Getting Started
To begin developing with MicroPython, the first step is typically to build the cross-compiler by running make within the mpy-cross/ directory. Following this, you can navigate to the specific port directory corresponding to your hardware (e.g., ports/stm32/) and run make submodules followed by make to generate the firmware image. Once flashed, the device provides a Read-Eval-Print Loop (REPL) over a serial connection, allowing for immediate Python command execution.
Comprehensive documentation, including API references for hardware-specific modules like machine and network, is available at the official MicroPython documentation site. For community support and project discussions, developers are encouraged to use the GitHub Discussions and the official Discord server.
Related Projects (130)
View All 130 Projects →Oficina de CircuitPython
A comprehensive educational repository for learning CircuitPython on microcontrollers like the Raspberry Pi Pico and ESP32. It covers fundamental electronics, Python programming basics, and advanced topics including NeoPixel control, serial buses, and display integration using DisplayIO.
xoss_sync: Open Source BLE Sync for XOSS Cyclo-computers
A synchronization tool written in CPython and MicroPython for fetching FIT activity files from XOSS and Cycplus cyclo-computers via Bluetooth Low Energy (BLE). It implements the YMODEM protocol over the Nordic UART Service to provide an open-source alternative to proprietary mobile applications.
ESP32 Cheap Yellow Display MicroPython LVGL
A comprehensive resource for running MicroPython and LVGL on the ESP32-2432S028R 'Cheap Yellow Display'. It provides precompiled firmwares for LVGL9 and LVGL8, specialized drivers for ILI9341 and XPT2046, and detailed guides for display calibration and UI development.
MicroPython Camera API for ESP32
A comprehensive camera API for MicroPython targeting ESP32-based boards. It provides a stable interface for various image sensors like the OV2640 and OV5640, supporting both synchronous and asynchronous capture modes with ESP-IDF integration.
esp-lvgl
A comprehensive collection of LVGL (Light and Versatile Graphics Library) examples and tutorials for ESP32 and ESP8266 platforms. It supports multiple development frameworks including Arduino, ESP-IDF, and MicroPython, providing drivers and UI templates for various display types.
micro-repl
A lightweight, SerialPort-based MicroPython REPL for microcontrollers that runs directly in the browser. It provides a fully interactive terminal with support for file uploads, code evaluation, and paste mode for boards like the Raspberry Pi Pico and Arduino Nano ESP32.
MicroPython SSD1306 Image Display
A comprehensive system for driving SSD-1306 OLED displays and converting standard bitmap images into MicroPython-compatible buffers. It features an I2C driver for 128x64 monochrome displays and a Python-based conversion utility for JPG and PNG files.
DIY Arcade Machine
A MicroPython-based arcade system for RP2040 microcontrollers and 64x64 HUB75 LED matrices. It supports physical hardware via the Pimoroni Interstate 75 and a desktop emulator using PyGame. The project includes over 16 classic-inspired games with persistent high scores and memory-optimized graphics handling.