EEPROM (electrically erasable programmable read-only memory) is a type of user-modifiable ROM, also known as Flash Memory. It can be erased and reprogrammed (written) repeatedly by applying a voltage higher than normal voltage.
EEPROM is a non-volatile ROM that can erase and reprogram individual bytes of data. This is why EEPROM chips are referred to as byte-erasable chips. EEPROM is typically used to store small amounts of data in computing and other electronic devices.
The EEPROM size of Arduino Uno is 1024 bytes.
The EEPROM size of ESP32 is 512 bytes. This means that using ESP32 and the EEPROM library, there can be 512 different addresses, each capable of storing a value between 0 and 255.
In simple terms, the data in EEPROM can be saved even when the power is off; when powered back on, the data will not be lost. However, one limitation of flash memory is the number of times data can be rewritten. You can read data from flash memory as many times as needed, but most devices’ flash memory is designed for approximately 100,000 to 1,000,000 write operations.
Reading and writing in the flash memory of ESP32 will use the EEPROM library. It is essentially the same as Arduino’s EEPROM, with no significant differences.
This example mainly records how to use EEPROM on ESP32/Arduino, and how to read and write data.
Program Points
First, load the EEPROM library
// Load the EEPROM library
#include
Initialize the size of EEPROM in setup
// Define the size of EEPROM
#define EEPROM_SIZE 1 // Here define the size as 1 byte
void setup() {
...
// Initialize EEPROM to the predefined size
EEPROM.begin(EEPROM_SIZE);
...
}
Read data from a specified position in EEPROM
For example, read data from position 0:
read_value = EEPROM.read(0); // Read data from EEPROM position 0
Write data to a specified position in EEPROM
For example, rewrite the data at position 0 to the value of read_value. Finally, don’t forget to EEPROM.commit();
to commit, running this line of code is necessary to truly write the data.
EEPROM.write(0, read_value); // Write the value of variable read_value to position 0
EEPROM.commit(); // Must commit to truly write data to EEPROM
Complete Code
This program increments the variable read_value by 1 every second and writes it to EEPROM. When power is lost and restored, it can continue from the data before the power loss instead of starting from 0.
// welcome to lingshunlab.com
// Load the EEPROM library
#include
// Define the size of EEPROM
#define EEPROM_SIZE 1 // Here define the size as 1 byte
int read_value = 0;
void setup() {
Serial.begin(115200);
// Initialize EEPROM to the predefined size
EEPROM.begin(EEPROM_SIZE);
}
void loop() {
read_value = EEPROM.read(0); // Read data from EEPROM position 0
Serial.println(read_value);
read_value++; // Increment read_value + 1, but EEPROM only accepts values from 0 to 255, exceeding will result in a modulo 255 value
EEPROM.write(0, read_value); // Write the value of variable read_value to position 0
EEPROM.commit(); // Must commit to truly write data to EEPROM
delay(1000);
}