01ESP32520 KB SRAMheap@0x1000files1from machine import Pin2import time3led = Pin(13, Pin.OUT)4while True:5 led.on()6 time.sleep(0.5)7 led.off()8 time.sleep(0.5)USB>>>Pinbc1^C
Back: the ESP32 board (chip, flash chip, USB port, LED on pin 13)Front: main.py and the laptop, whose front shows the serial terminalTokens: the firmware, the >>> prompt, a typed line, bytecode, a 1 for the LED, main.py and Ctrl-CAn ESP32 board with MicroPython: the ESP32 chip and its heap, a 4 MiB flash chip holding MicroPython (at 0x1000) and a small filesystem, an LED on pin 13, and a laptop on a USB cable. Flash the interpreter once, then type Python at the >>> prompt: each line is compiled to bytecode on the chip and run at once. Save main.py and the board runs it at every reset, blinking the LED once a second until Ctrl-C brings the prompt back.

Python on the chip

  1. MicroPython is Python 3 made small enough for a microcontroller: it fits in 256 KB of code and 16 KB of RAM. Its compiler and runtime run on the ESP32 itself, so the chip can read Python text and run it.

Live at the prompt

On its own

The price

What happens

;; step 1/10 · interpreter
MicroPython  a lean Python 3 for microcontrollers
  needs      256 KB of code space, 16 KB of RAM
  compiler   Python text  -> bytecode   (on the chip)
  runtime    a virtual machine runs the bytecode
ESP32        up to 240 MHz, 520 KB SRAM, 448 KB ROM

The ESP32 chip itself has no flash for programs: the board's 4 MiB flash chip sits beside it (the figure draws it as its own chip). The download page asks you to erase the flash first and to pick the .bin for your board: the ESP32_GENERIC build is for boards with 4 MiB or more of flash. The same blink in Arduino C++ for an ESP32 board is digitalWrite(LED, HIGH); delay(500); and so on, compiled to machine code on the laptop and flashed after every change (see the lesson "From code to chip: compile, link and flash"). It runs faster and has no garbage collector; MicroPython trades that for the live REPL.