A guide to EEPROM

Learn how to use EEPROM, short for electrically erasable programmable read-only memory, on Arduino boards.

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

Functions in the EEPROM class is automatically included with the platform for your board, meaning you do not need to install any external libraries.

Hardware Required

All of the following boards have an EEPROM:

EEPROM Clear

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

1/*
2 * EEPROM Clear
3 *
4 * Sets all of the bytes of the EEPROM to 0.
5 * Please see eeprom_iteration for a more in depth
6 * look at how to traverse the EEPROM.
7 *
8 * This example code is in the public domain.
9 */
10
11#include <EEPROM.h>
12
13void setup() {
14 // initialize the LED pin as an output.
15 pinMode(13, OUTPUT);
16
17 /***
18 Iterate through each byte of the EEPROM storage.
19 Larger AVR processors have larger EEPROM sizes, E.g:
20 - Arduino Duemilanove: 512 B EEPROM storage.
21 - Arduino Uno: 1 kB EEPROM storage.
22 - Arduino Mega: 4 kB EEPROM storage.
23 Rather than hard-coding the length, you should use the pre-provided length function.
24 This will make your code portable to all AVR processors.
25 ***/
26
27 for (int i = 0 ; i < EEPROM.length() ; i++) {
28 EEPROM.write(i, 0);
29 }
30
31 // turn the LED on when we're done
32 digitalWrite(13, HIGH);
33}
34
35void loop() {
36 /** Empty loop. **/
37}

EEPROM CRC

A CRC is a simple way of checking whether data has changed or become corrupted. This example calculates a CRC value directly on the EEPROM values. This CRC is like a signature and any change in the calculated CRC means a change in the stored data. The purpose of this example is to highlight how the EEPROM object can be used just like an array.

1/***
2
3 Written by Christopher Andrews.
4
5 CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
6
7 A CRC is a simple way of checking whether data has changed or become corrupted.
8
9 This example calculates a CRC value directly on the EEPROM values.
10
11 The purpose of this example is to highlight how the EEPROM object can be used just like an array.
12
13***/
14
15#include <Arduino.h>
16#include <EEPROM.h>
17
18void setup() {
19
20 //Start serial
21
22 Serial.begin(9600);
23
24 while (!Serial) {
25
26 ; // wait for serial port to connect. Needed for native USB port only
27
28 }
29
30 //Print length of data to run CRC on.
31
32 Serial.print("EEPROM length: ");
33
34 Serial.println(EEPROM.length());
35
36 //Print the result of calling eeprom_crc()
37
38 Serial.print("CRC32 of EEPROM data: 0x");
39
40 Serial.println(eeprom_crc(), HEX);
41
42 Serial.print("\n\nDone!");
43}
44
45void loop() {
46
47 /* Empty loop */
48}
49
50unsigned long eeprom_crc(void) {
51
52 const unsigned long crc_table[16] = {
53
54 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
55
56 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
57
58 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
59
60 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
61
62 };
63
64 unsigned long crc = ~0L;
65
66 for (int index = 0 ; index < EEPROM.length() ; ++index) {
67
68 crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
69
70 crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
71
72 crc = ~crc;
73
74 }
75
76 return crc;
77}

EEPROM Get

The purpose of this example is to show how the put and get methods provide a different behaviour than write and read, that work on single bytes. Getting different variables from EEPROM retrieve a number of bytes that is related to the variable datatype.

1/***
2
3 eeprom_get example.
4
5 This shows how to use the EEPROM.get() method.
6
7 To pre-set the EEPROM data, run the example sketch eeprom_put.
8
9 This sketch will run without it, however, the values shown
10
11 will be shown from what ever is already on the EEPROM.
12
13 This may cause the serial object to print out a large string
14
15 of garbage if there is no null character inside one of the strings
16
17 loaded.
18
19 Written by Christopher Andrews 2015
20
21 Released under MIT licence.
22
23***/
24
25#include <EEPROM.h>
26
27void setup() {
28
29 float f = 0.00f; //Variable to store data read from EEPROM.
30
31 int eeAddress = 0; //EEPROM address to start reading from
32
33 Serial.begin(9600);
34
35 while (!Serial) {
36
37 ; // wait for serial port to connect. Needed for native USB port only
38
39 }
40
41 Serial.print("Read float from EEPROM: ");
42
43 //Get the float data from the EEPROM at position 'eeAddress'
44
45 EEPROM.get(eeAddress, f);
46
47 Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
48
49 /***
50
51 As get also returns a reference to 'f', you can use it inline.
52
53 E.g: Serial.print( EEPROM.get( eeAddress, f ) );
54
55 ***/
56
57 /***
58
59 Get can be used with custom structures too.
60
61 I have separated this into an extra function.
62
63 ***/
64
65 secondTest(); //Run the next test.
66}
67
68struct MyObject {
69
70 float field1;
71
72 byte field2;
73
74 char name[10];
75};
76
77void secondTest() {
78
79 int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
80
81 MyObject customVar; //Variable to store custom object read from EEPROM.
82
83 EEPROM.get(eeAddress, customVar);
84
85 Serial.println("Read custom object from EEPROM: ");
86
87 Serial.println(customVar.field1);
88
89 Serial.println(customVar.field2);
90
91 Serial.println(customVar.name);
92}
93
94void loop() {
95
96 /* Empty loop */
97}

