|
| 1 | +--- |
| 2 | +title: EEPROM Library |
| 3 | +description: Documentation for usage of the EEPROM library. EEPROM is a memory whose values are kept when the board is powered off. |
| 4 | +author: 'Arduino' |
| 5 | +tags: [EEPROM] |
| 6 | +--- |
| 7 | + |
| 8 | +The microcontroller on the Arduino and Genuino AVR based board has EEPROM: memory 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. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +To use this library |
| 13 | + |
| 14 | +``` |
| 15 | +#include <EEPROM.h> |
| 16 | +``` |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +To see a list of examples for the EEPROM library, click the link below: |
| 21 | + |
| 22 | +- [A Guide to EEPROM](/learn/programming/eeprom-guide) |
| 23 | + |
| 24 | +## Functions |
| 25 | + |
| 26 | +### `read()` |
| 27 | + |
| 28 | +#### Description |
| 29 | +Reads a byte from the EEPROM. Locations that have never been written to have the value of 255. |
| 30 | + |
| 31 | +#### Syntax |
| 32 | + |
| 33 | +``` |
| 34 | +EEPROM.read(address) |
| 35 | +``` |
| 36 | + |
| 37 | +#### Parameters |
| 38 | +address: the location to read from, starting from 0 (int) |
| 39 | + |
| 40 | +#### Returns |
| 41 | +the value stored in that location (byte) |
| 42 | + |
| 43 | +#### Example |
| 44 | + |
| 45 | +``` |
| 46 | +#include <EEPROM.h> |
| 47 | +
|
| 48 | +int a = 0; |
| 49 | +int value; |
| 50 | +
|
| 51 | +void setup() |
| 52 | +{ |
| 53 | + Serial.begin(9600); |
| 54 | +} |
| 55 | +
|
| 56 | +void loop() |
| 57 | +{ |
| 58 | + value = EEPROM.read(a); |
| 59 | +
|
| 60 | + Serial.print(a); |
| 61 | + Serial.print("\t"); |
| 62 | + Serial.print(value); |
| 63 | + Serial.println(); |
| 64 | +
|
| 65 | + a = a + 1; |
| 66 | +
|
| 67 | + if (a == 512) |
| 68 | + a = 0; |
| 69 | +
|
| 70 | + delay(500); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### `write()` |
| 75 | + |
| 76 | +#### Description |
| 77 | +Write a byte to the EEPROM. |
| 78 | + |
| 79 | +#### Syntax |
| 80 | + |
| 81 | +``` |
| 82 | +EEPROM.write(address, value) |
| 83 | +``` |
| 84 | + |
| 85 | +#### Parameters |
| 86 | +address: the location to write to, starting from 0 (int) |
| 87 | + |
| 88 | +value: the value to write, from 0 to 255 (byte) |
| 89 | + |
| 90 | +#### Returns |
| 91 | +none |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +#### Example |
| 96 | + |
| 97 | +``` |
| 98 | +
|
| 99 | +#include <EEPROM.h> |
| 100 | +
|
| 101 | +void setup() |
| 102 | +{ |
| 103 | + for (int i = 0; i < 255; i++) |
| 104 | + EEPROM.write(i, i); |
| 105 | +} |
| 106 | +
|
| 107 | +void loop() |
| 108 | +{ |
| 109 | +} |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +### `update()` |
| 114 | + |
| 115 | +#### Description |
| 116 | +Write a byte to the EEPROM. The value is written only if differs from the one already saved at the same address. |
| 117 | + |
| 118 | +#### Syntax |
| 119 | + |
| 120 | +``` |
| 121 | +EEPROM.update(address, value) |
| 122 | +
|
| 123 | +``` |
| 124 | + |
| 125 | +#### Parameters |
| 126 | +address: the location to write to, starting from 0 (int) |
| 127 | + |
| 128 | +value: the value to write, from 0 to 255 (byte) |
| 129 | + |
| 130 | +#### Returns |
| 131 | +none |
| 132 | + |
| 133 | +>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 |
| 134 | +
|
| 135 | +#### Example |
| 136 | + |
| 137 | +``` |
| 138 | +
|
| 139 | +#include <EEPROM.h> |
| 140 | +
|
| 141 | +void setup() |
| 142 | +{ |
| 143 | + for (int i = 0; i < 255; i++) { |
| 144 | + // this performs as EEPROM.write(i, i) |
| 145 | + EEPROM.update(i, i); |
| 146 | + } |
| 147 | + for (int i = 0; i < 255; i++) { |
| 148 | + // write value "12" to cell 3 only the first time |
| 149 | + // will not write the cell the remaining 254 times |
| 150 | + EEPROM.update(3, 12); |
| 151 | + } |
| 152 | +} |
| 153 | +
|
| 154 | +void loop() |
| 155 | +{ |
| 156 | +} |
| 157 | + |
| 158 | +``` |
| 159 | + |
| 160 | +### `get()` |
| 161 | + |
| 162 | +#### Description |
| 163 | +Read any data type or object from the EEPROM. |
| 164 | + |
| 165 | +#### Syntax |
| 166 | + |
| 167 | +``` |
| 168 | +EEPROM.get(address, data) |
| 169 | +
|
| 170 | +``` |
| 171 | + |
| 172 | +#### Parameters |
| 173 | +address: the location to read from, starting from 0 (int) |
| 174 | + |
| 175 | +data: the data to read, can be a primitive type (eg. float) or a custom struct |
| 176 | + |
| 177 | +#### Returns |
| 178 | +A reference to the data passed in |
| 179 | + |
| 180 | +#### Example |
| 181 | + |
| 182 | +``` |
| 183 | +
|
| 184 | +#include <EEPROM.h> |
| 185 | +
|
| 186 | +struct MyObject{ |
| 187 | + float field1; |
| 188 | + byte field2; |
| 189 | + char name[10]; |
| 190 | +}; |
| 191 | +
|
| 192 | +void setup(){ |
| 193 | +
|
| 194 | + float f = 0.00f; //Variable to store data read from EEPROM. |
| 195 | + int eeAddress = 0; //EEPROM address to start reading from |
| 196 | +
|
| 197 | + Serial.begin( 9600 ); |
| 198 | + while (!Serial) { |
| 199 | + ; // wait for serial port to connect. Needed for Leonardo only |
| 200 | + } |
| 201 | + Serial.print( "Read float from EEPROM: " ); |
| 202 | +
|
| 203 | + //Get the float data from the EEPROM at position 'eeAddress' |
| 204 | + EEPROM.get( eeAddress, f ); |
| 205 | + Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. |
| 206 | +
|
| 207 | + // get() can be used with custom structures too. |
| 208 | + eeAddress = sizeof(float); //Move address to the next byte after float 'f'. |
| 209 | + MyObject customVar; //Variable to store custom object read from EEPROM. |
| 210 | + EEPROM.get( eeAddress, customVar ); |
| 211 | +
|
| 212 | + Serial.println( "Read custom object from EEPROM: " ); |
| 213 | + Serial.println( customVar.field1 ); |
| 214 | + Serial.println( customVar.field2 ); |
| 215 | + Serial.println( customVar.name ); |
| 216 | +} |
| 217 | +
|
| 218 | +void loop(){ /* Empty loop */ } |
| 219 | + |
| 220 | +``` |
| 221 | + |
| 222 | +### `put()` |
| 223 | + |
| 224 | +#### Description |
| 225 | +Write any data type or object to the EEPROM. |
| 226 | + |
| 227 | +#### Syntax |
| 228 | + |
| 229 | +``` |
| 230 | +EEPROM.put(address, data) |
| 231 | +
|
| 232 | +``` |
| 233 | + |
| 234 | +#### Parameters |
| 235 | +address: the location to write to, starting from 0 (int) |
| 236 | + |
| 237 | +data: the data to write, can be a primitive type (eg. float) or a custom struct |
| 238 | + |
| 239 | +#### Returns |
| 240 | +A reference to the data passed in |
| 241 | + |
| 242 | +>Note: This function uses EEPROM.update() to perform the write, so does not rewrites the value if it didn't change. |
| 243 | +
|
| 244 | +#### Example |
| 245 | + |
| 246 | +``` |
| 247 | +
|
| 248 | +#include <EEPROM.h> |
| 249 | +
|
| 250 | +struct MyObject { |
| 251 | + float field1; |
| 252 | + byte field2; |
| 253 | + char name[10]; |
| 254 | +}; |
| 255 | +
|
| 256 | +void setup() { |
| 257 | +
|
| 258 | + Serial.begin(9600); |
| 259 | + while (!Serial) { |
| 260 | + ; // wait for serial port to connect. Needed for native USB port only |
| 261 | + } |
| 262 | +
|
| 263 | + float f = 123.456f; //Variable to store in EEPROM. |
| 264 | + int eeAddress = 0; //Location we want the data to be put. |
| 265 | +
|
| 266 | +
|
| 267 | + //One simple call, with the address first and the object second. |
| 268 | + EEPROM.put(eeAddress, f); |
| 269 | +
|
| 270 | + Serial.println("Written float data type!"); |
| 271 | +
|
| 272 | + /** Put is designed for use with custom structures also. **/ |
| 273 | +
|
| 274 | + //Data to store. |
| 275 | + MyObject customVar = { |
| 276 | + 3.14f, |
| 277 | + 65, |
| 278 | + "Working!" |
| 279 | + }; |
| 280 | +
|
| 281 | + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. |
| 282 | +
|
| 283 | + EEPROM.put(eeAddress, customVar); |
| 284 | + Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); |
| 285 | +} |
| 286 | +
|
| 287 | +void loop() { /* Empty loop */ } |
| 288 | + |
| 289 | +``` |
| 290 | + |
| 291 | +### `EEPROM[]` |
| 292 | +Description |
| 293 | +This operator allows using the identifier `EEPROM` like an array. EEPROM cells can be read and written directly using this method. |
| 294 | + |
| 295 | +#### Syntax |
| 296 | + |
| 297 | +``` |
| 298 | +EEPROM[address] |
| 299 | +
|
| 300 | +``` |
| 301 | + |
| 302 | +#### Parameters |
| 303 | +address: the location to read/write from, starting from 0 (int) |
| 304 | + |
| 305 | +#### Returns |
| 306 | +A reference to the EEPROM cell |
| 307 | + |
| 308 | +#### Example |
| 309 | + |
| 310 | +``` |
| 311 | +
|
| 312 | +#include <EEPROM.h> |
| 313 | +
|
| 314 | +void setup(){ |
| 315 | +
|
| 316 | + unsigned char val; |
| 317 | +
|
| 318 | + //Read first EEPROM cell. |
| 319 | + val = EEPROM[ 0 ]; |
| 320 | +
|
| 321 | + //Write first EEPROM cell. |
| 322 | + EEPROM[ 0 ] = val; |
| 323 | +
|
| 324 | + //Compare contents |
| 325 | + if( val == EEPROM[ 0 ] ){ |
| 326 | + //Do something... |
| 327 | + } |
| 328 | +} |
| 329 | +
|
| 330 | +void loop(){ /* Empty loop */ } |
| 331 | + |
| 332 | +``` |
0 commit comments