# Need explanation for EEPROM usage (generally and on Photon)

**URL:** <https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172>\
**Category:** Firmware\
**Created:** [March 17, 2016, 4:35pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172 "2016-03-17T16:35:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![superholz](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@superholz](https://community.particle.io/u/superholz)\
**Post date:** [March 17, 2016, 4:35pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/1 "2016-03-17T16:35:49Z")

</div>

I’m lacking some basic understanding on how the EEPROM on the Photon can be used. As I understand there are 2047 bytes of date accessible as permanent storage. Every byte can be addressed by an address as an integer. What can I store in one byte under one address? One byte is 8 bit I guess - so can I store a number from 0 to 255? If correct what do I do if I want to store bigger numbers or strings with more than one character. Can I store characters?

If I read/write access the storage address as I understand it I store there “pure data”, so I could not actually store a letter but only …yes what a number coding for the letter?

If I use the function storing an object instead of directly accessing data, what would be the practical difference?

Would appreciate if someone could give me a hint.

Sebastian

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [March 17, 2016, 6:25pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/2 "2016-03-17T18:25:08Z")

</div>

Here’s an example of reading and writing all sorts of data into EEPROM.

```cpp
// EEPROM sample code
// EEPROM Documentation: https://docs.particle.io/reference/firmware/photon/#eeprom

#include "Particle.h"

void clearEEPROM(); // forward declaration

// This is just a list of 16 fish names from Wikipedia for testing
const char *fishNames[] = {"Aeneus corydoras", "African glass catfish", "African lungfish", "Aholehole",
						   "Airbreathing catfish", "Airsac catfish", "Alaska blackfish", "Albacore",
						   "Alewife", "Alfonsino", "Algae eater", "Alligatorfish",
						   "Alligator gar", "American sole", "Amur pike", "Anchovy"};

typedef struct {
	int a;
	float b;
	bool c;
} SimpleStruct;

void setup() {
	Serial.begin(9600);

	// clearEEPROM();
}

void loop() {
	int addr = 0;
	int intVal;

	// int
	{
		// You can get and put simple values like int, long, bool, etc. using get and put directly

		EEPROM.get(addr, intVal);
		Serial.printlnf("addr=%d, intVal=%d, sizeof(int)=%d", addr, intVal, sizeof(int));

		intVal++;
		EEPROM.put(addr, intVal);

		addr += sizeof(int);
	}
	// double
	{
		double doubleVal;

		// Same for float, double
		EEPROM.get(addr, doubleVal);
		Serial.printlnf("addr=%d, doubleVal=%lf, sizeof(doubleVal)=%d", addr, doubleVal, sizeof(doubleVal));

		doubleVal += 0.1;
		EEPROM.put(addr, doubleVal);

		addr += sizeof(doubleVal);
	}

	// Strings are a bit more of a pain because you have to know how much space you want to reserve.
	// In this example, we store a string of up to 15 characters, plus a null byte, in a 16 character buffer
	{
		const int STRING_BUF_SIZE = 16;
		char stringBuf[STRING_BUF_SIZE];

		EEPROM.get(addr, stringBuf);
		stringBuf[sizeof(stringBuf) - 1] = 0; // make sure it's null terminated

		// Initialize a String object from the buffer
		String str(stringBuf);

		Serial.printlnf("addr=%d, str=%s, sizeof(stringBuf)=%d", addr, str.c_str(), sizeof(stringBuf));

		str = String(fishNames[intVal & 0xf]);
		Serial.printlnf("next fish name=%s", str.c_str());

		// getBytes handles truncating the string if it's longer than the buffer.
		str.getBytes((unsigned char *)stringBuf, sizeof(stringBuf));
		EEPROM.put(addr, stringBuf);

		addr += sizeof(stringBuf);
	}

	// A simple structure
	{
		SimpleStruct data;

		// You can even store a small structure of values
		EEPROM.get(addr, data);
		Serial.printlnf("addr=%d, a=%d b=%f c=%d, sizeof(data)=%d", addr, data.a, data.b, data.c, sizeof(data));

		data.a += 2;
		data.b += 0.02;
		data.c = !data.c;

		EEPROM.put(addr, data);

		addr += sizeof(data);
	}
	Serial.println("--------");

	delay(30000);
}

void clearEEPROM() {
	for(int addr = 0; addr < 256; addr++) {
		EEPROM.write(addr, 0);
	}
}

```

And the sample output from running it:

```auto
addr=0, intVal=0, sizeof(int)=4
addr=4, doubleVal=0.000000, sizeof(doubleVal)=8
addr=12, str=, sizeof(stringBuf)=16
next fish name=African glass catfish
addr=28, a=0 b=0.000000 c=0, sizeof(data)=12
--------
addr=0, intVal=1, sizeof(int)=4
addr=4, doubleVal=0.100000, sizeof(doubleVal)=8
addr=12, str=African glass c, sizeof(stringBuf)=16
next fish name=African lungfish
addr=28, a=2 b=0.020000 c=1, sizeof(data)=12
--------
addr=0, intVal=2, sizeof(int)=4
addr=4, doubleVal=0.200000, sizeof(doubleVal)=8
addr=12, str=African lungfis, sizeof(stringBuf)=16
next fish name=Aholehole
addr=28, a=4 b=0.040000 c=0, sizeof(data)=12
--------
addr=0, intVal=3, sizeof(int)=4
addr=4, doubleVal=0.300000, sizeof(doubleVal)=8
addr=12, str=Aholehole, sizeof(stringBuf)=16
next fish name=Airbreathing catfish
addr=28, a=6 b=0.060000 c=1, sizeof(data)=12
--------

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 18, 2016, 7:21am UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/3 "2016-03-18T07:21:56Z")

</div>

> [@superholz](#):
>
> so I could not actually store a letter but only ....yes what a number coding for the letter?

I do hear some misconception here.  
A binary processor does not differenciate between numbers or letters, hex or dec numbers or such - it only deals with bits and collections of them, which we humans connotate with some kind of human-readable representation.  
So storing a `byte` _number_ or a `char` _letter_ _ **is** _ the _same_ for the processor, we only tell the compiler that we want to _see_ the one collection of bits as _number_ and the other as _letter_.

And as a next step, if you want to store strings or bigger numbers or such, that's also just a collection of bytes stored in sequence starting at one byte address and taking up the space of multiple individually addressable bytes (which we don't intend to read on their own).

For storing and reading back these _compounds_ the docs tell about  
[`EEPROM.put()`](https://docs.particle.io/reference/firmware/photon/#put-) and [`EEPROM.get()`](https://docs.particle.io/reference/firmware/photon/#get-)

---

<div class="post-metadata">

**Author:** ![superholz](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@superholz](https://community.particle.io/u/superholz)\
**Post date:** [March 18, 2016, 3:58pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/4 "2016-03-18T15:58:15Z")

</div>

Thank you both for the example and explanation. For my program I will use the get and put methods. If I would use the write method what do I actually store under the byte address? In the reference example:

> int addr = 1;  
> uint8\_t val = 0x45;  
> EEPROM.write(addr, val);

The "0x45" is that hexadecimal representation of the data stored? Do I have to use hexadecimal?

If I use the put method. The library will take care of how many bytes will be used. But do i have to consider the length of my data? In other words if I want to store two objects wich are larger than 1 byte I could not use 0 and 1 as adresses for the two objects because otherwise the second will be written partly on top of the first, correct?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 18, 2016, 5:26pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/5 "2016-03-18T17:26:25Z")

</div>

As said, hex is only for us humans to understand it. If you write 0x45 or 69 or ‘E’ does not matter.

For the length of your data you can use the `sizeof(firstVar)` operator to calculate the next base address.

---

<div class="post-metadata">

**Author:** ![moz](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moz/32/20176_2.png) [@moz](https://community.particle.io/u/moz)\
**Post date:** [August 16, 2019, 2:00pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/6 "2019-08-16T14:00:08Z")

</div>

How can we store IPAddress values in EEPROM?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 16, 2019, 2:03pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/7 "2019-08-16T14:03:41Z")

</div>

What have you tried?  
In what way did it not work?  
If you haven’t tried anything, may I encourage you to do?

This might be a good place to start from  
[https://docs.particle.io/reference/device-os/firmware/photon/#put-](https://docs.particle.io/reference/device-os/firmware/photon/#put-)

---

<div class="post-metadata">

**Author:** ![moz](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moz/32/20176_2.png) [@moz](https://community.particle.io/u/moz)\
**Post date:** [August 16, 2019, 2:09pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/8 "2019-08-16T14:09:03Z")

</div>

Well I did try it the direct way where I pass it the IP on an address. However, when I try to read it, the photon goes into SOS mode.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 16, 2019, 2:47pm UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/9 "2019-08-16T14:47:29Z")

</div>

You can try this

```cpp
  EEPROM.put(addr, ip.raw());
  EEPROM.get(addr, ip.raw());

```

---

<div class="post-metadata">

**Author:** ![shanevanj](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/shanevanj/32/21186_2.png) [@shanevanj](https://community.particle.io/u/shanevanj)\
**Post date:** [August 17, 2019, 4:56am UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/10 "2019-08-17T04:56:27Z")

</div>

As an alternate to try - [this](https://github.com/jwhiddon/EDB) library abstracts some of the concepts and while you could be more space efficient, it does provide a simple approach to managing data in records. I use out all the time to hold configuration information on my devices.

---

<div class="post-metadata">

**Author:** ![prusaga](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prusaga](https://community.particle.io/u/prusaga)\
**Post date:** [June 2, 2020, 6:16am UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/12 "2020-06-02T06:16:25Z")

</div>

Hello,  
Can someone help me understand the limitation in the available permanent memory? As the device for example electron has 1Mb storage capacity but the usable permanent space is getting reduced to 2047 bytes. Can this be increased somehow or it is a limitation from hardware point of view?

Regards,  
Prudhvi Sagar

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 2, 2020, 7:45am UTC](https://community.particle.io/t/need-explanation-for-eeprom-usage-generally-and-on-photon/21172/13 "2020-06-02T07:45:57Z")

</div>

First you may want to have a look at the [memory map](https://docs.particle.io/datasheets/electron/electron-datasheet/#memory-map) how the 1MB are divided into multiple sections.  
And then from that  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/b/a/baa2aea127e267cc5678b16c1f4aa666fac09ca5.png)  
plus the documentation about [EEPROM](https://docs.particle.io/reference/device-os/firmware/electron/#eeprom) you should be able to see _why_ you only get 2K usable EEPROM space (due to the need for wear leveling).

If you need more data stored you could use low level access to the EEPROM emulation banks and place your data there directly, but this is not recommended nor officially supported.
