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:

    H, and frst, thank you very much for your tutorals. Thanks to you and a few others I was able to enter Arduno world wth confdence.
    In ths tutoral you may have added a few sample about how to store other datas than just a byte (lke a nt or a long wth byte shftng), I thng t would be nterestng to a lot of people. I for one use EEPROM to store the user preferences of my LCD menu for the arduno, preferences that come n many flavor and data-types. But t’s only an dea.
    Agan, thank you very much for your work !

  2. Sergio says:

    Great tutoral as always, t has completely cleared how EEPROM works !!!

    As t has been sad, t would be nce to expand ths tutoral wth just an example on how you could store a long number.

    Thanks for ths amazng tutorals, please dont stop creatng them

    • John Boxall says:

      Thanks for your feedback Sergo.
      You could do some btwse arthmetc and chop up a large number nto bytes, then reconstruct t. I wll work on t soon.
      cheers
      john

      • Sergio says:

        Great, I have come up wth a very basc / smple soluton for my proyect, I am gong to test t and f t works I wll post t here. Nevertheless, I would really apprecate a proper way of handlng ths ssue as my soluton s more of a hack than a real soluton.

        I have a queston about the lfecycle, here you sad that the datasheet states 100,000 cycles (whch I guess reffers to a read or wrte to a partcular address) however n your vdeo t shows 1,230,163 cycles.

        Are ths cycles a read AND a wrte to each address?
        From ths, was your mcro lfecycle 10 tmes bgger than datasheet?

        I would lke to know how many tmes I can read OR wrte to a SAME address before t fals n order to adjust my program wrttng nterval.

        Thank you for your help

      • John Boxall says:

        Hello
        Each cycle n my demonstraton was a read/wrte to each address. The fnal count was the number of my cycles untl one of the addresses was at fault. Therefore ths exceeded the data sheet specfcaton. However you need to take nto consderaton temperature as well, the test was conducted n a room around 22 degrees C. If you are plannng somethng I would stll stck wth Atmel’s spec n the data sheet. Or n other words, always plan for the worst. :)
        cheers
        john

  3. Sergio says:

    Great, thanks for that explanaton.

    I thought each wrte OR read counted towards the 100,000 lmt, but ts a cycle of both of them whch add a unt.

    I wll do my calculatons usng the 100,000 cycles, but I was qute surprse that you could get 10x more out of the chp, I thought I was not understandng somethng.

    Thanks a lot.

    • John Boxall says:

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

      • Sergio says:

        H agan, I have got a further queston, hope t s ok.

        The expected lfecycle s for a certan address? I mean can I expect a 100.000cycles (read/wrte) to address 0, then another 100.000cycles to address 1, etc etc…
        or does any cycle affect the whole EEPROM towards the lmt?

        Thank you.

      • John Boxall says:

        H Sergo
        Lookng at the Atmel data sheet ths s not specfcally mentoned. However consderng my experence I would say t s 100,000 read/wrte cycles for each address.
        cheers
        john

      • Sergio says:

        Great thanks, that s what I was expectng but good to know you beleve that too.
        Once agan, thanks for takng the tme to answer me.

  4. Duncan says:

    I can see occasons when the runnng sketch would want to save acqured data values to EEPROM, but how about when there are a set of pre-determned values?

    I have a number of values between 0 and 1024 whch I need to store (23 values for the frst condton, 27 for the second, 25 for the thrd and 25 for the fourth) and then read and send va IR to a remote devce and I’d also want to srore the current software verson n EEPROM.

    Is t possble to wrte values dectly to the EEPROM space durng sketch upload – or as a seperate operaton from the PC?

    • John Boxall says:

      You could nclude a functon n your sketch that sends the data to the EEPROM for you; or you could create a separate applcaton that just copes the data to the EEPROM. I would use the latter as t puts less stress on the EEPROM.
      john

      • Duncan says:

        It was, I thnk, the second opton that was hopng to do (f I understand you correctly) but I cannot fnd out how to acheve t. I found two artcles on the nternet – one related to “LadyAda” that was totally unntelgble to me and the other whch looked promsng and then contaned the words “whch s beyond the scope of ths artcle”…

        It seems to me to be a perfectly reasonable thng to want to do n the case of data that does not change and s called from wthn the sketch runnng on the Arduno wthout the overhead of havng all that data contaned wthn the sketch (thereby usng up valuable space) and wrtng t nto EEPROM every tme the sketch boots – as I thnk would be the case n the frst opton you suggested.

      • John Boxall says:

        Mm. You could wrte a sketch to accept numbers usng the seral montor box, and store them n the EEPROM. Then the sketch would know how to read those values from the EEPROM. No wasted sketch space. We looked at acceptng numbers from the seral montor n chapter eght.

  5. Duncan says:

    Ah…

    So, f I’m followng ths correctly, there sn’t any way that I can wrte drectly from the PC to the EEPROM? I’d have to upload a sketch to the Arduno – run somethng else on the PC that wll output numbers to the seral port – have the sketch on the Arduno read them from the seral port and wrte them to EEPROM – then I have to upload another sketch to the Arduno that overwrtes the frst sketch and does what I want t to wth the data that s now n EEPROM?

    That seems a very convoluted and tme consumng way of dong thngs – or am I msunderstandng?

    • John Boxall says:

      No, t’s the lmtatons of usng the Arduno way of dong thngs. You most probably wll want to examne wrtng to the mcrocontroller drectly, perhaps usng AVRstudo, etc. You may want to check out the avrfreaks.net forum for advce.

  6. Duncan says:

    Thanks John, no further forward at ths pont but clearer n my understandng…

  7. Gursi says:

    Thanks you so much. Can you please tell me how to store more than 1 byte n EEPROm. For example, IR nfrared protocol (NEC Protocol) has more than 32 bts, but I want to store t n the EEPROM. So can you please tell me how to do ths.
    Please reply as soon as possble.

    Thank you

  8. elben montil says:

    Good Day Sr, Thank for the tutoral.. But I have a problem sr, on how to save the data n the eeprom when I press the keypad to make a password.

  9. Th3badwolf says:

    H Mr. Boxall,I’ve tred to post a comm but t got deleted or spammed guess.
    If you could reach me by emal asap I’d apprecate t thanks.

  10. ametist says:

    H John ,
    Could you explan us how to store array value n bult-n eeprom wth keypad and eeprom lbrary please. Instead to wrte t on lcd, save t n eeprom:

    vod loop()
    {
    char key = keypad.getKey();
    f (key != NO_KEY)
    {
    lcd.prnt(key);
    count++;
    f (count==17)
    {
    lcd.clear();
    count=0;
    }
    }
    }

    Could you show me a concrete example to help me understand because I’m lost n the techncal data, ‘m a begnner and understand better by exemple.

    Thanks.

    • John Boxall says:

      Due to tme constrants and farness, I don’t wrte custom code for people. Please revew the tutorals, work through the examples and you should then have the knowledge to take care of t.

  11. Jitu Kumar says:

    hello
    can u make Prepad Energy Meter
    how can store data n eeprom
    f power f off want to store the data
    lke meter readng n the eeprom
    when restart the program want data s start to where the ts end

  12. Stewart says:

    The lfe cycle descrbed here s a lttle ambguous. The eeprom can be wrtten/re-wrtten at least 100,000 tmes. It can be read an almost nfnte number of tmes.

  13. DRAI Francis says:

    Hello John

    I am newbe n arduno word. I want to have a clarfcaton about the memory usage n arduno box: s the flash memory dedcated to the program or part of t can be used to store data?

    thanks and congratulaton for your very useful ste

    Francs

  14. Leone says:

    Good afternoon John,

    would ad n the programmng of a stuaton.

    any varable has a type nt value = 9856; lke ths number of stores each varable n 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 dea of how t performs programmng.

Trackbacks/Pingbacks


Leave a Reply

Subscribe via email

Receive notifications of new posts by email.

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: