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-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
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. If your computer doesn’t detect the COM port for the ESP32 board, please refer to the driver installation instructions at the end of this tutorial.
  • 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 ≡

DRIVER INSTALLATION

ESP32 boards from different vendors may use different USB-to-serial chips. Common ones include CH340, CP210x, and FTDI. The board we’re using in this tutorial is the DIYables ESP32, which uses the CP210x chip.

In most cases, your operating system will recognize the board automatically. However, if it doesn't show up in Thonny IDE, you may need to install the driver manually.

1. Download the CP210x Driver

2. Extract the Driver Files

Unzip the downloaded ZIP file to a folder on your computer. There is no automatic installer in the package.

3. Open Device Manager (Windows Only)

  • Press Windows + X and choose Device Manager from the menu.
  • In Device Manager, check under Ports (COM & LPT). A new device labeled something like USB Serial Port should appear when you plug in the board.
ESP32 Device in Device Manager

4. Connect the ESP32 Board

  • Connect your DIYables ESP32 board to your computer using a USB cable.
  • Watch Device Manager to see which COM port is added when the board is connected.

5. Install or Update the Driver

  • Right-click the new device under Ports (COM & LPT) (e.g., “USB Serial Port”) and select Update driver.
Update Driver
  • Choose Browse my computer for drivers.
Browse for Driver

6. Select the Extracted Driver Folder

  • Click Browse, navigate to the folder where you unzipped the driver, and click Next.
  • Follow the prompts to complete the installation.

7. Verify the COM Port

Once installed, your ESP32 board should appear with a proper name under Ports (COM & LPT).

COM Port Appears Correctly

Note: If you still face issues, uninstall the existing driver for that COM port and repeat the installation process.

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!