How to clear the EEPROM with Arduino?

ArduinoSoftware & CodingHardware

Arduino Uno has 1 kB of EEPROM storage. EEPROM is a type of non-volatile memory, i.e., its contents are preserved even after power-down. Therefore, it can be used to store data that you want to be unchanged across power cycles. Configurations or settings are examples of such data.

In this article, we will see how to clear the EEPROM, i.e., set all its bytes to 0. We will be walking through an inbuilt example in Arduino. The EEPROM examples can be accessed from − File → Examples → EEPROM.

Example

We will look at the eeprom_clear example. It is very easy. You essentially use the EEPROM.write() function, and iterate over the length of the EEPROM, and write 0 at each address.

We begin with the inclusion of the library.

#include <EEPROM.h>

Within the Setup, you set pin 13 as output, and connect an LED to it. Then you make the LED glow once we are done. This is optional though. The code snippet of interest is the for loop wherein we iterate over each address in the EEPROM, till we reach the end of the EEPROM, and write 0 to each address.

void setup() {
   // initialize the LED pin as an output.
   pinMode(13, OUTPUT);

   for (int i = 0 ; i < EEPROM.length() ; i++) {
      EEPROM.write(i, 0);
   }

   // turn the LED on when we're done
   digitalWrite(13, HIGH);
}

Nothing happens in the loop.

void loop() {
   /** Empty loop. **/
}
raja
Published on 26-Jul-2021 09:42:56
Advertisements