MicroPython on the ESP32
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
MicroPython puts a Python 3 compiler and runtime on the microcontroller itself. Flash it once onto an ESP32 board, open the >>> prompt over USB, and type Python that runs straight away: two lines light an LED on pin 13. Then save main.py to the board's flash so it blinks the LED at every reset, take control back with Ctrl-C, and see what the convenience costs in speed and memory. The lessons “From code to chip: compile, link and flash” and “setup() and loop(): the superloop” show the C++ way this replaces.
Python on the chip
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.
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
اقرأ الدرس خطوة بخطوة
Python on the chip
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.
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 ROMFlash the interpreter once
First, a laptop tool called esptool copies MicroPython onto the board's 4 MiB flash chip, starting at address 0x1000. That is the only firmware you flash. From then on, your Python is just text the interpreter reads.
on the laptop, once: $ esptool.py erase_flash $ esptool.py --baud 460800 write_flash 0x1000 ESP32_GENERIC.bin flash 4 MiB chip on the board 0x1000 MicroPython firmware after it a small filesystem for your filesOpen the REPL
Plug in USB and open a serial terminal at 115,200 baud. The chip answers with >>>, the REPL prompt. REPL means read, evaluate, print, loop: it reads a line you type, runs it, prints any result and waits again.
$ mpremote (serial terminal, 115200 baud) >>> the chip is waiting R read a line of Python text E evaluate compile it and run it P print show the result L loop wait for the next lineType one line
Type led = Pin(13, Pin.OUT) and press Enter. The text travels down the cable. On the chip the compiler turns it into bytecode, a compact list of steps, and the virtual machine runs them. Now led lives in the heap, in SRAM.
>>> from machine import Pin >>> led = Pin(13, Pin.OUT) laptop sends the text over USB ESP32 compile -> bytecode run -> GPIO 13 becomes an output heap led (a Pin object, in SRAM)The LED lights at once
Type led.value(1). The chip runs it and sets pin 13 high, close to 3.3 volts, and the LED lights. Nothing was compiled on the laptop or flashed: you try an idea and see the answer straight away.
>>> led.value(1) pin 13 high: at least 2.64 V (0.8 x 3.3 V) LED on no compile on the laptop, no flash, no restartSave main.py
Typed lines are gone after a reset. To keep a program, save it as a file: mpremote cp main.py : copies main.py into a small filesystem on the flash chip, beside MicroPython. Files survive power off.
$ mpremote cp main.py : ":" means the board's side of the copy flash chip 0x1000 MicroPython files main.py (kept with the power off) from machine import Pin import time led = Pin(13, Pin.OUT) while True: led.on() time.sleep(0.5) led.off() time.sleep(0.5)A reset runs main.py
At every reset MicroPython runs boot.py if there is one, then main.py. The reset starts with empty memory, so lines 1 to 3 build led again: get Pin, import the time module, make pin 13 an output.
reset -> boot.py (if found) -> main.py (no main.py, or it ends: the >>> prompt) line 1 from machine import Pin line 2 import time line 3 led = Pin(13, Pin.OUT)Blink, forever
Line 4 starts a loop that never ends. led.on() sets the pin high, time.sleep(0.5) waits half a second, led.off() sets it low, and it waits again: one blink per second, with no laptop needed.
line 4 while True: forever line 5-6 on, wait 0.5 s line 7-8 off, wait 0.5 s period 0.5 + 0.5 = 1 s -> 1 blink per secondCtrl-C takes it back
With the cable in, press Ctrl-C in the terminal. MicroPython raises KeyboardInterrupt inside the loop, main.py stops where it was, and the >>> prompt returns. Edit, copy, reset, and try again in seconds.
Ctrl-C -> KeyboardInterrupt in main.py >>> the REPL is back; the LED keeps its last level Ctrl-D -> soft reset: memory cleared, main.py runs againThe price: speed and memory
Convenience costs time. The virtual machine does many machine steps per bytecode; MicroPython's own native mode runs roughly 2 times faster. Every new object takes heap, and the garbage collector that frees it can pause for several milliseconds.
bytecode each step is run by the VM (slow-ish) native machine code: roughly 2x faster, bigger heap every new object takes SRAM garbage collector frees it, pausing several ms for tight timing: C, or native code, or hardware timers
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01After MicroPython is flashed onto an ESP32, what happens to a line of Python you type at the >>> prompt?
MicroPython's compiler and runtime live on the chip. The text goes down the USB serial link, is compiled to bytecode on the ESP32 and run by its virtual machine straight away.
02On the ESP32 download page, at which flash address is the MicroPython firmware written?
The ESP32_GENERIC instructions write the .bin “starting at address 0x1000” with esptool. You do this once; after that your programs are files, not firmware.
03Your board has main.py saved on it. What runs when you press reset?
MicroPython's boot sequence runs boot.py if it is found, then main.py. If there is no main.py, or it finishes, the REPL starts.
04main.py blinks with led.on(), time.sleep(0.5), led.off(), time.sleep(0.5) inside while True. How often does the LED blink?
On for 0.5 s plus off for 0.5 s is a period of 1 s: one blink per second, forever.
05Why might you write a timing-critical part in C, or in MicroPython's native mode, instead of plain MicroPython?
Every bytecode step is carried out by the VM, so it is slower than machine code: MicroPython's own native emitter is roughly 2 times faster. Freeing heap with the garbage collector can take several milliseconds, a pause a tight timing loop may not allow.
المسرد
- MicroPython
- A lean implementation of Python 3 that runs directly on a microcontroller, with its own compiler and runtime.
- Interpreter
- A program that reads code and carries it out step by step. MicroPython is flashed once and then runs your Python files.
- REPL
- Read, evaluate, print, loop: the interactive >>> prompt that runs each line as you type it.
- Bytecode
- A compact list of steps that MicroPython compiles from Python text; its virtual machine carries them out.
- Heap
- The region of RAM where MicroPython keeps objects your program creates, such as led.
- Garbage collector
- The part of MicroPython that frees heap memory no longer in use; it can pause the program while it works.
- esptool
- The laptop tool that writes firmware into an ESP32's flash over USB.
- mpremote
- A laptop tool that opens the board's REPL and copies files such as main.py to and from its filesystem.
الملاحظات والمصادر
- 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 REPL runs over the board's USB serial port. On the original ESP32, GPIO 1 and 3 carry that serial link and GPIO 6, 7, 8, 11, 16 and 17 talk to the flash chip, so the example uses GPIO 13, a free output pin. mpremote opens the same REPL and also copies files.
- The figure shows the compiled line as one token. Really, each line becomes a short run of bytecode instructions; objects such as led live in the heap, a region of SRAM.
- Before boot.py, MicroPython runs a built-in script, _boot.py, that handles setup such as preparing the filesystem on first boot. A soft reset (Ctrl-D or machine.soft_reset()) clears Python memory but keeps the files and some settings, such as network connections.
- 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.
- MicroPython: home page (what it is, size)
- MicroPython download: ESP32_GENERIC (esptool, address 0x1000, 4 MiB flash)
- MicroPython docs: getting started on the ESP32 (REPL at 115200 baud)
- MicroPython docs: ESP32 quick reference (Pin, time, usable pins)
- MicroPython docs: machine.Pin
- MicroPython docs: the REPL (Ctrl-C, Ctrl-D)
- MicroPython docs: reset and boot sequence
- MicroPython docs: mpremote
- MicroPython docs: glossary (bytecode, heap, filesystem)
- MicroPython docs: maximising MicroPython speed
- Espressif ESP32 Series datasheet
اختبار يتاح بعد الخطوة الأخيرة.