Welcome to this comprehensive guide by YaranaIoT Guru, where we will explore how to unlock the full potential of Arduino Nano ESP32 using MicroPython.
MicroPython is a lightweight Python interpreter for microcontrollers, enabling rapid prototyping and simplified IoT development. Unlike traditional Arduino C++ programming, MicroPython allows you to write code in Python, making it easier for beginners and professionals alike.
πΉ Why MicroPython on Arduino Nano ESP32?
- Simplified programming: Python is easier to read and write than C/C++
- Rapid prototyping: Quickly test IoT ideas without complex Arduino libraries
- IoT ready: ESP32 supports Wi-Fi, Bluetooth, and various peripherals
- Versatility: Supports GPIO, ADC, PWM, I2C, SPI, UART, and more
πΉ Project Goals
By the end of this guide, you will:
- Install MicroPython firmware on Arduino Nano ESP32
- Set up the development environment (Thonny or uPyCraft IDE)
- Connect and test Nano ESP32 using MicroPython
- Write your first MicroPython program controlling LEDs, sensors, or motors
- Understand the basics of MicroPython libraries and syntax
πΉ Applications
- IoT prototyping with Wi-Fi and Bluetooth devices
- Smart home projects with Python scripts
- Sensor data collection and real-time processing
- Robotics and automation projects
- Educational platforms for Python and electronics
πΉ Required Tools & Software
| Tool / Software | Purpose |
|---|---|
| Arduino Nano ESP32 | Microcontroller board |
| USB Cable | Communication with PC |
| Thonny IDE / uPyCraft IDE | MicroPython programming |
| esptool.py | Flashing MicroPython firmware |
| MicroPython Firmware | Official firmware for Nano ESP32 |
| PC (Windows/Linux/macOS) | Development environment |
| Jumper Wires & Breadboard | Optional hardware testing |
| LEDs / Sensors / Motors | For MicroPython test projects |
π§° Step 1: Install Python on Your PC
Before flashing MicroPython, ensure you have Python installed on your PC:
- Go to https://www.python.org/downloads/
- Download the latest Python 3.x version
- During installation, check βAdd Python to PATHβ
- Verify installation:
python --versionThis should display the installed Python version.
π§° Step 2: Install esptool.py
esptool.py is the official flashing tool for ESP32 boards. Install it via command prompt:
pip install esptool
Check installation:
esptool.py version
π§° Step 3: Download MicroPython Firmware
- Visit the official MicroPython downloads for ESP32: https://micropython.org/download/esp32/
- Choose the latest stable release for ESP32
- Download the
.binfirmware file (e.g.,esp32-20231018-v1.21.0.bin)
Keep the firmware file location handy for flashing.
π§° Step 4: Connect Arduino Nano ESP32 to PC
- Use a micro-USB cable to connect Nano ESP32 to your PC
- Note the COM port number (Windows: Device Manager β Ports (COM & LPT))
- Disconnect any other devices to avoid conflicts
π§° Step 5: Erase Existing Flash
Before installing MicroPython, erase the existing Arduino firmware:
esptool.py --port COM3 erase_flash
- Replace
COM3with your Nano ESP32 COM port - This clears the previous Arduino firmware from flash memory
π§° Step 6: Flash MicroPython Firmware
Use the following command:
esptool.py --chip esp32 --port COM3 write_flash -z 0x1000 esp32-20231018-v1.21.0.bin
- Replace
COM3with your port - Replace
.binfile with the firmware you downloaded
β Once completed, your Nano ESP32 now has MicroPython installed.
πΉ Step 7: Verify MicroPython Installation
- Open Thonny IDE or uPyCraft IDE
- Select Python (MicroPython) β ESP32 as your interpreter
- Connect to the correct COM port
- Open a new script and type:
print("Hello MicroPython!") - Run the script β If you see Hello MicroPython! in the console, installation was successful
πΉ Step 8: First GPIO Test (LED Blinking)
- Connect an LED to GPIO2 (with a 220Ξ© resistor to GND)
- Use this code:
from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
while True:
led.value(1) # ON
sleep(1)
led.value(0) # OFF
sleep(1)
- Upload and run β LED should blink every second
- Congratulations! Your Nano ESP32 is now running MicroPython
πΉ Step 1: Connecting ESP32 to Wi-Fi
The Nano ESP32 has built-in Wi-Fi. With MicroPython, connecting to a network is straightforward:
import network
import time
ssid = "YourWiFiName"
password = "YourWiFiPassword"
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
print("Connecting to WiFi...")
time.sleep(1)
print("Connected, IP:", station.ifconfig()[0])
β After running this code, your ESP32 will connect to Wi-Fi and display the IP address β essential for web server projects.
πΉ Step 2: Simple Web Server on ESP32
MicroPython can host a lightweight web server to control devices remotely. Hereβs an example to control an LED:
import socket
from machine import Pin
led = Pin(2, Pin.OUT)
ip = "192.168.1.100" # Replace with your ESP32 IP
html = """<!DOCTYPE html>
<html>
<head><title>ESP32 LED Control</title></head>
<body>
<h2>ESP32 LED Control</h2>
<button onclick="fetch('/?led=on')">ON</button>
<button onclick="fetch('/?led=off')">OFF</button>
</body>
</html>
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
request = conn.recv(1024)
request = str(request)
if '/?led=on' in request:
led.value(1)
if '/?led=off' in request:
led.value(0)
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)
conn.close()
- Upload this script to ESP32
- Open the ESP32 IP in a web browser β Youβll see buttons to turn the LED ON/OFF
This can be expanded to control multiple appliances or relays.
πΉ Step 3: Reading Sensors with MicroPython
MicroPython supports GPIO, ADC, I2C, SPI sensors. Example: DHT11 Temperature & Humidity Sensor
Hardware setup:
- DHT11 β Data pin to GPIO4, VCC to 3.3V, GND to GND
MicroPython Code:
import dht
from machine import Pin
import time
sensor = dht.DHT11(Pin(4))
while True:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temperature:", temp, "Β°C Humidity:", hum, "%")
time.sleep(2)
- Reads temperature and humidity every 2 seconds
- Can be integrated into web dashboard or Firebase for IoT monitoring
πΉ Step 4: Controlling Relays & Appliances
MicroPython makes it easy to control relays for home automation:
from machine import Pin
relay1 = Pin(5, Pin.OUT) # GPIO5
relay2 = Pin(18, Pin.OUT) # GPIO18
# Turn relay ON
relay1.value(1)
relay2.value(1)
# Turn relay OFF
relay1.value(0)
relay2.value(0)
- Combine with web server or Wi-Fi control for remote appliance management
πΉ Step 5: Combining Wi-Fi + Sensors + Actuators
By combining the steps above, you can create IoT projects like:
- Smart home automation
- Temperature-based fan control
- Humidity-controlled irrigation
- Light control via web interface
Example: Turn ON a fan when temperature exceeds 30Β°C
from machine import Pin
import dht
import time
fan = Pin(18, Pin.OUT)
sensor = dht.DHT11(Pin(4))
while True:
sensor.measure()
temp = sensor.temperature()
if temp > 30:
fan.value(1)
else:
fan.value(0)
time.sleep(2)
πΉ 6. Best Practices for MicroPython on ESP32
- Use try/except blocks for sensor errors
- Keep scripts lightweight to avoid memory overflow
- Use NTP for accurate time if using timers
- Modularize code into functions for cleaner programming
- Avoid long blocking loops β Use
time.sleep()minimally
πΉ 1. Firebase Integration with MicroPython
Firebase Realtime Database allows your ESP32 to send and receive data in real-time, enabling remote control and monitoring.
Step 1: Install urequests module
MicroPython doesnβt have native Firebase libraries, but we can use HTTP requests:
import urequests
import json
Step 2: Setup Firebase URL and Secret
FIREBASE_URL = "https://your-project.firebaseio.com/"
DEVICE1_PATH = "device1.json" # Example path
Step 3: Read & Write Data
- Turn ON device from Firebase:
response = urequests.get(FIREBASE_URL + DEVICE1_PATH)
data = response.json()
if data["state"] == 1:
relay1.value(1)
else:
relay1.value(0)
- Update Firebase when device state changes:
payload = {"state": relay1.value()}
urequests.put(FIREBASE_URL + DEVICE1_PATH, json=payload)
This enables two-way synchronization between ESP32 and Firebase β just like advanced home automation systems.
πΉ 2. OTA (Over-The-Air) Updates
MicroPython allows over-the-air updates, which means you can upload new scripts without reconnecting USB:
- Use webrepl module:
import webrepl webrepl.start() - Connect via WebREPL client in a browser
- Upload new
.pyfiles directly to the ESP32
This is perfect for remote debugging and deploying IoT projects.
πΉ 3. Advanced Projects with MicroPython
Now that ESP32 is Wi-Fi-enabled and Firebase-ready, you can create complex projects:
| Project Idea | Description |
|---|---|
| Smart Home Automation | Control lights, fans, or appliances remotely using Firebase and web dashboard |
| IoT Weather Station | Monitor temperature, humidity, and air quality, push data to cloud |
| Security System | Detect motion with PIR sensor, trigger alarms, and send notifications |
| Greenhouse Automation | Control irrigation and fans based on sensor readings |
| Data Logging & Visualization | Push sensor data to Firebase, visualize in web or mobile app |
All these projects use same MicroPython fundamentals: GPIO control, Wi-Fi, sensor reading, and cloud integration.
πΉ 4. Combining Timers & Sensor-Based Automation
You can implement smart timers and sensor rules:
from machine import Pin
import dht
import time
import urequests
fan = Pin(18, Pin.OUT)
sensor = dht.DHT11(Pin(4))
FIREBASE_URL = "https://your-project.firebaseio.com/device1.json"
while True:
sensor.measure()
temp = sensor.temperature()
# Firebase control override
response = urequests.get(FIREBASE_URL)
data = response.json()
if data["state"] == 1 or temp > 30:
fan.value(1)
else:
fan.value(0)
time.sleep(2)
- Fan turns ON either by Firebase command or temperature threshold
- Ensures hybrid control, just like professional IoT systems
πΉ 5. Best Practices & Tips
- Always use try/except for HTTP requests
- Modularize scripts for multiple devices
- Use NTP time for accurate scheduling
- Avoid blocking loops; consider uasyncio for concurrent tasks
- Use WebREPL or OTA for easy updates
πΉ 6. Testing and Troubleshooting
- Verify Wi-Fi connection with
station.isconnected() - Test sensor readings locally before cloud integration
- Check Firebase URL and Secret carefully
- Use serial monitor for debugging
β Once tested, your Arduino Nano ESP32 with MicroPython is ready for full-fledged IoT applications.
πΉ 7. Future Upgrades
- Add OLED/LCD displays for real-time sensor data
- Integrate MQTT protocol for faster cloud messaging
- Combine Bluetooth + Wi-Fi for hybrid IoT control
- Build multi-device networks for smart homes or classrooms
πΉ Conclusion
By following this guide, you now have:
- MicroPython running on Arduino Nano ESP32
- Wi-Fi connectivity and web server control
- Sensor integration and actuator control
- Firebase integration for cloud-based IoT
- OTA updates and advanced project capabilities
This setup transforms your Nano ESP32 into a powerful Python-based IoT platform, capable of remote monitoring, smart automation, and cloud-connected projects.
βUnlocking MicroPython on ESP32 is not just about programming β itβs about empowering IoT innovation with simplicity and scalability.β
β YaranaIoT Guru
π Contact YaranaIoT Guru Empowering IoT Innovation | ESP32 | Home Automation | Smart Solutions | 50K+ Community
Weβd love to hear from you! Whether itβs IoT project queries, collaborations, tech support, custom PCB design, bulk orders, corporate training, college workshops, or freelance development β weβre just one message away.
βοΈ Email (Official)
For detailed inquiries, project support, business collaboration, sponsorships, or documentation: π© contact@yaranaiotguru.in π§ Alternate: support@yaranaiotguru.in β³ Response: Within 24 hours (MonβSat) π‘ Best for attachments (code, schematics, logs, etc.)
π± Phone / WhatsApp (24Γ7 Support)
Instant live help, troubleshooting, project consultation, or order updates: π +91 70527 22734 π¬ WhatsApp: Chat Now β° Call Hours: MonβSat, 10 AM β 7 PM IST π Emergency? WhatsApp anytime β reply within 1 hour
π Official Website
Tutorials, code, PDFs, schematics, blogs, free tools & online store: π www.yaranaiotguru.in π Shop: yaranaiotguru.in/shop (ESP32 DevKits, Sensors, Relays, Custom PCBs, Project Kits)
βΆοΈ YouTube Channel
Step-by-step IoT builds, live coding, ESP32, Blynk, Node-RED, MQTT, Home Assistant & more: π Yarana IoT Guru πΊ 1,200+ Videos | 52K+ Subs | 5.5M+ Views | 4.8β Rating π₯ New Video Every Week β π Subscribe & Turn On Notifications
π GitHub (100% Open Source)
All codes, Arduino sketches, PlatformIO projects, Node-RED flows, MQTT configs & docs: π github.com/YaranaIotGuru β 50+ Repos | 10K+ Stars & Forks
π₯ Top Projects:
- ESP32 WebSocket Real-Time Dashboard
- Smart Home with Blynk & Alexa
- IoT Irrigation System with Soil Moisture
- MQTT + Node-RED + MySQL Logging
- OLED Weather Station with API
πΈ Instagram
Daily reels, quick tips, live builds, student showcases & giveaways: π @YaranaIoTGuru π± 10K+ Followers | Reels | Stories | Live Sessions
πΌ LinkedIn (Professional Network)
B2B, IoT consulting, training, hiring & partnerships: π Yarana IoT Guru
π€ Services Offered:
- Custom IoT Product Development
- Embedded Systems Training
- College Workshops & FDPs
- PCB Design & Prototyping
π¦ Twitter / X
Real-time updates, polls, project launches & community Q&A: π @YaranaIoTGuru π’ Follow for instant alerts
π Hackster.io (Project Showcases)
In-depth write-ups, circuits, BOM, code & ratings: π hackster.io/yaranaiotguru π 50+ Projects | 100K+ Views | Top 5% Creator
π Instructables (DIY Guides)
Beginner-friendly step-by-step guides with templates & troubleshooting: π instructables.com/member/YaranaIoTGuru
π Featured Guides:
- Automatic Plant Watering System
- Voice-Controlled Home Appliances
- WiFi-enabled Temperature Monitor
π Medium Blog
Deep-dive articles, trends, tutorials & career tips in embedded systems: π medium.com/@yaranaiotguru βοΈ 50+ Articles | 15K+ Readers
π Online Store & Kits
Ready-to-use IoT kits, custom PCBs, sensors & merch: π yaranaiotguru.in/shop π¦ Free Shipping in India above βΉ999 π³ Payment: UPI, Card, Wallet, COD
π Community Platforms
| Platform | Link | Purpose |
|---|---|---|
| Telegram Channel | t.me/YaranaIoTGuru | Project files, PDFs, updates |
| Telegram Group | t.me/YaranaIoTCommunity | Peer support, doubts |
| Discord Server | discord.gg/yarana-iot | Live voice help, coding rooms |
| WhatsApp Community | Join Here | Announcements & polls |
π’ Office & Studio Address
Yarana Studio & Software (Yarana IoT Guru HQ) π Near Rookh Baba Mandir, Umariya Badal Urf Gainda, Allahabad (Prayagraj), Uttar Pradesh β 212507, India β Google Rating: 5.0 β (100+ Reviews)
π Opening Hours: MonβSat: 10:00 AM β 5:00 PM Sunday: Closed
π Associated Website: yaranawebtech.in πΊοΈ View on Google Maps: Search “Yarana Studio & Software” π Walk-ins welcome | Appointment recommended for project discussions