MicroPython
Introduction to MicroPython
MicroPython is a lean implementation of Python 3 designed to run on microcontrollers. It gives you the simplicity and readability of Python with direct access to hardware peripherals — GPIOs, I2C, SPI, UART, PWM, and more.
What is MicroPython?
MicroPython compiles and runs Python code directly on a microcontroller's bare metal, without an operating system. It includes:
- A Python 3 runtime (subset of CPython)
- A REPL (Read-Eval-Print Loop) over serial — type Python commands live
- A small filesystem to store and run
.pyfiles - Hardware modules:
machine,network,utime,ujson,urequests - Platform-specific libraries for each supported chip
Supported Boards
| Board | Chip | Notes |
|---|---|---|
| Raspberry Pi Pico | RP2040 | Recommended beginner board |
| Raspberry Pi Pico W | RP2040 + CYW43 | Pico with Wi-Fi |
| Raspberry Pi Pico 2 | RP2350 | Faster, more RAM |
| ESP32 / ESP32-S3 / ESP32-C3 | Xtensa / RISC-V | Wi-Fi + BLE built-in |
| ESP8266 | Xtensa | Legacy, low-cost |
| STM32 (select boards) | Cortex-M | pyboard variants |
MicroPython vs. Arduino vs. ESP-IDF
| Feature | MicroPython | Arduino | ESP-IDF |
|---|---|---|---|
| Language | Python | C/C++ | C/C++ |
| Setup complexity | Very easy | Easy | Moderate |
| Performance | Moderate | High | Highest |
| Memory footprint | Medium | Low | Low |
| Hardware access | Via machine module | Via Arduino API | Full direct access |
| RTOS | No (cooperative) | No | FreeRTOS |
| Best for | Rapid prototyping, learning | Simple projects | Production firmware |
MicroPython Workflow
text
1. Write .py code → 2. Copy to board → 3. Code runs on boot
Or interactively via the REPL:
text
1. Connect serial → 2. Type Python → 3. Instant feedback
Development Tools
| Tool | Type | Best For |
|---|---|---|
| Thonny | GUI IDE | Beginners, visual file manager |
| mpremote | CLI | Advanced users, automation |
| rshell | CLI | File management, scripting |
| VS Code + Pymakr | IDE extension | Professional workflow |
Key MicroPython Modules
python
1# Hardware control2from machine import Pin, ADC, I2C, SPI, UART, PWM, Timer, WDT34# Time5import time6import utime78# Networking (ESP32 / Pico W)9import network10import urequests # HTTP requests11import ujson # JSON parsing1213# File system14import uos # OS operations (ls, mkdir, etc.)15import uio # IO operations1617# Math and data18import umath # Math functions19import ustruct # Pack/unpack binary data20import ubinascii # Base64, hex encodingMicroPython REPL Cheat Sheet
Connect to your board's serial port and you get an interactive Python shell:
python
1# Help2help() # General help3help(machine) # Module-specific help4help(Pin) # Class-specific help56# Quick GPIO test without writing a file7from machine import Pin8p = Pin(2, Pin.OUT)9p.on() # LED on10p.off() # LED off11p.toggle() # Toggle1213# Check free memory14import gc15gc.collect()16print(gc.mem_free()) # Available heap in bytes1718# List files on the board19import uos20uos.listdir('/')Next Steps
Pick your setup guide:
- Thonny IDE Setup → (Recommended for all users)
- ESP32 — Flash & Connect →
- mpremote CLI Guide →

