MicroPython and Lua on the ESP32: programming without compiling

How to flash and use MicroPython on the ESP32, what you can and cannot do, the REPL, mpremote, and why Lua (NodeMCU) is no longer worth it.

Also available in: EN, ES

Series Introduction to the ESP32 Part 13 View the series →
MicroPython on the ESP32

There is a way to program the ESP32 where you compile nothing: you flash a firmware, open a console and type Python. In five minutes you have an LED blinking. It is called MicroPython and it is the best on-ramp there is.

The other side: it is slower and not everything is covered. Let’s be honest about it.

What MicroPython is

It is a Python 3 implementation made for microcontrollers. It is small, fits on the ESP32 and gives you a REPL (interactive console) over USB or Wi-Fi.

Flash and connect

Install it with esptool (the firmware is published by micropython.org):

esptool.py --chip esp32 -p /dev/ttyUSB0 erase_flash
esptool.py --chip esp32 -p /dev/ttyUSB0 write_flash -z 0x1000 esp32-*.bin

And to talk to the board, mpremote:

mpremote connect /dev/ttyUSB0
>>> import machine, time
>>> led = machine.Pin(2, machine.Pin.OUT)
>>> while True:
...     led.value(not led.value()); time.sleep(1)

That is it. No compiling, no setup(), no loop(). Perfect for learning.

Upload your code

The ESP32 shows up as a file system (with boot.py and main.py):

mpremote cp main.py :main.py     # copy
mpremote run main.py             # run without copying

main.py runs automatically on boot.

The good

  • Minimal curve if you know Python (or if you do not).
  • Instant iteration: try a line and see the result.
  • Great for teaching and for workshops.
  • Good modules: machine, network, time, socket, json.

The bad (important)

  1. Speed: it is interpreted, so forget fine timing or audio/video processing.
  2. Memory: the interpreter takes part of the RAM; PSRAM helps, but it is not C.
  3. Peripherals: not every ESP32 feature is exposed. If one is missing, you usually drop to C.
  4. Real time: no real priority tasks like in FreeRTOS.
  5. Production: for a serious product, you almost always end up in C/C++ or Rust.

And Lua (NodeMCU)?

It was popular with the ESP8266. Today it is largely parked: less support, fewer libraries, and MicroPython is nicer and more alive. For new projects, I do not recommend it.

Quick comparison

MicroPythonArduinoESP-IDF
CompileNoYesYes
SpeedLowHighHigh
Available RAMLessLotsLots
TasksNo1 loopN tasks
CurveVery lowLowHigh
Best forLearning, prototypesEverythingProducts

When to use it (and when not to)

Yes:

  • You are learning or teaching.
  • You want to try an idea in 10 minutes.
  • The project is a sensor that publishes and does not need speed.

No:

  • You need precise timing or DSP.
  • You are going to production with memory or power constraints.
  • You need a peripheral that is not exposed in Python.

Summary

  • MicroPython: Python on the ESP32, no compiling, with a REPL and a file system. Current version: 1.29.
  • It is the best way to start and to prototype fast.
  • It is slower and uses more RAM: it does not fit everything.
  • Lua/NodeMCU is no longer worth it for new projects.
  • You can start in MicroPython and drop to C/Rust when the project demands it.

Next step: Rust on the ESP32, the other extreme: maximum safety and control. Or go back to the series hub.

Related posts

↑↓ navigate ↵ open Esc close