Every Arduino has a built in EEPROM. Depending on the actual MCU on your Arduino Board the size of the EEPROM will vary.
In this article you will learn how to
- Read and write Float variables into EEPROM easily
- Store and retrieve custom data types easily
Let us get started 🏃♀️
Arduino CodeHere is the example which demonstrates the EEPROM operation. Please scroll to the end of the article to see the complete code with formatting
Here is the output viewYou can use this link to play with the simulator, tinker with the code and add useful sections to your projects in the future!
Feel free to share your feedback on this article in the comments
https://wokwi.com/arduino/projects/323281869291389524
Writing a FLOAT data type into Arduino UNO
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);
The EEPROM.put is the key here. You have to send two parameters
- The
address in the EEPROM
where you want to write the data - the
data
to be written
The EEPROM put()
function accepts simply the address and the object. The function interface is too simple and very helpful.
-------------------------------------------------------------------------------------------
Links for EEPROM put()
function - https://www.arduino.cc/en/Reference/EEPROMPut
Links for EEPROM get()
function - https://www.arduino.cc/en/Reference/EEPROMGet
While reading the data, you have to make sure you send the object parameter correctly. Once you read it, you can print the object elements easily.
EEPROM.get( eeAddress, customVar );EEPROM.get( eeAddress, customVar );
Serial.println( "Read custom object from EEPROM: " );
Serial.println( customVar.field1 );
Serial.println( customVar.field2 );
Serial.println( customVar.name );
I hope you will be able to use these functions in your next project and easily store and retrieve custom datatypes easily.
ConclusionIn this article you saw how to read and write objects (float types, integers, text and more) easily! Please feel free to post your questions and feedback.
Thank you and see you next time!
Comments
Please log in or sign up to comment.