Hack star
Published © MIT

Read and Write Float values into EEPROM on Arduino UNO

You will learn how to read and write floating value into EEPROM easily. Learn how to work with custom data types as well! Using Wokwi!

BeginnerProtip8,935
Read and Write Float values into EEPROM on Arduino UNO

Things used in this project

Story

Read more

Code

EEPROM read and write in a easier way

Arduino
#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 */ }

Credits

Hack star

Hack star

75 projects β€’ 98 followers
an Arduino enthusiast and an electronic hobbyist

Comments