Saving Floating Point into EEPROM

Hi, i have been learning using Arduino Uno for only about three weeks now for my industrial training.
I have been assigned on a project to figure out how to store floating point(with atleast 8 decimal point) in the inbuilt EEPROM.

I tried the union struct but it only allows up to 2 decimal point. Then, I use the dtostrf function where I successfully display the floating point but I do not know how to store it in EEPROM as the dtostrf function changes the float into char array as shown in the attached coding.

Can anyone please help me on the coding to write and read the value into EEPROM. I’d really appreciate it, thank you :slight_smile:

dtostrf.ino (254 Bytes)

You can use the .get() & .put() methods to achieve this easily. There is no need to convert the float to a string (most of the time it would use more memory anyway.

#include <EEPROM.h>

static float val=1.2345678;

int address = 0;

void setup() {

  //Write value to EEPROM starting at 'address'.
  EEPROM.put( address, val );
  
  //Read value from EEPROM starting at 'address'.
  EEPROM.get( address, val );
}

void loop() {}

Click the link in my signature for more info.

FrhNadiah:
I tried the union struct but it only allows up to 2 decimal point.

What do you mean by that? This is a display issue, not a storage one.

You don’t need to attach such small code. Here is your code:

#include <EEPROM.h>
static float val=1.2345678;
char out[15];
void setup() {
  dtostrf(val,13,12,out);
  Serial.begin(9600);
  for(int i=0;i<16; i++){
    EEPROM.write(i,out[i]);
    float a = EEPROM.read(i); 
    Serial.println(a);
  }

}

void loop() {

}

This looks very weird. You are reading a float from EEPROM, 16 times. Is this wise? Don’t you want to read a byte?

pYro_65:
You can use the .get() & .put() methods to achieve this easily. There is no need to convert the float to a string (most of the time it would use more memory anyway.

#include <EEPROM.h>

static float val=1.2345678;

int address = 0;

void setup() {

//Write value to EEPROM starting at ‘address’.
  EEPROM.put( address, val );
 
  //Read value from EEPROM starting at ‘address’.
  EEPROM.get( address, val );
}

void loop() {}




Click the link in my signature for more info.

Thank you sir, this is great but I need to be able to read/display the value on the serial window and no matter how I tried, it only displayed up to 2 decimal point.

Yes Sir, i am so sorry if I sounded stupid but this is all really new to me and I have been learning these past few weeks on my own reading forums here and there, so I would say that my skill are not that good. I was trying to read the float byte by byte to be displayed on the window. I am sorry if it is incorrect and I would love it if you can enlighten me :smiley:

I am having trouble storing the floating point as well as to display them the way they were stored.

I really appreciate any of your help. Thank you sir :slight_smile:

You can print longer decimal parts using the second parameter (the default is 2):

Serial.print( 1.23456f, 5 );

See the reference: Serial.print() - Arduino Reference

See http://playground.arduino.cc/Code/EEPROMWriteAnything - you don’t need to reinvent the wheel here.

WOWWWW!!

Wish I knew this earlier,would save a lot of my time. Thank you so much both of you for the help. Really appreciate it!! :slight_smile: :slight_smile:

pYro_65:
You can print longer decimal parts using the second parameter (the default is 2):

Serial.print( 1.23456f, 5 );

See the reference: Serial.print() - Arduino Reference

Hi, sorry I need to ask something again. I used the code you gave earlier and also I add the second parameter for the serial.print. I successfully displayed the floating point but when close the program and tried to code only to read the data located in the address before, it does not display the previous saved value. Do you know where did i get it wrong?

You didn't post your code so we can't say.

You didn't post your code so we can't say.

Sure we can. OP didn't post his/her code. THAT was here he/she got it wrong.