Tutorial: Your Arduino’s inbuilt EEPROM

This is chapter thirty-one of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here 

[Updated 09/01/2013]

Today we are going to examine the internal EEPROM in our Arduino boards. What is an EEPROM some of you may be saying? An EEPROM is an Electrically Erasable Programmable Read-Only Memory. It is a form of non-volatile memory that can remember things with the power being turned off, or after resetting the Arduino. The beauty of this kind of memory is that we can store data generated within a sketch on a more permanent basis.

Why would you use the internal EEPROM? For situations where data that is unique to a situation needs a more permanent home. For example, storing the unique serial number and manufacturing date of a commercial Arduino-based project – a function of the sketch could display the serial number on an LCD, or the data could be read by uploading a ‘service sketch’. Or you may need to count certain events and not allow the user to reset them – such as an odometer or operation cycle-counter.

What sort of data can be stored? Anything that can be represented as bytes of data. One byte of data is made up of eight bits of data. A bit can be either on (value 1) or off (value 0), and are perfect for representing numbers in binary form. In other words, a binary number can only uses zeros and ones to represent a value. Thus binary is also known as “base-2″, as it can only use two digits.

How can a binary number with only the use of two digits represent a larger number? It uses a lot of ones and zeros. Let’s examine a binary number, say 10101010. As this is a base-2 number, each digit represents 2 to the power of x, from x=0 onwards:

binary2 binary12

See how each digit of the binary number can represent a base-10 number. So the binary number above represents 85 in base-10 – the value 85 is the sum of the base-10 values. Another example – 11111111 in binary equals 255 in base 10.

binary2

Now each digit in that binary number uses one ‘bit’ of memory, and eight bits make a byte. Due to internal limitations of the microcontrollers in our Arduino boards, we can only store 8-bit numbers (one byte) in the EEPROM. This limits the decimal value of the number to fall between zero and 255. It is then up to you to decide how your data can be represented with that number range. Don’t let that put you off – numbers arranged in the correct way can represent almost anything!

There is one limitation to take heed of – the number of times we can read or write to the EEPROM. According to the manufacturer Atmel, the EEPROM is good for 100,000 read/write cycles (see the data sheet). One would suspect this to be a conservative estimate, however you should plan accordingly. *Update* After some experimentation, the life proved to be a lot longer

Now we know our bits and and bytes, how many bytes can be store in our Arduino’s microcontroller? The answer varies depending on the model of microcontroller. For example:

  • Boards with an Atmel ATmega328, such as Arduino Uno, Uno SMD, Lilypad or the Freetronics KitTen/Eleven – 1024 bytes (1 kilobyte)
  • Boards with an Atmel ATmega1280 or 2560, such as the Arduino Mega series – 4096 bytes (4 kilobytes)
  • Boards with an Atmel ATmega168, such as the original Arduino Lilypad, old Nano, Diecimila etc – 512 bytes.

If y0u are unsure have a look at the Arduino hardware index or ask your board supplier.

If you need more EEPROM storage than what is available with your microcontroller, consider using an external I2C EEPROM as described in the Arduino and I2C tutorial part two.

At this point we now understand what sort of data and how much can be stored in our Arduino’s EEPROM. Now it is time to put this into action. As discussed earlier, there is a finite amount of space for our data. In the following examples, we will use a typical Arduino board with the ATmega328 with 1024 bytes of EEPROM storage.

To use the EEPROM, a library is required, so use the following library in your sketches:

The rest is very simple. To store a piece of data, we use the following function:

The parameter a is the position in the EEPROM to store the integer (0~255) of data b. In this example, we have 1024 bytes of memory storage, so the value of a is between 0 and 1023. To retrieve a piece of data is equally as simple, use:

Where z is an integer to store the data from the EEPROM position a. Now to see an example.

This sketch will create random numbers between 0 and 255, store them in the EEPROM, then retrieve and display them on the serial monitor. The variable EEsize is the upper limit of your EEPROM size, so (for example) this would be 1024 for an Arduino Uno, or 4096 for a Mega.