EEPROM Iteration

The purpose of this example is to show how to go through the whole EEPROM memory space with different approaches. The code provided doesn't run on its own but should be used as a surce of code snippets to be used elsewhere.

1/***
2
3 eeprom_iteration example.
4
5 A set of example snippets highlighting the
6
7 simplest methods for traversing the EEPROM.
8
9 Running this sketch is not necessary, this is
10
11 simply highlighting certain programming methods.
12
13 Written by Christopher Andrews 2015
14
15 Released under MIT licence.
16
17***/
18
19#include <EEPROM.h>
20
21void setup() {
22
23 /***
24
25 Iterate the EEPROM using a for loop.
26
27 ***/
28
29 for (int index = 0 ; index < EEPROM.length() ; index++) {
30
31 //Add one to each cell in the EEPROM
32
33 EEPROM[ index ] += 1;
34
35 }
36
37 /***
38
39 Iterate the EEPROM using a while loop.
40
41 ***/
42
43 int index = 0;
44
45 while (index < EEPROM.length()) {
46
47 //Add one to each cell in the EEPROM
48
49 EEPROM[ index ] += 1;
50
51 index++;
52
53 }
54
55 /***
56
57 Iterate the EEPROM using a do-while loop.
58
59 ***/
60
61 int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
62
63 do {
64
65 //Add one to each cell in the EEPROM
66
67 EEPROM[ idx ] += 1;
68
69 idx++;
70
71 } while (idx < EEPROM.length());
72
73} //End of setup function.
74
75void loop() {}

EEPROM Put

The purpose of this example is to show the

EEPROM.put()
method that writes data on EEPROM using also the EEPROM.update() that writes data only if it is different from the previous content of the locations to be written. The number of bytes written is related to the datatype or custom structure of the variable to be written.

1/***
2
3 eeprom_put example.
4
5 This shows how to use the EEPROM.put() method.
6
7 Also, this sketch will pre-set the EEPROM data for the
8
9 example sketch eeprom_get.
10
11 Note, unlike the single byte version EEPROM.write(),
12
13 the put method will use update semantics. As in a byte
14
15 will only be written to the EEPROM if the data is actually
16
17 different.
18
19 Written by Christopher Andrews 2015
20
21 Released under MIT licence.
22
23***/
24
25#include <EEPROM.h>
26
27struct MyObject {
28
29 float field1;
30
31 byte field2;
32
33 char name[10];
34};
35
36void setup() {
37
38 Serial.begin(9600);
39
40 while (!Serial) {
41
42 ; // wait for serial port to connect. Needed for native USB port only
43
44 }
45
46 float f = 123.456f; //Variable to store in EEPROM.
47
48 int eeAddress = 0; //Location we want the data to be put.
49
50 //One simple call, with the address first and the object second.
51
52 EEPROM.put(eeAddress, f);
53
54 Serial.println("Written float data type!");
55
56 /** Put is designed for use with custom structures also. **/
57
58 //Data to store.
59
60 MyObject customVar = {
61
62 3.14f,
63
64 65,
65
66 "Working!"
67
68 };
69
70 eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
71
72 EEPROM.put(eeAddress, customVar);
73
74 Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
75}
76
77void loop() {
78
79 /* Empty loop */
80}

EEPROM Read

This example illustrates how to read the value of each byte EEPROM using the

EEPROM.read()
function, and how to print those values to the serial window of the Arduino Software (IDE).

1/*
2 * EEPROM Read
3 *
4 * Reads the value of each byte of the EEPROM and prints it
5 * to the computer.
6 * This example code is in the public domain.
7 */
8
9#include <EEPROM.h>
10
11// start reading from the first byte (address 0) of the EEPROM
12int address = 0;
13byte value;
14
15void setup() {
16 // initialize serial and wait for port to open:
17 Serial.begin(9600);
18 while (!Serial) {
19 ; // wait for serial port to connect. Needed for native USB port only
20 }
21}
22
23void loop() {
24 // read a byte from the current address of the EEPROM
25 value = EEPROM.read(address);
26
27 Serial.print(address);
28 Serial.print("\t");
29 Serial.print(value, DEC);
30 Serial.println();
31
32 /***
33 Advance to the next address, when at the end restart at the beginning.
34
35 Larger AVR processors have larger EEPROM sizes, E.g:
36 - Arduino Duemilanove: 512 B EEPROM storage.
37 - Arduino Uno: 1 kB EEPROM storage.
38 - Arduino Mega: 4 kB EEPROM storage.
39
40 Rather than hard-coding the length, you should use the pre-provided length function.
41 This will make your code portable to all AVR processors.
42 ***/
43 address = address + 1;
44 if (address == EEPROM.length()) {
45 address = 0;
46 }
47
48 /***
49 As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
50 EEPROM address is also doable by a bitwise and of the length - 1.
51
52 ++address &= EEPROM.length() - 1;
53 ***/
54
55 delay(500);
56}

