Setting Up esptool in a Python Virtual Environment
/I’m trying to get into the habit of asking ChatGPT to create a blog post each time I do something I want to remember (and perhaps tell others about). I did this today to get a device ready for my shutter controller….
So, you’ve got an ESP32, and you need to flash some firmware. The official tool for the job is esptool
, but before you go installing things system-wide like a reckless cowboy, let’s do it properly—with a Python virtual environment.
Why? Because keeping things self-contained means no dependency nightmares down the line. Trust me, future-you will thank present-you for this.
Step 1: Install Python (If You Haven’t Already)
First, make sure you have Python installed:
python3 --version
If that spits out a version number, you’re good. If not, go install it from python.org.
On Windows, also make sure pip
is installed:
python -m ensurepip --default-pip
Step 2: Create a Virtual Environment
Now, let’s create a virtual environment so we don’t clutter up our system’s Python installation:
python -m venv esptool-env
This makes a new folder called esptool-env
, which holds its own little self-contained Python world.
Step 3: Activate the Virtual Environment
Before installing anything, you need to activate the virtual environment.
- On Windows (Command Prompt or PowerShell):
esptool-env\Scripts\activate
- On macOS/Linux:
source esptool-env/bin/activate
Your terminal should now show something like (esptool-env)
at the beginning of the prompt, meaning you're inside the virtual environment.
Step 4: Install esptool
Now, install esptool
inside your virtual environment:
pip install esptool
To check if it installed correctly, run:
esptool --version
✅ If you see a version number, you're all set!
Step 5: Using esptool
(No .py
Required!)
At this point, you might be tempted to type:
esptool.py chip_id
But stop right there! On Windows (and even some Linux/macOS systems), esptool
installs as an executable, meaning you don’t need the .py
suffix.
Instead, just run:
esptool chip_id
Much cleaner, right?
Step 6: What is firmware.bin
?
When flashing an ESP32, you need a firmware file—usually named firmware.bin
. This file contains the actual program that will run on the ESP32, including:
✅ The operating system (like MicroPython or an IoT framework)
✅ Libraries and runtime components
✅ The main application code
Think of it as the brain of your ESP32—it tells the chip what to do when it powers on.
If you’re installing MicroPython, you’ll need to download the latest ESP32 firmware from the official site:
🔗 Download MicroPython Firmware
Make sure you get the correct version for your ESP32 model (ESP32, ESP32-C3, ESP32-S3, etc.).
Step 7: Flashing MicroPython to an ESP32
Once you have the firmware.bin
file, you can flash it using esptool
.
1️⃣ Erase the Existing Flash Memory
Before installing new firmware, it’s a good idea to wipe the existing flash:
esptool --port COM3 erase_flash
(Replace COM3
with your actual serial port. On Linux/macOS, it might be something like /dev/ttyUSB0
.)
2️⃣ Flash the MicroPython Firmware
Now, install MicroPython by flashing the .bin
file:
esptool --port COM3 --baud 460800 write_flash -z 0x1000 firmware.bin
🛠 Breaking this down:
--port COM3
→ Specifies the serial port--baud 460800
→ Speeds up flashing (can be lowered if issues arise)write_flash -z 0x1000 firmware.bin
→ Writes the firmware at the correct address (0x1000
)
When it’s done, MicroPython is now installed! 🎉
Step 8: Connecting to MicroPython
Once the firmware is flashed, you can connect to the ESP32 using a serial terminal program:
1️⃣ Install a terminal program like PuTTY, screen, or minicom
2️⃣ Connect using the correct port and baud rate (115200 baud)
3️⃣ Press Enter, and you should see the MicroPython REPL (interactive shell)
Alternatively, use Thonny (a beginner-friendly Python IDE) to interact with MicroPython.
Step 9: Deactivating the Virtual Environment
When you're done, exit the virtual environment with:
deactivate
The next time you need to use esptool
, just reactivate the environment and you’re good to go:
esptool-env\Scripts\activate # Windows
source esptool-env/bin/activate # macOS/Linux
Final Thoughts
By using a virtual environment, you've kept your system clean and organized—no global Python clutter, no version conflicts. And remember:
🔥 Just type esptool
, no .py
needed!
🔥 MicroPython firmware is just a .bin
file that needs to be flashed to your ESP32!
Now, go forth and flash those ESP32s like a pro. 🚀