Erasing Flash Memory on ESP32 (Factory Reset) Using ESPTool

Factory resets are the nuclear solution for many ESP32 issues. Whether you want to delete changes made to the firmware or configuration settings, the system is crashing constantly and you can’t upload new code, or you simply want to clear data that is no longer needed, a reset is the most convenient option.

In this tutorial, we’ll guide you through the step-by-step process of erasing your ESP32’s flash memory using ESPTool.

What is ESPTool?

ESPToool is a versatile utility designed for working with Espressif Systems chips, such as the ESP32 and ESP8266. It’s a crucial tool for anyone developing or troubleshooting projects with these chips.

ESPToool is typically used via your computer’s command line or terminal. You connect your ESP32 to your computer via USB, and then use ESPTool commands to communicate with it. The tool is written in Python, making it cross-platform and accessible on Windows, macOS, and Linux.

Here’s a quick overview of what ESPTool can do:

  • Flashing Firmware: ESPToool can upload (or “flash”) compiled code onto your ESP32’s flash memory, allowing you to run your custom programs.
  • Erasing Flash: It can completely erase the flash memory, essentially resetting the ESP32 to its factory state (as we’re focusing on in this tutorial).
  • Reading Flash: You can use it to read back the contents of the flash memory, which is helpful for debugging or backup purposes.
  • Interacting with the Bootloader: ESPTool communicates with the ESP32’s bootloader, which is a small program that runs on startup and allows you to upload new code or perform other actions.

Additional features include:

  • Monitoring Serial Output: It can monitor the serial output from your ESP32, making it useful for viewing debug messages or data from your running programs.
  • Chip Configuration: ESPToool provides commands to read and modify various chip settings and configurations.
  • Security Features: You can use it for tasks like reading and writing secure boot keys.

Okay, let’s begin with the steps for erasing the flash memory of your ESP32.

Step 1: Install Python

ESPTool is a Python-based tool, so you’ll need Python installed on your system. Make sure you download Python version 3.7 or newer. If you’re using Windows, you can find the MSI installer on the official Python website.

download python 3

Launch the installer and proceed through the installation wizard. Just make sure the “Add python.exe to Path” option is enabled.

Step 2: Install ESPTool

After you’ve installed Python, proceed to install the latest stable release of ESPTool using pip, Python’s package manager. Open a terminal window and type the command:

pip install esptool

In some cases, this command might not work due to specific Python configurations. If you encounter an error, try these alternative commands instead:

pip3 install esptool
python -m pip install esptool
pip2 install esptool

Additionally, ESPTool relies on the “setuptools” package, which might not be included in all systems by default. If it’s missing, install it using the following command:

pip install setuptools
esptool installation through pip

Once you’ve successfully installed ESPTool, it should be located in your Python executables directory. You can verify this by opening your terminal and running:

python -m esptool

If installed correctly, you should see a message confirming the version and basic usage information of ESPTool.

esptool installation verification

Step 3: Connect Your ESP32

Now connect your ESP32 board to your computer using a USB cable.

esp32 connected to computer using a usb cable

Step 4: Identify the COM Port

To identify the COM port your ESP32 is connected to, follow the instructions for your operating system:

Windows Users: Open the Device Manager and expand the “Ports (COM & LPT)” section. Look for a device labeled “Silicon Labs CP210x USB to UART Bridge” or something similar. The associated COM port number (e.g., COM3) is what you’ll need.

find esp32 in device manager

macOS/Linux Users: Open your terminal and type the command ls /dev/tty.*. This will list all connected serial devices. Your ESP32 should appear as /dev/tty.SLAB_USBtoUART or a similar name.

Step 5: Erase the Flash Memory

Now, open a terminal window on your computer and hold down the BOOT button on your ESP32. While still holding the BOOT button, copy and paste the following command into your terminal, replacing COMx with your ESP32’s actual COM port:

python -m esptool --port COMx erase_flash

If you prefer, you can also use this alternative command, which specifies the ESP32 chip type:

python -m esptool --chip esp32 erase_flash

After pasting either of these commands into your terminal, press Enter.

Once you see the “Erasing” process start in your terminal, you can release the BOOT button. After a few seconds, the ESP32 flash memory will be completely erased.

esp32 erase flash esptool

If, after the “Connecting…” message, you notice a continuous stream of dots, it means your ESP32 hasn’t entered flashing mode. In that case, repeat the process, making sure to hold the BOOT button while executing the command to ensure your ESP32 enters flashing mode and completes the erasing process successfully.

Step 6: Verification and Troubleshooting

After the flash memory has been erased, you can confirm that the ESP32 is working correctly by re-uploading a simple sketch, such as the Blink program.

ESP32 Development Board Blink Sketch Working Arduino IDE

If you run into issues, here are a few troubleshooting tips:

  • Driver issues: Ensure the necessary USB-to-Serial drivers for your ESP32 are installed correctly.
  • Correct port selection: Double-check that you’ve selected the correct COM port.
  • Retry the erase process: If the first attempt fails, retry the erase command.
  • Permission error: Try running the command prompt or terminal as an administrator (or use sudo on Linux). This should resolve any permission-related issues.