Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
galoebn
Published © GPL3+

Hold data after turning off the Arduino (EEPROM)

In this project you learn how to store data on the Arduino so that it is not lost after the Arduino is turned off.

BeginnerProtip1,961
Hold data after turning off the Arduino (EEPROM)

Things used in this project

Story

Read more

Schematics

Schematics

Code

Code EEPROM

Arduino
#include <EEPROM.h>

void setup() {
  pinMode(10, INPUT_PULLUP); //because of the pullup resistor we dont need an external resistor
  pinMode(LED_BUILTIN, OUTPUT);
}



void loop() {
  bool button_pressed = digitalRead(10);

  if(!button_pressed && EEPROM.read(0) == 0){
    EEPROM.write(0, 1); //EEPROM.write(address, value) ; the value should be between 0 and 255
  }
  
  else if(!button_pressed){
    EEPROM.update(0, 0);  //the update function writes only to the EEPROM if the new value 
                          //is not the same as the old one. In some cases this is very important
                          //to save cylces on the EEPROM.
  }

  delay(100);
  digitalWrite(LED_BUILTIN, EEPROM.read(0));
}

Credits

galoebn
9 projects • 4 followers

Comments