How to distinguish reset by power on from reset by software command ?

Hey.
He intends to assemble a simple system that triggers a LED when there is a lack of electricity. It's just for learning. I'm using an ESP and this library that monitors and points out the reasons for a boot/reset.

I am only interested in two reasons, which is why I commented on the others.

#include <Arduino.h>
#include <esp_task_wdt.h>

#define WDT_TIMEOUT 3          // define a 3 seconds WDT (Watch Dog Timer)
int resetreason;
 
void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.println("");
 
    resetreason = esp_reset_reason();
 
    Serial.print("Reset Reason was: "); Serial.println(resetreason);
    
    esp_reset_reason_t reason = esp_reset_reason();
 
    switch (reason) {
      
        case ESP_RST_POWERON:
          Serial.println("Power ON");
        break;

        case ESP_RST_SW:
          Serial.println("Reset ESPrestart()");
        break;

//case ESP_RST_PANIC:Serial.println("Reset por exception/panic");break;
//case ESP_RST_UNKNOWN:Serial.println("Reset UNKNOW");break;
//case ESP_RST_EXT:Serial.println("Reset by external pin (not applicable for ESP32)");break;
//case ESP_RST_INT_WDT:Serial.println("Reset (software or hardware) por interrupção WATCHDOG");break;
//case ESP_RST_TASK_WDT:Serial.println("Reset WATCHDOG");break;
//case ESP_RST_WDT:Serial.println("Reset others WATCHDOG´s");break;                                
//case ESP_RST_DEEPSLEEP:Serial.println("Reset DEEP SLEEP MODE");break;
//case ESP_RST_BROWNOUT:Serial.println("Brownout reset (software or hardware)");break;
//case ESP_RST_SDIO:Serial.println("Reset over SDIO");break;
        
        default:
        break;
    }
}
 
void loop() {
  esp_task_wdt_reset();    
}

However, I notice that it doesn't matter if I energize the ESP or if I reset it using the button on the board itself, the reason doesn't change, it's always 1. When it happens through ESP.restart(); the reason is 3. Even when tested with all uncommented reset reason options.

But there is no difference between powering up the system, say turning on the power supply and pressing the reset button on the board of the ESP32 itself.

Is there something to be fixed? Is that so ? or does it only work with ESP-IDF?

Anybody know ?

Thanks.

It shouldn't make a difference in theory, because you a resetting the whole processor again either way....The reset button is just a more convenient to use then pulling the power.

There are many processors that can distinguish between cold start and hot start. I will check if ESP32 has this feature.

@GolamMostafa will be able to answer your question....Knows quite a bit more about ESP kind of things than I do. :slightly_smiling_face:

From ChatGPT
The ESP32 microcontroller itself does not have a built-in capability to directly distinguish between a cold start and a hot start in the traditional sense. However, you can implement mechanisms in your software to infer whether the ESP32 has just powered on (cold start) or has been reset without a power cycle (hot start). Here is an approach to achieve this:

Check for Power-On Reset (POR):
You can check the ESP32's reset reason register (RTC_CNTL_STORE0) to determine the reset reason. The Power-On Reset (POR) flag indicates that the device has just been powered on, indicating a cold start. Try the following sketch:

void setup()
{
  Serial.begin(9600);
  Serial.println();
  esp_reset_reason_t reset_reason = esp_reset_reason();
  Serial.println(reset_reason);
  if (reset_reason == ESP_RST_POWERON)
  {
    Serial.println("This is a cold start.");
  }
  else
  {
    Serial.println("This is a hot start (other reset reason).");
  }
}

void loop(){}

Thanks for helping with code. I tested it, my current test board is an ESP32cam. The result is always 1. Either energizing or executing the RST button:

RST

I believe that the POR flag becomes LOW when the ESP32 is powered up from cold state. However, we have difficulties to read this value.

I tried with the following setup; where, the NANO-Master reads the POR flag from ESP32-Slave; but, the value is still read as HIGH.

Actually, when two participants here in this forum said, there is no reason for a reset using the RST button to occur in practice.

I decided to use the example from post#1.

But there are situations in which the electrical network suffers noise and these can only cause the reset of a microcontroller. It doesn't have to be a total grid shutdown.

Good week everyone, especially my colleague GolamMostafa who posted code.

2 Likes

if you require to know what the reset reason was have a look at ESP32 esp_reset_reason_t

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.