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 (126)

View All 126 Projects →
esp-lvgl

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.

freertos lvgltft-espimicropython
11

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
17
MicroPython SSD1306 Image Display

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.

micropython
45
DIY Arcade Machine

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.

micropython
18

M5Stack Cardputer Virtual REPL

A virtual REPL environment for the M5Stack Cardputer that enables interactive Python coding directly on the device's keyboard and screen. It provides command line history, editing functions, and automatic execution of scripts on boot. The project is designed for CircuitPython and includes support for I2S audio playback and SD card integration.

micropython
20
mBot2 and CyberPi Programming Examples

mBot2 and CyberPi Programming Examples

A comprehensive collection of programming examples and projects for the mBot2 robot and CyberPi control board. It provides support for both MicroPython and Arduino (PlatformIO) development, covering hardware features such as motion sensing, AI, networking, and motor control.

micropythonplatformio-platformio-core
19

RP2040 Pico W ESP8285 WiFi Library

A MicroPython library designed for Raspberry Pi Pico boards equipped with an ESP8285 WiFi chip instead of the standard Infineon module. It provides an Arduino-inspired API for managing WiFi connections, TCP/SSL client sockets, and data transmission via an AT-command interface.

micropython
15

NeoDucky

NeoDucky is a compact and efficient BadUSB/RubberDucky device powered by CircuitPython and the Adafruit NeoKey Trinkey. It supports automated OS detection, stealth mode operation, and a flexible payload syntax for HID injection attacks.

micropython
29