arduino How to store variables in EEPROM and use them for permanent storage Store a variable in EEPROM and then retrieve it and print to screen

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

First, add a reference to <EEPROM.h> at the start of your sketch:

#include <EEPROM.h>

Then your other code:

// Stores value in a particular address in EEPROM. There are almost 512 addresses present.

    // Store value 24 to Address 0 in EEPROM
    int addr = 0;
    int val = 24;
    EEPROM.write(addr, val);     // Writes 24 to address 0
    
    // ---------
    // Retrieves value from a particular address in EEPROM
    // Retrieve value from address 0 in EEPROM
    int retrievedVal = EEPROM.read(0);    // Retrieves value stored in 0 address in
                                          // EEPROM

    // *[NOTE: put Serial.begin(9600); at void setup()]*
    Serial.println(retrievedVal);        // Prints value stored in EEPROM Address 0 to 
                                         // Serial (screen)


Got any arduino Question?