Arduino
Arduino
whose values are kept when the board is turned off (like a tiny hard drive). This library
enables you to read and write those bytes.
The supported micro-controllers on the various Arduino and Genuino boards have different
amounts of EEPROM: 1024 bytes on the ATmega328P, 512 bytes on the ATmega168 and
ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and
Genuino 101 boards have an emulated EEPROM space of 1024 bytes.
COPY
1#include <EEPROM.h>
Examples
To see a list of examples for the EEPROM library, click the link below:
A Guide to EEPROM
Functions
read()
Description
Reads a byte from the EEPROM. Locations that have never been written to have the value of
255.
Syntax
COPY
1EEPROM.read(address)
Parameters
Returns
Example
COPY
1#include <EEPROM.h>
3int a = 0;
4int value;
6void setup()
7{
8 Serial.begin(9600);
9}
10
11void loop()
12{
13 value = EEPROM.read(a);
14
15 Serial.print(a);
16 Serial.print("\t");
17 Serial.print(value);
18 Serial.println();
19
20 a = a + 1;
21
22 if (a == 512)
23 a = 0;
24
25 delay(500);
26}
write()
Description
COPY
1EEPROM.write(address, value)
Parameters
Returns
none
Note: An EEPROM write takes 3.3 ms to complete. The EEPROM memory has a specified life
of 100,000 write/erase cycles, so you may need to be careful about how often you write to
it.
Example
COPY
1#include <EEPROM.h>
3void setup()
4{
6 EEPROM.write(i, i);
7}
9void loop()
10{
11}
update()
Description
Write a byte to the EEPROM. The value is written only if differs from the one already saved
at the same address.
Syntax
COPY
1EEPROM.update(address, value)
Parameters
Returns
none
Note: An EEPROM write takes 3.3 ms to complete. The EEPROM memory has a specified life
of 100,000 write/erase cycles, so using this function instead of write() can save cycles if the
written data does not change often
Example
COPY
1#include <EEPROM.h>
3void setup()
4{
7 EEPROM.update(i, i);
8 }
12 EEPROM.update(3, 12);
13 }
14}
15
16void loop()
17{
18}
get()
Description
Syntax
COPY
1EEPROM.get(address, data)
Parameters
data: the data to read, can be a primitive type (eg. float) or a custom struct
Returns
Example
COPY
1#include <EEPROM.h>
3struct MyObject{
4 float field1;
5 byte field2;
6 char name[10];
7};
9void setup(){
10
14 Serial.begin( 9600 );
15 while (!Serial) {
17 }
19
21 EEPROM.get( eeAddress, f );
22 Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
23
25 eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
28
30 Serial.println( customVar.field1 );
31 Serial.println( customVar.field2 );
32 Serial.println( customVar.name );
33}
34
put()
Description
Syntax
COPY
1EEPROM.put(address, data)
Parameters
data: the data to write, can be a primitive type (eg. float) or a custom struct
Returns
Note: This function uses EEPROM.update() to perform the write, so does not rewrites the
value if it didn't change.
Example
COPY
1#include <EEPROM.h>
3struct MyObject {
4 float field1;
5 byte field2;
6 char name[10];
7};
9void setup() {
10
11 Serial.begin(9600);
12 while (!Serial) {
13 ; // wait for serial port to connect. Needed for native USB port only
14 }
15
18
19
20 //One simple call, with the address first and the object second.
21 EEPROM.put(eeAddress, f);
22
24
25 /** Put is designed for use with custom structures also. **/
26
27 //Data to store.
28 MyObject customVar = {
29 3.14f,
30 65,
31 "Working!"
32 };
33
34 eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
35
36 EEPROM.put(eeAddress, customVar);
37 Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can
retrieve the values!");
38}
39
EEPROM[]
Description
EEPROM
like an array. EEPROM cells can be read and written directly using this method.
Syntax
COPY
1EEPROM[address]
Parameters
address: the location to read/write from, starting from 0 (int)
Returns
Example
COPY
1#include <EEPROM.h>
3void setup(){
8 val = EEPROM[ 0 ];
11 EEPROM[ 0 ] = val;
12
13 //Compare contents
15 //Do something...
16 }
17}
18
length()
This function returns an unsigned int containing the number of cells in the EEPROM.
Description
unsigned int
containing the number of cells in the EEPROM.
Syntax
COPY
1EEPROM.length()