EEPROM Update

The purpose of this example is to show the

EEPROM.update()
method that writes data only if it is different from the previous content of the locations to be written. This solution may save execution time because every write operation takes 3.3 ms; the EEPROM has also a limit of 100,000 write cycles per single location, therefore avoiding rewriting the same value in any location will increase the EEPROM overall life.

1/***
2 EEPROM Update method
3
4 Stores values read from analog input 0 into the EEPROM.
5 These values will stay in the EEPROM when the board is
6 turned off and may be retrieved later by another sketch.
7
8 If a value has not changed in the EEPROM, it is not overwritten
9 which would reduce the life span of the EEPROM unnecessarily.
10
11 Released using MIT licence.
12 ***/
13
14#include <EEPROM.h>
15
16/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
17int address = 0;
18
19void setup() {
20 /** Empty setup **/
21}
22
23void loop() {
24 /***
25 need to divide by 4 because analog inputs range from
26 0 to 1023 and each byte of the EEPROM can only hold a
27 value from 0 to 255.
28 ***/
29 int val = analogRead(0) / 4;
30
31 /***
32 Update the particular EEPROM cell.
33 these values will remain there when the board is
34 turned off.
35 ***/
36 EEPROM.update(address, val);
37
38 /***
39 The function EEPROM.update(address, val) is equivalent to the following:
40
41 if( EEPROM.read(address) != val ){
42 EEPROM.write(address, val);
43 }
44 ***/
45
46
47 /***
48 Advance to the next address, when at the end restart at the beginning.
49
50 Larger AVR processors have larger EEPROM sizes, E.g:
51 - Arduino Duemilanove: 512 B EEPROM storage.
52 - Arduino Uno: 1 kB EEPROM storage.
53 - Arduino Mega: 4 kB EEPROM storage.
54
55 Rather than hard-coding the length, you should use the pre-provided length function.
56 This will make your code portable to all AVR processors.
57 ***/
58 address = address + 1;
59 if (address == EEPROM.length()) {
60 address = 0;
61 }
62
63 /***
64 As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
65 EEPROM address is also doable by a bitwise and of the length - 1.
66
67 ++address &= EEPROM.length() - 1;
68 ***/
69
70 delay(100);
71}

EEPROM Write

This example illustrates how to store values read from analog input 0 into the EEPROM using the

EEPROM.write()
function. These values will stay in the EEPROM when the board is turned off and may be retrieved later by another sketch.

1/*
2
3 * EEPROM Write
4
5 *
6
7 * Stores values read from analog input 0 into the EEPROM.
8
9 * These values will stay in the EEPROM when the board is
10
11 * turned off and may be retrieved later by another sketch.
12
13 */
14
15#include <EEPROM.h>
16
17/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
18int addr = 0;
19
20void setup() {
21
22 /** Empty setup. **/
23}
24
25void loop() {
26
27 /***
28
29 Need to divide by 4 because analog inputs range from
30
31 0 to 1023 and each byte of the EEPROM can only hold a
32
33 value from 0 to 255.
34
35 ***/
36
37 int val = analogRead(0) / 4;
38
39 /***
40
41 Write the value to the appropriate byte of the EEPROM.
42
43 these values will remain there when the board is
44
45 turned off.
46
47 ***/
48
49 EEPROM.write(addr, val);
50
51 /***
52
53 Advance to the next address, when at the end restart at the beginning.
54
55 Larger AVR processors have larger EEPROM sizes, E.g:
56
57 - Arduno Duemilanove: 512b EEPROM storage.
58
59 - Arduino Uno: 1kb EEPROM storage.
60
61 - Arduino Mega: 4kb EEPROM storage.
62
63 Rather than hard-coding the length, you should use the pre-provided length function.
64
65 This will make your code portable to all AVR processors.
66
67 ***/
68
69 addr = addr + 1;
70
71 if (addr == EEPROM.length()) {
72
73 addr = 0;
74
75 }
76
77 /***
78
79 As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
80
81 EEPROM address is also doable by a bitwise and of the length - 1.
82
83 ++addr &= EEPROM.length() - 1;
84
85 ***/
86
87 delay(100);
88}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.