MicroPython on ESP32 offers Arduino users a faster, more interactive way to develop IoT projects using Python instead of C++. If you are comfortable with Arduino but curious about Python, this getting started guide bridges the gap, showing how familiar Arduino concepts translate to MicroPython and where the Python approach truly shines, especially for WiFi, web servers, and rapid prototyping.
Table of Contents
- Why Switch from Arduino to MicroPython
- Flashing MicroPython Firmware
- The REPL: Interactive Hardware Control
- GPIO Control: Arduino to MicroPython Translation
- WiFi Project: Web-Controlled LED
- Reading Sensors in MicroPython
- Frequently Asked Questions
- Conclusion
Why Switch from Arduino to MicroPython
MicroPython brings several advantages over Arduino C++ for certain project types. The REPL (Read-Eval-Print Loop) lets you test code interactively, typing commands and seeing results instantly without the compile-upload cycle. Python syntax is more concise and readable than C++, with no semicolons, type declarations, or header files. String handling, JSON parsing, and HTTP requests are dramatically simpler in Python. WiFi and web server code that takes 50 lines in Arduino C++ takes 10 lines in MicroPython.
The trade-off is that MicroPython is slower than compiled C++ for computation-heavy tasks. For sensor reading, WiFi communication, and display updates, the speed difference is imperceptible. For high-speed data acquisition or precise timing (like controlling NeoPixel LEDs), Arduino C++ remains superior.
Flashing MicroPython Firmware
Download the MicroPython firmware for ESP32 from micropython.org. Install esptool: pip install esptool. Connect your ESP32 via USB. Erase the existing firmware: esptool.py --port COM3 erase_flash. Flash MicroPython: esptool.py --port COM3 write_flash -z 0x1000 esp32-firmware.bin. After flashing, the ESP32 reboots into MicroPython.
The REPL: Interactive Hardware Control
Connect to the ESP32’s serial port using Thonny IDE (the recommended IDE for MicroPython beginners) or any serial terminal at 115200 baud. You will see the MicroPython prompt: >>>. Type from machine import Pin; led = Pin(2, Pin.OUT); led.on() and press Enter. The onboard LED turns on immediately. No compilation, no uploading. Type led.off() to turn it off. This interactive approach is revolutionary for learning and debugging.
Thonny IDE provides a complete MicroPython development environment with a code editor, REPL terminal, and file manager for the ESP32’s filesystem. Install Thonny from thonny.org and select “MicroPython (ESP32)” as the interpreter in Tools, then Options, then Interpreter.
GPIO Control: Arduino to MicroPython Translation
Arduino’s pinMode() becomes Pin(number, Pin.OUT) or Pin(number, Pin.IN). digitalWrite() becomes pin.on() and pin.off() or pin.value(1) and pin.value(0). digitalRead() becomes pin.value(). analogRead() becomes from machine import ADC; adc = ADC(Pin(34)); adc.read(). analogWrite() (PWM) becomes from machine import PWM; pwm = PWM(Pin(2)); pwm.duty(512).
The conceptual mapping is straightforward. If you understand Arduino, you can translate to MicroPython by learning the equivalent object-oriented API. The MicroPython documentation at docs.micropython.org provides complete API reference.
WiFi Project: Web-Controlled LED
This is where MicroPython shines. A complete web server that controls an LED takes about 20 lines of Python. Connect to WiFi with import network; wlan = network.WLAN(network.STA_IF); wlan.active(True); wlan.connect('SSID', 'password'). Create a simple socket server that listens for HTTP requests. Parse the URL to determine if the request is to turn the LED on or off. Send back an HTML page with toggle buttons. The entire project can be typed interactively in the REPL for instant feedback.
Reading Sensors in MicroPython
MicroPython includes built-in support for common sensors. DHT sensors: from dht import DHT22; d = DHT22(Pin(4)); d.measure(); d.temperature(); d.humidity(). For I2C sensors like BME280, use community libraries available through the upip package manager or copy the library file to the ESP32’s filesystem via Thonny. The sensor reading code is more concise than Arduino equivalents and the REPL allows real-time sensor testing without upload cycles.
Frequently Asked Questions
Is MicroPython slower than Arduino C++?
Yes, typically 10 to 100 times slower for raw computation. However, for IoT applications where the ESP32 spends most time waiting for sensor readings, WiFi responses, or user input, the speed difference is not noticeable. The development speed advantage of Python (faster coding, interactive testing) far outweighs the execution speed disadvantage for most projects.
Can I use Arduino libraries in MicroPython?
No. Arduino libraries are written in C++ and cannot be used in MicroPython. However, MicroPython has its own ecosystem of libraries for common sensors and modules. For popular sensors, MicroPython libraries are readily available.
Can I go back to Arduino after flashing MicroPython?
Yes. Simply flash your Arduino sketch through the Arduino IDE as usual. The Arduino upload process overwrites the MicroPython firmware. You can switch between MicroPython and Arduino at any time by flashing the appropriate firmware.
Conclusion
MicroPython on ESP32 is the fastest path from idea to working IoT project. The interactive REPL, concise Python syntax, and built-in WiFi support make prototyping remarkably fast. For Arduino users, the transition is smooth since the hardware concepts remain identical. Start with simple GPIO control, build a WiFi-connected sensor project, and experience why MicroPython is becoming the preferred platform for IoT development.
Get ESP32 boards and sensors for MicroPython projects at Zbotic.in with fast delivery across India.
Add comment