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 .py files
  • Hardware modules: machine, network, utime, ujson, urequests
  • Platform-specific libraries for each supported chip

Supported Boards

BoardChipNotes
Raspberry Pi PicoRP2040Recommended beginner board
Raspberry Pi Pico WRP2040 + CYW43Pico with Wi-Fi
Raspberry Pi Pico 2RP2350Faster, more RAM
ESP32 / ESP32-S3 / ESP32-C3Xtensa / RISC-VWi-Fi + BLE built-in
ESP8266XtensaLegacy, low-cost
STM32 (select boards)Cortex-Mpyboard variants

MicroPython vs. Arduino vs. ESP-IDF

FeatureMicroPythonArduinoESP-IDF
LanguagePythonC/C++C/C++
Setup complexityVery easyEasyModerate
PerformanceModerateHighHighest
Memory footprintMediumLowLow
Hardware accessVia machine moduleVia Arduino APIFull direct access
RTOSNo (cooperative)NoFreeRTOS
Best forRapid prototyping, learningSimple projectsProduction 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

ToolTypeBest For
ThonnyGUI IDEBeginners, visual file manager
mpremoteCLIAdvanced users, automation
rshellCLIFile management, scripting
VS Code + PymakrIDE extensionProfessional workflow

Key MicroPython Modules

python
1# Hardware control
2from machine import Pin, ADC, I2C, SPI, UART, PWM, Timer, WDT
3
4# Time
5import time
6import utime
7
8# Networking (ESP32 / Pico W)
9import network
10import urequests # HTTP requests
11import ujson # JSON parsing
12
13# File system
14import uos # OS operations (ls, mkdir, etc.)
15import uio # IO operations
16
17# Math and data
18import umath # Math functions
19import ustruct # Pack/unpack binary data
20import ubinascii # Base64, hex encoding

MicroPython REPL Cheat Sheet

Connect to your board's serial port and you get an interactive Python shell:

python
1# Help
2help() # General help
3help(machine) # Module-specific help
4help(Pin) # Class-specific help
5
6# Quick GPIO test without writing a file
7from machine import Pin
8p = Pin(2, Pin.OUT)
9p.on() # LED on
10p.off() # LED off
11p.toggle() # Toggle
12
13# Check free memory
14import gc
15gc.collect()
16print(gc.mem_free()) # Available heap in bytes
17
18# List files on the board
19import uos
20uos.listdir('/')

Next Steps

Pick your setup guide:

Previous
C/C++ SDK Setup