The task is to track the number of times a microcontroller restarts (due to reset or power failure) and display the count on a serial terminal (e.g., PuTTY, Arduino IDE).
We are going to use EEPROM (Electrically Erasable Programmable Read-Only Memory) to store the restart count because it retains data even after power loss.
Requirements
Below are the solutions to the given task using different microcontrollers:
We are using the ESP32 DevKitC v4 development board and programming it using the Arduino IDE.
ESP32 does not have a real EEPROM chip inside. Instead, part of its Flash memory is used to simulate EEPROM behaviour.
Connect the ESP32 development board to your computer using a USB cable.
#include <EEPROM.h>
#define EEPROM_SIZE 64 // Using 64 bytes of flash (adjust as needed)
#define RESTART_COUNT_ADDR 0
void setup() {
delay(3000);
Serial.begin(115200);
while (!Serial)
; // Wait for serial connection
// Initialize EEPROM emulation with specified size
EEPROM.begin(EEPROM_SIZE);
Serial.println("ESP32 EEPROM Emulation - Restart Counter");
// Read, increment, and store restart count
int restartCount = EEPROM.read(RESTART_COUNT_ADDR);
restartCount++;
EEPROM.write(RESTART_COUNT_ADDR, restartCount);
EEPROM.commit(); // Must commit to save changes
Serial.print("Device restarted ");
Serial.print(restartCount);
Serial.println(" times.");
// Reset option
Serial.println("Do you want to reset the counter. Press Y or N");
while (1) {
if (Serial.available()) {
char input = Serial.read();
if (input == 'Y' || input == 'y') {
Serial.println("Resetting counter...");
EEPROM.write(RESTART_COUNT_ADDR, 0);
EEPROM.commit();
Serial.println("Counter reset. Restart device.");
break;
} else {
Serial.println("Counter not reset");
break;
}
}
}
}
void loop() {
Serial.println("Performing normal operations...");
delay(5000);
}EEPROM_SIZE (macro)EEPROM.begin(EEPROM_SIZE)).RESTART_COUNT_ADDR (macro)setup()EEPROM.commit(), prints the new count, and asks if you want to reset it.loop()EEPROM.begin(EEPROM_SIZE)EEPROM.read(RESTART_COUNT_ADDR)EEPROM.write(RESTART_COUNT_ADDR, value)EEPROM.commit().EEPROM.commit()We are using the Arduino UNO development board and programming it using the Arduino IDE.
Connect the Arduino UNO development board to your computer using a USB cable.
We are going to use EEPROM.h library to implement this task. In Arduino UNO, there is a separate 1KB EEPROM chip available.
#include <EEPROM.h>
#define RESTART_COUNT_ADDR 0 // Address in EEPROM for restart count
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("EEPROM-Based Microcontroller Restart Counter");
// Increment and read the restart counter
int restartCount;
EEPROM.get(RESTART_COUNT_ADDR, restartCount);
restartCount++;
EEPROM.put(RESTART_COUNT_ADDR, restartCount);
Serial.print("Microcontroller restarted ");
Serial.print(restartCount);
Serial.println(" times.");
// Provide option to reset the counter
Serial.println("Do you want to reset the counter. Press Y or N");
while(1)
{
if (Serial.available()) {
char input = Serial.read();
if (input == 'Y' || input == 'y') {
Serial.println("Resetting restart counter...");
EEPROM.put(RESTART_COUNT_ADDR, 0);
Serial.println("Restart counter reset. Please restart the microcontroller.");
break;
}
else{
Serial.println("Counter not resetted");
break;
}
}
}
}
void loop() {
Serial.println("Doing some task.......");
delay(5000);
}RESTART_COUNT_ADDR (macro)setup()loop()EEPROM.get(address, variable)EEPROM.put(address, variable)