EEPROM Clear

The microcontroller on the Arduino and Genuino boards have 512 bytes of EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive).

This example illustrates how to set of all of those bytes to 0, initializing them to hold new information, using the EEPROM.write() function.

Hardware Required

  • Arduino or Genuino Board

Circuit

There is no circuit for this example.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematics

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

/*
 * EEPROM Clear
 *
 * Sets all of the bytes of the EEPROM to 0.
 * Please see eeprom_iteration for a more in depth
 * look at how to traverse the EEPROM.
 *
 * This example code is in the public domain.
 */


#include <EEPROM.h>

void setup() {
  // initialize the LED pin as an output.
  pinMode(13, OUTPUT);
 
  /***
    Iterate through each byte of the EEPROM storage.

    Larger AVR processors have larger EEPROM sizes, E.g:
    - Arduno Duemilanove: 512b EEPROM storage.
    - Arduino Uno:        1kb EEPROM storage.
    - Arduino Mega:       4kb EEPROM storage.

    Rather than hard-coding the length, you should use the pre-provided length function.
    This will make your code portable to all AVR processors.
  ***/


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

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

void loop() {
  /** Empty loop. **/
}

See also

  • EEPROM.write()

  • if()
  • EEPROM library reference
  • EEPROM Write – Stores values read from A0 into EEPROM.
  • EEPROM Crc – Calculates the CRC of EEPROM contents as if it was an array.
  • EEPROM Iteration – Programming examples on how to go through the EEPROM memory locations.
  • EEPROM Put – Put values in EEPROM using variable semantics (differs from EEPROM.write() ).
  • EEPROM Get – Get values from EEPROM and prints as float on serial.
  • EEPROM Update – Stores values read from A0 into EEPROM, writing the value only if different, to increase EEPROM life.


Last revision 2018/05/17 by SM