How to store Data using ATMEGA328 EEPROM

So far everything you do on an Arduino is lost as soon as power is disconnected. The question is how do you go about storing information so that it isn’t lost after a reset. The answer is EEPROM.

EEPROM is a form of solid-state non-volatile memory which is commonly used in systems to store small amounts of data for microcontrollers. It’s like a quick tiny hard drive embedded on the chip. Most EEPROM units use low-level communication protocols such as I2C which makes them easy to integrate into projects. The typical size of an EEPROM chip is between 512 bytes and 4 kilobytes. So it only allows a small amount of data to be stored at a time. Storing information in the EEPROM is simple where you specify the address you wish to access and perform the desired function via reading, writing or erasing Some chips may come with their own built-in EEPROM such as the ATMEGA328 whereas others do not and require an external EPROM chip. However, you can always add more EEPROM to your projects using the appropriate communication protocol. The Arduino UNO has 1,024 bytes or 1 kilobyte of onboard EEPROM to use and can be easily accessed using the Arduino EEPROM libraries.

Something to keep in mind when using EEPROM is that it has a rated life expectancy because of the way the EEPROM units function. You can only write to each address a certain amount of times before it will start to fail and become unreliable.

If you want to store data using the ATMEGA328 EEPROM on the Arduino we’ll be using a standard push button to increment a counter and then store that in EEPROM. We’re going to be using a library which is a set of code that we can import into our program and it gives us a whole bunch of extra functionality and built-in functions that can be used to supplement and perform operations or functions that will be otherwise long and tedious. In the code below, we allow data to be retained when power is disconnected and accessed later.

Source Code for ‘EEPROM Counter’


// pin definitions

int ledPin = 13;

int buttonPin = 2;

// global variables

int lastButtonState = 1;

long unsigned int lastPress;

int debounceTime = 20;

int counter;

void setup() {

  // setup pin modes

  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin, INPUT_PULLUP);

  //initialise Serial port


  //assign counter the value of address 0

  counter =;

  //write a 0 to address 0. This allows for consecutive resets to reset the counter



void loop() { 

  int buttonState = digitalRead(buttonPin);   //read the state of buttonPin and store it as buttonState (0 or 1)

  if((millis() - lastPress) > debounceTime)   //if the time between the last buttonChange is greater than the debounceTime


    lastPress = millis();   //update lastPress                                                    

    if(buttonState == 0 && lastButtonState == 1)    //if button is pressed and was released last change



      EEPROM.write(0, counter); //write counter to address 0

      digitalWrite(ledPin, HIGH); //momentary LED

      lastButtonState = 0;    //record the lastButtonState

      //print the results

      Serial.print("Counter: ");



    if(buttonState == 1 && lastButtonState == 0)    //if button is not pressed, and was pressed last change


      lastButtonState = 1;    //record the lastButtonState

      digitalWrite(ledPin, LOW);  //momentary LED


It’s really easy to use the EEPROM. If you have 1024 possible addresses then you’re going to need to nominate those between 0 and 1023 or 10-bit address. We can just see that EEPROM write is really simple working through the address and we’re giving the value to write much the same as digital write or analog write.

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.