MicroPython

v1.27.0
MicroPython is a lean and efficient implementation of the Python 3 programming language specifically optimized to run on microcontrollers and constrained embedded environments. It provides a complete Python compiler and runtime, a cross-compiler for bytecode, and a robust set of hardware-abstraction modules, allowing developers to write high-level code for hardware with as little as 16kiB of RAM.

Features

  • 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, and mqtt.
  • 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 (111)

View All 111 Projects →

SparkFun Python Examples

A comprehensive collection of Python examples and documentation for SparkFun Qwiic devices and development boards. It provides setup guides and code samples for both Linux-based single-board computers like the Raspberry Pi and various microcontroller platforms using MicroPython.

micropython
17

SparkFun MicroPython-OpenCV

A MicroPython port of the OpenCV computer vision library, specifically optimized for embedded devices like the Raspberry Pi RP2350. It provides a subset of OpenCV functions for image processing, filtering, and object detection within a Python environment on microcontrollers. The project integrates with the ulab library for NumPy-like array operations and requires hardware with PSRAM for effective use.

micropython
23

Stick Firmware

A MicroPython-based firmware for M5Stack devices including the M5StickC Plus 2 and Cardputer. It provides a suite of features such as an app launcher, file explorer, IMU-based screen rotation, and RTC synchronization.

micropython

SemiTO-V MicroPython Compatibility Layer (MCL)

A Python library that allows developers to include and control MicroPython code targeting microcontrollers directly from a CPython environment. It provides a seamless interface for managing MCU peripherals and variables over serial connections, specifically optimized for RISC-V hardware like the RP2350.

micropython
20

PicoShell

A lightweight, UNIX-style command-line interface for the Raspberry Pi Pico W 2 (RP2350) built with MicroPython. It provides features for filesystem management, Wi-Fi networking, Telnet access, and system monitoring on embedded hardware.

micropython
15

uht — micro HTTP Toolkit

A minimal HTTP/1.0 server designed for resource-constrained devices like the ESP32 and Raspberry Pi Pico. It provides an asynchronous routing system compatible with MicroPython and CircuitPython for building lightweight web applications on embedded hardware.

micropython
13

ESP32 GPS Gateway with RTK/NTRIP Support

A MicroPython-based gateway for ESP32 devices that interfaces with serial GPS modules to provide RTK and NTRIP services. It supports Bluetooth LE, ESPNow proxying, and multiple NTRIP modes including Client, Server, and Caster for high-precision positioning.

freertos micropython
14

Pico Claw Machine

A DIY claw machine project built using a Raspberry Pi Pico W and MicroPython. It features three-axis movement control using DC motor controllers and interfaces with a standard replacement claw mechanism and arcade-style controls.

micropython
19