Introduction
MicroPython lets you write Python code directly on microcontrollers like the ESP32, making IoT development accessible without C++. In 2026, it's ideal for quickly prototyping connected projects. This intermediate tutorial guides you through firmware installation, sensor management, WiFi connectivity, and web server creation. Each step includes complete, working code. You'll learn to avoid common pitfalls like limited microcontroller memory management. By the end, you'll have a solid foundation for professional IoT projects.
Prerequisites
- ESP32 (or MicroPython-compatible board)
- Computer with Python 3.10+
- esptool installed
- Basic Python knowledge
- USB cable and editor like Thonny or VS Code
MicroPython Firmware Installation
pip install esptool
esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20240123-v1.22.1.binThese commands erase the flash and install the MicroPython firmware on the ESP32. Adjust the port and .bin file according to your board and operating system. Always check for the latest firmware version on micropython.org.
Connection and First Test
Once the firmware is installed, connect via the serial interface using Thonny or a terminal. The interactive REPL lets you run code directly on the board.
LED Blinking
from machine import Pin
import time
led = Pin(2, Pin.OUT)
while True:
led.value(1)
time.sleep(0.5)
led.value(0)
time.sleep(0.5)This script turns the built-in LED on and off every 500 ms. Use machine.Pin to control GPIOs. Avoid blocking infinite loops in more complex projects.
Reading a Sensor
Let's add DHT22 sensor reading to measure temperature and humidity. Install the driver if needed via mip.
DHT22 Sensor Reading
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(4))
while True:
try:
sensor.measure()
print('Temp:', sensor.temperature(), '°C')
print('Hum:', sensor.humidity(), '%')
except OSError as e:
print('Sensor error:', e)
time.sleep(2)The code reads the sensor every 2 seconds and handles read errors. The DHT22 requires a delay between measurements to avoid data corruption.
WiFi Connection
import network
import time
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('SSID', 'motdepasse')
while not wlan.isconnected():
time.sleep(1)
print('IP:', wlan.ifconfig()[0])This script establishes a WiFi connection and displays the IP address. Handle reconnections in a real project for better robustness.
Creating a Web Server
To expose data, we create a simple web server with MicroPython.
Simple Web Server
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('Serveur sur', addr)
while True:
cl, addr = s.accept()
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send('<h1>Hello from ESP32</h1>')
cl.close()This server listens on port 80 and returns a basic HTML page. For real projects, use uasyncio to handle multiple clients simultaneously.
Best Practices
- Always use try/except for network and sensor operations
- Manage memory with gc.collect() in long loops
- Separate business logic into distinct modules
- Test on a simulator before deployment
- Document the GPIO pins used
Common Errors to Avoid
- Forgetting to handle OSError exceptions on sensors
- Using blocking delays in WiFi projects
- Neglecting firmware updates
- Ignoring RAM limits (around 100 KB available)
Going Further
Explore uasyncio for asynchronous applications and MQTT for industrial IoT. Check out our Learni courses to deepen your MicroPython and IoT knowledge.