The output from the serial monitor will appear as such:

So there you have it, another useful way to store data with our Arduino systems. Although not the most exciting tutorial, it is certainly a useful.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

33 Responses to “Tutorial: Your Arduino’s inbuilt EEPROM”

  1. Pixel_k says:

    Hi, and first, thank you very much for your tutorials. Thanks to you and a few others I was able to enter Arduino world with confidence.
    In this tutorial you may have added a few sample about how to store other datas than just a byte (like a int or a long with byte shifting), I thing it would be interesting to a lot of people. I for one use EEPROM to store the user preferences of my LCD menu for the arduino, preferences that come in many flavor and data-types. But it’s only an idea.
    Again, thank you very much for your work !

  2. Sergio says:

    Great tutorial as always, it has completely cleared how EEPROM works !!!

    As it has been said, it would be nice to expand this tutorial with just an example on how you could store a long number.

    Thanks for this amazing tutorials, please dont stop creating them

    • John Boxall says:

      Thanks for your feedback Sergio.
      You could do some bitwise arithmetic and chop up a large number into bytes, then reconstruct it. I will work on it soon.
      cheers
      john

      • Sergio says:

        Great, I have come up with a very basic / simple solution for my proyect, I am going to test it and if it works I will post it here. Nevertheless, I would really appreciate a proper way of handling this issue as my solution is more of a hack than a real solution.

        I have a question about the lifecycle, here you said that the datasheet states 100,000 cycles (which I guess reffers to a read or write to a particular address) however in your video it shows 1,230,163 cycles.

        Are this cycles a read AND a write to each address?
        From this, was your micro lifecycle 10 times bigger than datasheet?

        I would like to know how many times I can read OR write to a SAME address before it fails in order to adjust my program writting interval.

        Thank you for your help

      • John Boxall says:

        Hello
        Each cycle in my demonstration was a read/write to each address. The final count was the number of my cycles until one of the addresses was at fault. Therefore this exceeded the data sheet specification. However you need to take into consideration temperature as well, the test was conducted in a room around 22 degrees C. If you are planning something I would still stick with Atmel’s spec in the data sheet. Or in other words, always plan for the worst. :)
        cheers
        john

  3. Sergio says:

    Great, thanks for that explanation.

    I thought each write OR read counted towards the 100,000 limit, but its a cycle of both of them which add a unit.

    I will do my calculations using the 100,000 cycles, but I was quite surprise that you could get 10x more out of the chip, I thought I was not understanding something.

    Thanks a lot.

    • John Boxall says:

      In this case my ATmega328 lasted a lot longer than expected :)

      • Sergio says:

        Hi again, I have got a further question, hope it is ok.

        The expected lifecycle is for a certain address? I mean can I expect a 100.000cycles (read/write) to address 0, then another 100.000cycles to address 1, etc etc…
        or does any cycle affect the whole EEPROM towards the limit?

        Thank you.

      • John Boxall says:

        Hi Sergio
        Looking at the Atmel data sheet this is not specifically mentioned. However considering my experience I would say it is 100,000 read/write cycles for each address.
        cheers
        john

      • Sergio says:

        Great thanks, that is what I was expecting but good to know you believe that too.
        Once again, thanks for taking the time to answer me.

  4. Duncan says:

    I can see occasions when the running sketch would want to save acquired data values to EEPROM, but how about when there are a set of pre-determined values?

    I have a number of values between 0 and 1024 which I need to store (23 values for the first condition, 27 for the second, 25 for the third and 25 for the fourth) and then read and send via IR to a remote device and I’d also want to srore the current software version in EEPROM.

    Is it possible to write values diectly to the EEPROM space during sketch upload – or as a seperate operation from the PC?

    • John Boxall says:

      You could include a function in your sketch that sends the data to the EEPROM for you; or you could create a separate application that just copies the data to the EEPROM. I would use the latter as it puts less stress on the EEPROM.
      john

      • Duncan says:

        It was, I think, the second option that i was hoping to do (if I understand you correctly) but I cannot find out how to achieve it. I found two articles on the internet – one related to “LadyAda” that was totally uninteligible to me and the other which looked promising and then contained the words “which is beyond the scope of this article”…

        It seems to me to be a perfectly reasonable thing to want to do in the case of data that does not change and is called from within the sketch running on the Arduino without the overhead of having all that data contained within the sketch (thereby using up valuable space) and writing it into EEPROM every time the sketch boots – as I think would be the case in the first option you suggested.

      • John Boxall says:

        Mm. You could write a sketch to accept numbers using the serial monitor box, and store them in the EEPROM. Then the sketch would know how to read those values from the EEPROM. No wasted sketch space. We looked at accepting numbers from the serial monitor in chapter eight.

  5. Duncan says:

    Ah…

    So, if I’m following this correctly, there isn’t any way that I can write directly from the PC to the EEPROM? I’d have to upload a sketch to the Arduino – run something else on the PC that will output numbers to the serial port – have the sketch on the Arduno read them from the serial port and write them to EEPROM – then I have to upload another sketch to the Arduino that overwrites the first sketch and does what I want it to with the data that is now in EEPROM?

    That seems a very convoluted and time consuming way of doing things – or am I misunderstanding?

    • John Boxall says:

      No, it’s the limitations of using the Arduino way of doing things. You most probably will want to examine writing to the microcontroller directly, perhaps using AVRstudio, etc. You may want to check out the avrfreaks.net forum for advice.

  6. Duncan says:

    Thanks John, no further forward at this point but clearer in my understanding…

  7. Gursi says:

    Thanks you so much. Can you please tell me how to store more than 1 byte in EEPROm. For example, IR infrared protocol (NEC Protocol) has more than 32 bits, but I want to store it in the EEPROM. So can you please tell me how to do this.
    Please reply as soon as possible.

    Thank you

  8. elben montil says:

    Good Day Sir, Thank for the tutorial.. But I have a problem sir, on how to save the data in the eeprom when I press the keypad to make a password.

  9. Th3badwolf says:

    Hi Mr. Boxall,I’ve tried to post a comm but it got deleted or spammed i guess.
    If you could reach me by email asap I’d appreciate it thanks.

  10. ametist says:

    Hi John ,
    Could you explain us how to store array value in built-in eeprom with keypad and eeprom library please. Instead to write it on lcd, save it in eeprom:

    void loop()
    {
    char key = keypad.getKey();
    if (key != NO_KEY)
    {
    lcd.print(key);
    count++;
    if (count==17)
    {
    lcd.clear();
    count=0;
    }
    }
    }

    Could you show me a concrete example to help me understand because I’m lost in the technical data, i’m a beginner and i understand better by exemple.

    Thanks.

    • John Boxall says:

      Due to time constraints and fairness, I don’t write custom code for people. Please review the tutorials, work through the examples and you should then have the knowledge to take care of it.

  11. Jitu Kumar says:

    hello
    can u make Prepaid Energy Meter
    how can store data in eeprom
    if power if off i want to store the data
    like meter reading in the eeprom
    when i restart the program i want data is start to where the its end

  12. Stewart says:

    The life cycle described here is a little ambiguous. The eeprom can be written/re-written at least 100,000 times. It can be read an almost infinite number of times.

  13. DRAI Francis says:

    Hello John

    I am newbie in arduino word. I want to have a clarification about the memory usage in arduino box: is the flash memory dedicated to the program or part of it can be used to store data?

    thanks and congratulation for your very useful site

    Francis

  14. Leone says:

    Good afternoon John,

    would aid in the programming of a situation.

    any variable has a type int value = 9856; like this number of stores each variable in a type of memory space at address 0, stores the number 6 at address 1 stores the number 5 and so on, someone would have an idea of how it performs programming.

Trackbacks/Pingbacks


Leave a Reply

Receive notifications of new posts by email.

Arduino Tutorials

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: