# How i can write float to EEPROM

**URL:** <https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702>\
**Category:** Programming\
**Created:** [September 10, 2012, 6:13pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702 "2012-09-10T18:13:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 10, 2012, 6:13pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/1 "2012-09-10T18:13:45Z")

</div>

Hi guys i'm trying write a float to EEPROM

Of course EEPROM can only write int...So i really dont understand how i can write a float number ?

I read many topics here, but i still dont understand

Here a part of my code

float result;  
float soma=0.5;

if(state==LOW) // Menu temperature set  
{

lcd.clear();  
lcd.setCursor(0, 0);  
lcd.print("T.A.");  
lcd.setCursor(0, 1);  
lcd.print(result);  
delay(1000);  
readingpinaq = digitalRead(pinup);  
readingpinfaq2 = digitalRead(pindown);  
if(readingpinaq==HIGH)  
{  
result=result+soma;  
EEPROM.write(addr, result); // at this part i want write this float result

if(result\>=31)  
{  
result=31;

}  
}  
if(readingpinfaq2==HIGH)  
{  
result=result-soma;  
EEPROM.write(addr, result);  
if(result\<=18)  
{  
result=18;

}  
}

}

Help me

Tanks

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 10, 2012, 6:55pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/2 "2012-09-10T18:55:46Z")

</div>

This will make it a lot easier - [Arduino Playground - EEPROMWriteAnything](http://arduino.cc/playground/Code/EEPROMWriteAnything).

---

<div class="post-metadata">

**Author:** ![marcello.romani](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcello.romani](https://forum.arduino.cc/u/marcello.romani)\
**Post date:** [September 10, 2012, 9:09pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/3 "2012-09-10T21:09:10Z")

</div>

> Of course EEPROM can only write int...So i really dont understand how i can write a float number ?

No, EEPROM stores _bytes_. The meaning of those bytes is program-dependent.

Think about an integer variable or a float variable as a sequence of sizeof(variable) bytes. Each single byte can be transferred from RAM to EEPROM or vice-versa. In fact, you write or read one byte at a time to/from EEPROM.

Let me try with a pseudo-code example:

```arduino
EEPROM.write(addr, result);

```

becomes:

```arduino
ramAddr = &result;
eepromAddr = addr;
for i = 0 to sizeof(result) do:
    b = value of byte at ramAddr + i
    eeprom.writebyte(eepromAddr + i, b)
done

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 10, 2012, 9:43pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/4 "2012-09-10T21:43:56Z")

</div>

I use the "EEPROMAnything.h" and work. But i will try your code tux, i need understand

Reply later

Tanks !

---

<div class="post-metadata">

**Author:** ![lemming](https://avatars.discourse-cdn.com/v4/letter/l/ccd318/32.png) [@lemming](https://forum.arduino.cc/u/lemming)\
**Post date:** [September 10, 2012, 10:11pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/5 "2012-09-10T22:11:02Z")

</div>

I use the following to write setup settings to EEPROM:

```cpp
int NoElements = 3;

unsigned int setting_table[3][3] = {
 { 20, 20, 50}, 
 { 10, 0, 600}, 
 { 150, 20, 255},
};
..................
................
.......................

int ReadSetup()
{
  for (int i = 0; i < NoElements; i++)
  {
    byte h = EEPROM.read(2 * i);
    byte l = EEPROM.read((2 * i) + 1);
    setting_table[i][0] = word(h, l);  
  }
  return 0;
}

int WriteSetup()
{
  for (int i = 0; i < NoElements; i++)
  {
    byte h = highByte(setting_table[i][0]);
    byte l = lowByte(setting_table[i][0]);
    EEPROM.write(2 * i, h);
    EEPROM.write((2 * i) + 1, l);
  }
  return 0;
}

```

Haven't got time to adapt to your sketch but come back if you're stuck and I'll have a look at it later.  
The EEPROM only accepts bytes while an integer is two bytes. Double or floats use four bytes, therefore use a "4" instead of the "2"s above and use something other than highByte/lowByte to break up the double into its four constituent bytes.

---

<div class="post-metadata">

**Author:** ![lemming](https://avatars.discourse-cdn.com/v4/letter/l/ccd318/32.png) [@lemming](https://forum.arduino.cc/u/lemming)\
**Post date:** [September 11, 2012, 10:06am UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/6 "2012-09-11T10:06:38Z")

</div>

Had a second look and its a bit more complex. Try this:

```cpp
#include <EEPROM.h>

float num1 = 63412;
float num2 = 0;

void setup()
{
  Serial.begin(9600);
  delay(2000);
  EEPROM_Write(&num1, 1); // memory position tested as '1'. Memory position
  EEPROM_Read(&num2, 1); // starts at 0 and goes up to max depending on processor.
  Serial.println(num2, DEC);    
}

void loop()
{
}

void EEPROM_Write(float *num, int MemPos)
{
  byte ByteArray[4];
  memcpy(ByteArray, num, 4);
  for(int x = 0; x < 4; x++)
  {
    EEPROM.write((MemPos * 4) + x, ByteArray[x]);
  }  
}

void EEPROM_Read(float *num, int MemPos)
{
  byte ByteArray[4];
  for(int x = 0; x < 4; x++)
  {
    ByteArray[x] = EEPROM.read((MemPos * 4) + x);    
  }
  memcpy(num, ByteArray, 4);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 11, 2012, 8:57pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/7 "2012-09-11T20:57:10Z")

</div>

Tanks lemming, works fine !

I will this post help another people

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:55pm UTC](https://forum.arduino.cc/t/how-i-can-write-float-to-eeprom/119702/8 "2021-05-05T19:55:07Z")

</div>

Closed on May 5, 2021, 7:55pm UTC


