How do you store and retrieve data from EEPROM on Arduino?

 To store and retrieve data from EEPROM on Arduino, you can use the built-in EEPROM library. This allows you to store data that persists even after power is lost.


1. Include the EEPROM Library

cpp

#include <EEPROM.h>

2. Writing Data to EEPROM

a. Write a Byte

cpp

EEPROM.write(address, value); // value: 0–255

b. Write Any Data Type Using EEPROM.put()

cpp

int myValue = 123; EEPROM.put(address, myValue); // Handles any type (int, float, struct, etc.)

3. Reading Data from EEPROM

a. Read a Byte

cpp

byte val = EEPROM.read(address);

b. Read Any Data Type Using EEPROM.get()

cpp

int myValue; EEPROM.get(address, myValue);

EEPROM Characteristics

  • Limited write cycles (~100,000 per cell)

  • Non-volatile (keeps data after reset/power off)

  • Use EEPROM.update() instead of write() to avoid unnecessary writes.


Example: Store and Retrieve an Integer

cpp

#include <EEPROM.h> void setup() { Serial.begin(9600); int number = 42; // Store the integer EEPROM.put(0, number); // Read it back int storedNumber; EEPROM.get(0, storedNumber); Serial.print("Stored number: "); Serial.println(storedNumber); } void loop() { // Nothing here }

评论

此博客中的热门博文

How To Connect Stm32 To PC?

What is a Look-Up Table (LUT) in an FPGA, and how does it work?

Detailed Explanation of STM32 HAL Library Clock System