EEPROM stands from electrically erasable programmable Read-only Memory. Despite the name, the better definition is that it is a type of memory storage where you can store the data and read it back, even when there was a power loss. The data in the EEPROM stays as-is for years without any power backup.
The Arduino UNO comes with its own internal EEPROM memory inside the MCU itself. This saves money, as well as space on the PCB.
How much EEPROM memory does your Arduino have?
- 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.
- Storing the settings such as preferred flow of water, temperature settings
- storing brightness settings for the display
- storing user data such as names or ID etc
#include <EEPROM.h>
struct MyObject {
float field1;
byte field2;
char name[10];
};
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
float f = 123.456f; //Variable to store in EEPROM.
int eeAddress = 0; //Location we want the data to be put.
//One simple call, with the address first and the object second.
EEPROM.put(eeAddress, f);
Serial.println("Written float data type!");
/** Put is designed for use with custom structures also. **/
//Data to store.
MyObject customVar = {
3.14f,
65,
"Working!"
};
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.put(eeAddress, customVar);
Serial.print("\nWritten custom data type! Now we will read it in a second!");
delay(1000);
Serial.print("\n**************************************************");
Serial.print("\n Read float from EEPROM: " );
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get( eeAddress, f );
Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
// get() can be used with custom structures too.
eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
//MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get( eeAddress, customVar );
Serial.println( "Read custom object from EEPROM: " );
Serial.println( customVar.field1 );
Serial.println( customVar.field2 );
Serial.println( customVar.name );
}
void loop() { /* Empty loop */ }
Project link:
https://wokwi.com/arduino/projects/323281869291389524
Support/feedback/suggestions?you have many ways to ask for help, suggest a feature or share your feedback
- Open an issue on GitHub
- Visit Facebook group
- Hop on to Discord Server!
- leave a comment here 😀
Comments
Please log in or sign up to comment.