ESP32 (38) – Factory reset

by luca

In the previous two posts of this tutorial, I explained how to perform an over-the-air update of the firmware running on the esp32 chip.

Sometimes you may need to revert to the factory firmware, that is the firmware stored in the flash memory when the chip was programmed. Many consumer devices have a button or a pin that, if you press it for some seconds, triggers a reset function:


In this post I’ll show you how to add this functionality to your project.


As explained in a previous post, the flash memory connected to the esp32 chip is divided into some partitions, based on a layout configured when you program the chip.

Partitions that can store firmwares are of the app type. The partition that contains the firmware programmed via USB, has the factory subtype.

The esp-idf framework includes a method to search partitions in the flash memory:

#include "esp_partition.h"
esp_partition_iterator_t esp_partition_find(
  esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);

You can specify some filters to narrow down the results (they are not mandatory, use NULL if a filter is not needed): the type of the partition, the subtype and also a specific label.

If you want to look for the partition that contains the factory firmware, you can therefore write:

esp_partition_iterator_t pi = esp_partition_find(

The method returns a partition iterator, that is an object that allows to scroll through the partitions found.

If the search was successful, this object is not NULL and you can get a pointer to the partition with the method:

const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator);

After the use, it’s important to release the iterator object with:

void esp_partition_iterator_release(esp_partition_iterator_t iterator);

After having obtained the correct partition, that contains the factory firmware, you only have to flag it as the boot partition and restart the chip:

if(pi != NULL) {
  const esp_partition_t* factory = esp_partition_get(pi);
  if(esp_ota_set_boot_partition(factory) == ESP_OK) esp_restart();	


In the following video you can see how to perform a factory reset. In the video you can also learn how to “count” the number of seconds a button is pressed to trigger the reset function only after a fixed threshold (3 seconds in my example). Enjoy!

[youtube id=”IQMJSFrRie4″ width=”600″ height=”350″]

Related Posts


Germán Sunday January 13th, 2019 - 12:14 PM

Ciao Luca. I cannot find video link on this post. Anyway I’ve found it on Google. Good job! I didn’t know this function. Thank you

spleen Tuesday January 29th, 2019 - 03:40 PM

it’s possibile to make a tool like this

with esp32?

transform a usb keyboard in a bluetooth one


luca Friday February 1st, 2019 - 02:35 PM

not an easy way because of esp32 doesn’t include an hardware usb host controller (needed to connect to usb keyboard/mouse)

johnnm Wednesday June 12th, 2019 - 01:11 PM

USB host isn’t required for a keyboard or mouse. The vast majority of USB keyboards and mice are backwards compatible with ps/2. Those little USB to ps/2 converters that used to be bundled with usb keyboards and mice are purely mechanical.

USB data + becomes the ps/2 clock and USB data – becomes ps/2 clock. No idea of the voltages, I’d assume about 4.5~5 volts but after that problems been handled I’m almost certain you can interface almost any USB keyboard or mouse to an ESP32 and read the keystrokes / mouse input.

johnnm Wednesday June 12th, 2019 - 05:45 PM

USB data – becomes ps/2 data.***

kjames Wednesday July 10th, 2019 - 09:52 AM

Hello Luca, thank you, this is a very useful tutorial!

I could not find the source code in your repository, maybe I am missing something, or will it be available soon? thanks!

luca Wednesday July 17th, 2019 - 10:01 AM

hi, as the code to perform the reset is only 6-7 lines (described in the article) I didn’t prepare a full example


Leave a Comment

fifteen − eight =