Storing and Retrieving Characters, Floats, and Structs in Arduino’s Internal EEPROM Memory for Absolute beginner

RSDevX
3 min readNov 29, 2023

Arduino microcontrollers are widely used for a variety of projects due to their versatility and ease of use. In this article, we will explore how to leverage the internal EEPROM (Electrically Erasable Programmable Read-Only Memory) of an Arduino to store and retrieve characters, floats, and structs.

We also have an Youtube Video Tutorials(below)

Source Codes

All Source codes can be downloaded using the below link

Arduino boards feature EEPROM with varying sizes, ranging from 1024 bytes on the Arduino UNO to 4096 bytes on the Arduino Mega board. This storage space is particularly beneficial for preserving small datasets like serial numbers, calibration values, and unique identifiers.

It’s crucial to be aware of the limited write/erase endurance cycle of EEPROM, which is typically around 100,000 operations. Excessive writing and erasing, especially to the same byte location, can lead to the wearing out of the memory cell at that specific location.

The code provided here is versatile and can be applied to various Arduino boards that utilize Microchip AVR ATmega Microcontrollers.

Determining EEPROM Size on Arduino UNO

To determine the EEPROM size on your Arduino board, you can utilize the EEPROM.length() function, which provides the size in the form of an unsigned integer. The following program demonstrates how to display the number of available bytes:

#include <EEPROM.h>

void setup() {
// Initialize Serial communication
Serial.begin(9600);

// Get the EEPROM size
unsigned int eepromSize = EEPROM.length();

// Display the EEPROM size
Serial.print(“EEPROM Size: “);
Serial.print(eepromSize);
Serial.println(“ bytes”);
}

void loop() {
// Nothing to do here for this example
}

This program includes the EEPROM.h library and utilizes the EEPROM.length() function to retrieve the EEPROM size. The obtained size is then printed to the Serial Monitor, providing valuable information about the available EEPROM space on your Arduino board.

Storing a Byte in Arduino’s EEPROM Memory

To write a byte to the Arduino EEPROM, you can use the EEPROM.write() function. Here’s a simple example:

#include <EEPROM.h>

void setup() {
// Initialize Serial communication
Serial.begin(9600);

// Data to be stored
byte dataToStore = 42;

// Write the byte to EEPROM address 0
EEPROM.write(0, dataToStore);
}

void loop() {
// No specific actions required in this example’s loop
}
In this example, the value 42 is stored at EEPROM address 0 using the EEPROM.write() function.

Reading a Byte from EEPROM:

Reading a byte from EEPROM involves using the EEPROM.read() function. Here’s an example:

#include <EEPROM.h>

void setup() {
// Initialize Serial communication
Serial.begin(9600);

// Read the byte from EEPROM address 0
byte retrievedData = EEPROM.read(0);

// Display the retrieved byte
Serial.print(“Retrieved Byte: “);
Serial.println(retrievedData);
}

void loop() {
// No specific actions required in this example’s loop
}
In this example, the byte stored at EEPROM address 0 is read using EEPROM.read() and then displayed on the Serial Monitor.

Storing a float value in a EEPROM of Arduino

Storing a float or double value in the EEPROM of an Arduino involves some additional considerations due to the fact that these data types occupy multiple bytes. Unlike a byte or integer, which takes up a single byte, a float typically requires four bytes, and a double requires eight bytes.

To store a float or double in the EEPROM, you can use the EEPROM.put() function, which is designed to write multiple bytes to EEPROM. Here’s an example:

#include <EEPROM.h>

void setup() {
// Initialize Serial communication
Serial.begin(9600);

// Data to be stored
float floatValue = 3.14;

// Write the float to EEPROM starting at address 0
EEPROM.put(0, floatValue);
}

void loop() {
// No specific actions required in this example’s loop
}

In this example, the EEPROM.put() function is used to write the float value 3.14 to the EEPROM starting at address 0.

// Read the float from EEPROM starting at address 0
float retrievedFloat;
EEPROM.get(0, retrievedFloat);

// Display the retrieved float
Serial.print(“Retrieved Float: “);
Serial.println(retrievedFloat);

--

--