.st0{fill:#FFFFFF;}

Arduino

Using EEPROM on Arduino for Permanent Data Storage 

 June 14, 2023

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

As you embark on this DIY journey with your trusty Arduino board, you’ve likely asked yourself, “How can I store data permanently, even when my Arduino is switched off or loses power?” This is where our good friend EEPROM comes into play.

What is EEPROM?

EEPROM stands for Electrically Erasable Programmable Read-Only Memory. It’s a bit of a mouthful, huh? Simply put, it’s a memory that doesn’t lose its content when the power is turned off. Yep, you read that right. It’s like that little notepad that keeps all your Arduino’s important doodles and sketches safe and sound.

Your Arduino Uno, powered by the ATMega328P microcontroller, comes with 1KB of built-in EEPROM. Not a ton, but often just the right amount to store essential configuration parameters, high scores for Arduino-based games, or sensor data logs when transmitting isn’t an immediate option.

EEPROM in the ATMEGA328P: a quick spec review

The Atmel ATmega328P microcontroller, the powerhouse of the popular Arduino Uno board, features a built-in EEPROM. Here are its key technical specifications:

  1. EEPROM Size: 1KB (1024 bytes) – Each address stores one byte of data; hence you have 1024 locations (0 to 1023) to store your data.
  2. Data retention: 20 years at 85°C/100 years at 25°C. So, your data will stay safe and sound for quite a while!
  3. Endurance: Approximately 100,000 write/erase cycles per cell. While this seems like a lot, it can run out surprisingly quickly in a program with a loop that writes to the EEPROM frequently.
  4. Access Time: The ATmega328P’s EEPROM can be accessed with a read time of 3.4 ms and a write time of 3.4 ms. It’s not the fastest form of storage, but it’s perfect for non-volatile storage needs.
  5. Byte Access: The EEPROM allows for single-byte reads and writes, ensuring efficient space utilisation. There are also functions provided in the Arduino IDE for multi-byte data types.
  6. In-System Programmable: The EEPROM can be accessed and written to while the system is running. This allows for real-time data storage and logging.

Remember, the EEPROM is a precious resource, so use it wisely and sparingly. Regular, unnecessary writing can wear out the EEPROM, limiting its lifespan. Always check if the existing value is the same as the one you want to write, and if it is, you can skip the write operation.

Dipping Our Toes into EEPROM

Let’s start with the basics. You will need the EEPROM library to access the EEPROM on your Arduino. It’s one of the standard libraries bundled with the Arduino IDE, so you don’t have to download anything new. Handy, right?

Just start your sketch with #include <EEPROM.h> at the top of your code, and you’re good to go!

Writing to EEPROM

Writing to the EEPROM is as easy as pie with the EEPROM.write() function. This function needs two arguments:

  1. The address in the EEPROM (from 0 to 1023 for Arduino Uno).
  2. The data you want to write (a single byte).

Here’s how it looks:

#include <EEPROM.h>

void setup() {
  EEPROM.write(0, 'A'); // writing 'A' to address 0
}

Reading from EEPROM

Reading from EEPROM is just as straightforward. Use the EEPROM.read() function. It only requires the address you wish to read:

#include <EEPROM.h>

void setup() {
  char myChar = EEPROM.read(0); // reading from address 0
  Serial.begin(9600);
  Serial.println(myChar);
}

Saving and Retrieving More Than Just Bytes

While the basic functions allow you to store and retrieve single bytes, you might ask, “What if I want to store larger data types, like integers or floating-point values?”

Here’s what to do:

Storing and Retrieving Integers

Integers in Arduino are 2-byte (16-bit) data, so they can’t fit into the 1-byte space that EEPROM.write() provides. But with a little bit of trickery, we can make it work. Here’s how:

#include <EEPROM.h>

void setup() {
  int numberToStore = 12345;
  EEPROM.put(0, numberToStore); // Storing the integer
}

void loop() {
  int retrievedNumber;
  EEPROM.get(0, retrievedNumber); // Retrieving the integer
  Serial.begin(9600);
  Serial.println(retrievedNumber);
}

The Dos and Don’ts

Remember, EEPROM has a limited number of write cycles (about 100,000 times). Repeatedly writing to the same location can wear it out.

Try to minimize writes by checking if the new value differs from the existing one. If it’s the same, skip the write.

Real-World EEPROM Scenarios: When and Why to Use It

Alright, you’ve got a good grasp on the how, but what about the why? When might you want to use EEPROM in your Arduino projects? Let’s dive into some practical scenarios.

Storing User Preferences or Game High Scores

Remember those retro arcade machines where you could proudly stamp your initials next to your high score? EEPROM is perfect for this! If you’re building a game with Arduino, you can store high scores in EEPROM. So, the top scores are right there whenever you power up your game, and the competition can continue!

Imagine a more complex scenario where you’re developing a multi-game arcade system. EEPROM can remember the last game played or specific game settings chosen by the user. You could even use it to store sound volume levels or game difficulty settings.

Data Logging When No Immediate Transmission is Possible

Say you’ve designed a weather station that collects data from various sensors. Suppose you can’t transmit the data immediately due to a lack of Wi-Fi, or maybe you just don’t want to overload your network with continuous data transmission.

In these cases, you can store the data in EEPROM and transmit it later when it’s more convenient. However, EEPROM on Arduino has limited storage (1KB for Uno), so consider the amount of data you’ll store.

Keeping Device State During Power Losses

You may have an Arduino project that needs to remember its state between power cycles. For instance, if you’re controlling garden lights that can be set to different modes, you wouldn’t want your Arduino to forget the chosen mode every time you power it down, right?

EEPROM can remember the last state or mode and set the lights back to that mode when powered back up. It’s like your Arduino has a little helper that never forgets.

Storing Network Configuration Settings

Let’s say you’ve made a smart device that connects to a Wi-Fi network. It’s much better to store network credentials on the device, so it can connect to the network whenever it powers up without needing additional setup.

By storing network SSID and password in EEPROM, your Arduino project can automatically reconnect to the network, saving the user the hassle of inputting these details every time.

Remember, when storing sensitive information, it’s crucial to consider security. While EEPROM storage is not typically easily accessible, it’s worth looking into some sort of data encryption.

Venturing Out: Exploring External EEPROM Options

So, you’ve explored the Arduino’s built-in EEPROM to its fullest, but it’s not enough? Maybe you’ve outgrown the 1KB limitation or hit the write cycle ceiling?

External EEPROM is here to save the day.

There are numerous EEPROM ICs and modules out there that provide much larger storage capacities than your Arduino’s built-in EEPROM. Here are some popular options:

  1. Microchip’s 24LC256: This EEPROM IC offers 256Kbits of storage space. That’s about 32KB, 32 times the Arduino Uno’s internal EEPROM!
  2. ST’s M24M01: Another IC with 1Mbit (128KB) storage space.
  3. SparkFun DeadOn RTC DS3234: An interesting combination of a Real-Time Clock and 256 bytes of EEPROM.
  4. Adafruit’s I2C Non-Volatile Data Storage: A module providing 32KB of storage, easily accessible over I2C.

Hooking Up the External EEPROM

Let’s take Microchip’s 24LC256 as an example. We must connect it to the Arduino Uno using the I2C interface. Here’s the wiring:

  • VCC (Pin 8 on the IC) connects to 5V on the Arduino.
  • GND (Pin 4) goes to the Arduino’s ground.
  • SDA (Pin 5) links to A4 on the Arduino Uno.
  • SCL (Pin 6) hooks up with A5 on the Arduino.
  • The address pins (A0, A1, and A2) should be connected to the ground for the address 0x50.

Coding with the External EEPROM

For communication with the 24LC256, we’ll use the Wire library. Here’s how you can write and read data:

#include <Wire.h>

#define EEPROM_ADDR 0x50

void setup() {
  Wire.begin(); // join I2C bus
}

void loop() {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write((int)(0 >> 8));   // MSB
  Wire.write((int)(0 & 0xFF)); // LSB
  Wire.write('A'); // Write 'A' at address 0
  Wire.endTransmission();

  delay(100); // Add a small delay to ensure the write completes

  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write((int)(0 >> 8));   // MSB
  Wire.write((int)(0 & 0xFF)); // LSB
  Wire.endTransmission();

  Wire.requestFrom(EEPROM_ADDR, 1); // Request 1 byte from EEPROM

  if (Wire.available()) {
    char c = Wire.read(); // Get the byte
    Serial.begin(9600);
    Serial.println(c); // Print the byte
  }

  while (1); // Loop indefinitely
}

In this example, we’re writing the character ‘A’ to address 0 of the EEPROM, then reading it back and printing it to the Serial Monitor.

Keep Exploring

In a nutshell, EEPROM can add a whole new level of functionality to your Arduino projects. Its ability to store data between power cycles opens up endless possibilities, from games that remember high scores to weather stations that store data for later transmission.

Okay, so you’ve got the basics down now. But as always, there’s much more to learn and discover about Arduino and EEPROM. Here are some key resources to help you dive deeper

  1. EEPROM Clear: An official Arduino guide on clearing the EEPROM.
  2. EEPROM Get: Another official guide, this time on using the EEPROM.get() function.
  3. EEPROM Put: Yet another official guide, this one’s about the EEPROM.put() function.

Happy tinkering!


Tags

EEPROM, Programming


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Understanding the power requirements is crucial for any enthusiast using the Arduino Uno in their projects. How you manage voltage, amperage, and power sources impacts the success or failure of your endeavours. In this guide,

Read More
The Ultimate Guide to Powering Your Arduino Uno Board

If you are curious about how electronic devices work, understanding the fundamentals of the operation of their basic components is essential. Every electronic device—from the smartphones in our pockets to the satellites orbiting our planet—contains

Read More
A Guide to Essential Electronic Components in Circuits