How to write data into EEPROM with Arduino?

ArduinoHardwareSoftware & Coding

Arduino Uno has 1 kB of EEPROM storage. EEPROM is a type of non-volatile memory, i.e., its contents are preserved even after power-down. Therefore, it can be used to store data that you want to be unchanged across power cycles. Configurations or settings are examples of such data.


We will see how to write data to the EEPROM in this example. We will be walking through an inbuilt example in Arduino. The EEPROM examples can be accessed from − File → Examples → EEPROM.

We will look at the eeprom_write example. It is quite straightforward, thanks to the EEPROM library. A word of caution here. Please use the .write() function frugally. Each EEPROM has a limited number of write cycles (~100,000) per address. If you write excessively to the EEPROM, you will reduce the lifespan of the EEPROM.

We begin with the inclusion of the library.

#include <EEPROM.h>

Next, a global variable is defined for the EEPROM address field (the address to which you will write)

int address = 0;

Within the setup, we do nothing.

void setup() {
   /** Empty setup. **/

In the loop, we read data from an Analog Input (replace the pin number with the pin to which you have connected some analog sensor). We divide that value by 4, because analogRead values are from 0 to 1023, while one byte can store values from 0 to 255. We write this value to EEPROM using the EEPROM.write() function, which takes in two arguments: address to which to write, and the value to be written. We then increment the address and repeat this. If this address reaches the end of EEPROM memory, we go back to the beginning (addr = 0).

void loop() {
   int val = analogRead(A0) / 4;

   EEPROM.write(addr, val);

   addr = addr + 1;
   if (addr == EEPROM.length()) {
      addr = 0;

If you need to write value to a specific address, you can just provide that address as the argument in EEPROM.write().

Updated on 26-Jul-2021 09:40:24