Scripts2026年5月6日·1 分钟阅读

MicroPython — Python for Microcontrollers

Run Python directly on ESP32, RP2040, STM32, and other microcontrollers with an interactive REPL, hardware APIs, and a minimal footprint.

Introduction

MicroPython is a lean implementation of Python 3 designed to run on microcontrollers with as little as 256 KB of flash and 16 KB of RAM. It provides an interactive REPL, standard Python syntax, and hardware-specific modules for GPIO, I2C, SPI, and networking — bringing rapid prototyping to embedded systems.

What MicroPython Does

  • Runs Python 3 on resource-constrained microcontrollers (ESP32, RP2040, STM32, nRF)
  • Provides an interactive REPL over serial or WebREPL for live coding on the device
  • Includes hardware abstraction modules for GPIO, ADC, PWM, I2C, SPI, and UART
  • Supports networking (Wi-Fi, Bluetooth) and filesystem access on flash storage
  • Enables rapid prototyping without a compile-flash-debug cycle

Architecture Overview

MicroPython compiles Python source to bytecode and executes it on a custom virtual machine written in C. The VM runs on bare metal or atop a minimal RTOS. A port layer adapts the VM to each microcontroller family. The frozen module mechanism pre-compiles Python files into the firmware image to save RAM. An internal filesystem (LittleFS or FAT) allows storing scripts on the device flash.

Self-Hosting & Configuration

  • Download the pre-built firmware for your board from micropython.org/download
  • Flash using esptool (ESP32), picotool (RP2040), or DFU (STM32)
  • Access the REPL over USB-serial with any terminal emulator
  • Upload scripts via mpremote, ampy, or WebREPL
  • Configure boot behavior via boot.py and main.py on the device filesystem

Key Features

  • Interactive REPL for live hardware debugging and experimentation
  • asyncio support for concurrent tasks on single-threaded MCUs
  • Native code emitter for performance-critical sections
  • WebREPL for wireless script upload and debugging
  • Frozen modules to reduce RAM usage by embedding bytecode in firmware

Comparison with Similar Tools

  • CircuitPython — Adafruit fork focused on beginner friendliness and USB mass storage workflow; MicroPython supports more boards and lower-level control
  • Arduino (C/C++) — Compiled to native code for maximum performance; MicroPython trades speed for development velocity and interactive debugging
  • Lua (NodeMCU) — Lightweight scripting alternative; MicroPython offers richer standard library and broader community
  • Rust (embedded) — Memory-safe compiled language; MicroPython prioritizes rapid iteration over runtime efficiency

FAQ

Q: Is MicroPython fast enough for real-time applications? A: For most sensor reading, display driving, and networking tasks it is sufficient. For tight timing loops, use the native code emitter or write critical sections as C modules.

Q: How much RAM does MicroPython need? A: The VM runs in as little as 16 KB of RAM, though 64-256 KB is recommended for practical applications with networking.

Q: Can I use pip packages with MicroPython? A: Not directly. MicroPython has its own package index (mip) with compatible libraries. Many popular Python libraries have MicroPython ports.

Q: Which board should I start with? A: The Raspberry Pi Pico (RP2040) or an ESP32-based board are the most popular starting points due to extensive documentation and community support.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产