ESP32 MicroPython Getting Started

This tutorial instructs you on how to get started with the ESP32 using MicroPython. It covers setting up the Thonny IDE, installing MicroPython firmware on the ESP32, and running a simple Python script on the ESP32 board.

Getting Started with ESP32 and MicroPython

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

To get started with MicroPython on your ESP32 board, follow these steps:

  1. Flash MicroPython firmware: Upload the MicroPython firmware to your ESP32.
  2. Install Thonny IDE: Use Thonny as your development environment for MicroPython on ESP32.
  3. Run a "Hello World" program: Test your setup by running a basic "Hello World" program.

Let's explore each step in detail.

Flash MicroPython firmware

  • Connect the ESP32 board to your computer using a USB cable.
  • Open the Device Manager on your Windows system to identify the COM port that your ESP32 is connected to, such as COM12.
  • Open a command prompt with administrator privileges on your computer.
Windows command prompt administrator
  • Install esptool, a utility used to upload MicroPython firmware to your ESP32 board, by running the following command:
pip install esptool
  • Delete the code running on ESP32 by running the following command (Make sure to replace the COM port number in the command with the correct one.):
esptool --port COM12 erase_flash
Command Prompt
C:\WINDOWS\system32>esptool --port COM12 erase_flash esptool.py v4.7.0 Serial port COM12 Connecting.... Detecting chip type... Unsupported detection protocol, switching and trying again... Connecting.... Detecting chip type... ESP32 Chip is ESP32-D0WD-V3 (revision v3.1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 10:06:1c:86:95:e4 Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 1.4s Hard resetting via RTS pin... C:\WINDOWS\system32>
  • Download the latest version of the MicroPython firmware for ESP32 and save it to a convenient location on your computer, such as D:\MicroPython\ESP32_GENERIC-20240602-v1.23.0.bin
  • Enter the following command to upload the downloaded MicroPython firmware to the ESP32 board (be sure to replace the COM port number and firmware file name with the correct ones for the command):
esptool.py --chip esp32 --port COM12 --baud 460800 write_flash -z 0x1000 D:\MicroPython\ESP32_GENERIC-20240602-v1.23.0.bin
Command Prompt
C:\WINDOWS\system32>esptool --chip esp32 --port COM12 --baud 460800 write_flash -z 0x1000 D:\MicroPython\ESP32_GENERIC-20240602-v1.23.0.bin esptool.py v4.7.0 Serial port COM12 Connecting.......... Chip is ESP32-D0WD-V3 (revision v3.1) Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None Crystal is 40MHz MAC: 10:06:1c:86:95:e4 Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Changed. Configuring flash size... Flash will be erased from 0x00001000 to 0x001a8fff... Compressed 1734240 bytes to 1142447... Wrote 1734240 bytes (1142447 compressed) at 0x00001000 in 27.1 seconds (effective 511.3 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin... C:\WINDOWS\system32>

Now, the MicroPython firmware is loaded to your ESP32 board.

Install Thonny IDE on your computer

  • Download and install Thonny IDE on your computer

Running the first program on ESP32

  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
getting started with ESP32 MicroPython
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the below code and paste it to the Thonny IDE's editor.
print("Hello, World!")
  • Save the script to your ESP32 board by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and MicroPython device. Select MicroPython device
    how to upload MicroPython code to ESP32
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Hello, World!
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!