Tutorial: Arduino EEPROM

Introduction

Whatever you are doing with the Arduino is lost the moment you pressed the RESET button or you removed the power. The work around that is to implement some data logging to an SD card or through the EEPROM.

So what is EEPROM?
EEPROM stands for Electrically Erasable Programmable Read-Only Memory. Each EEPROM address can save 1 byte of data. Arduino Uno has 1024 bytes of addressable positions while Arduino Mega has 4096 bytes of addressable positions.

EEPROM is effective solution for saving data but do not abuse the use of it. Why? The manufacturer specified an approximate of 100,000 write cycles for each address position before it gets degrading and giving weird results. Read access is possible as many times as you like without compromising its life expectancy.

Video Demonstration

Circuit Diagram

Source Code

1. Without EEPROM

 1
 2#define YLW_LED_PIN 10
 3#define RED_LED_PIN 9
 4#define YLWBUTTON_PIN  8
 5#define REDBUTTON_PIN  7
 6
 7bool YLWledState = LOW;
 8bool YLWbuttonState;
 9bool YLWlastButtonState = LOW;
10unsigned long YLWlastDebounceTime = 0;
11unsigned long YLWdebounceDelay = 50;
12
13bool REDledState = LOW;
14bool REDbuttonState;
15bool REDlastButtonState = LOW;
16unsigned long REDlastDebounceTime = 0;
17unsigned long REDdebounceDelay = 50;
18
19void setup() {
20  pinMode(YLWBUTTON_PIN, INPUT_PULLUP);
21  pinMode(REDBUTTON_PIN, INPUT_PULLUP);
22  pinMode(YLW_LED_PIN, OUTPUT);
23  pinMode(RED_LED_PIN, OUTPUT);
24}
25
26void loop() {
27  // Debouncing of the input button
28  int YLWreading = digitalRead(YLWBUTTON_PIN);
29  int REDreading = digitalRead(REDBUTTON_PIN);
30  if (YLWreading != YLWlastButtonState) {
31    YLWlastDebounceTime = millis();
32  }
33  if (REDreading != REDlastButtonState) {
34    REDlastDebounceTime = millis();
35  }
36  if ((millis() - YLWlastDebounceTime) > YLWdebounceDelay) {
37    if (YLWreading != YLWbuttonState) {
38      YLWbuttonState = YLWreading;
39      if (YLWbuttonState == LOW) {
40        YLWledState = !YLWledState;
41        digitalWrite(YLW_LED_PIN, YLWledState);
42      }
43    }
44  }
45  if ((millis() - REDlastDebounceTime) > REDdebounceDelay) {
46    if (REDreading != REDbuttonState) {
47      REDbuttonState = REDreading;
48      if (REDbuttonState == LOW) {
49        REDledState = !REDledState;
50        digitalWrite(RED_LED_PIN, REDledState);
51      }
52    }
53  }
54
55  YLWlastButtonState = YLWreading;
56  REDlastButtonState = REDreading;
57}

2. With EEPROM

 1
 2#include "EEPROM.h"
 3
 4#define YLW_LED_PIN 10
 5#define RED_LED_PIN 9
 6#define YLWBUTTON_PIN  8
 7#define REDBUTTON_PIN  7
 8
 9bool YLWledState = LOW;
10bool YLWbuttonState;
11bool YLWlastButtonState = LOW;
12unsigned long YLWlastDebounceTime = 0;
13unsigned long YLWdebounceDelay = 50;
14
15bool REDledState = LOW;
16bool REDbuttonState;
17bool REDlastButtonState = LOW;
18unsigned long REDlastDebounceTime = 0;
19unsigned long REDdebounceDelay = 50;
20
21void setup() {
22  pinMode(YLWBUTTON_PIN, INPUT_PULLUP);
23  pinMode(REDBUTTON_PIN, INPUT_PULLUP);
24  pinMode(YLW_LED_PIN, OUTPUT);
25  pinMode(RED_LED_PIN, OUTPUT);
26  getLEDState();
27}
28
29void loop() {
30  // Debouncing of the input button
31  int YLWreading = digitalRead(YLWBUTTON_PIN);
32  int REDreading = digitalRead(REDBUTTON_PIN);
33  if (YLWreading != YLWlastButtonState) {
34    YLWlastDebounceTime = millis();
35  }
36  if (REDreading != REDlastButtonState) {
37    REDlastDebounceTime = millis();
38  }
39  if ((millis() - YLWlastDebounceTime) > YLWdebounceDelay) {
40    if (YLWreading != YLWbuttonState) {
41      YLWbuttonState = YLWreading;
42      if (YLWbuttonState == LOW) {
43        YLWledState = !YLWledState;
44        EEPROM.update(0, YLWledState);
45        digitalWrite(YLW_LED_PIN, YLWledState);
46      }
47    }
48  }
49
50  if ((millis() - REDlastDebounceTime) > REDdebounceDelay) {
51    if (REDreading != REDbuttonState) {
52      REDbuttonState = REDreading;
53      if (REDbuttonState == LOW) {
54        REDledState = !REDledState;
55        EEPROM.update(1, REDledState);
56        digitalWrite(RED_LED_PIN, REDledState);
57      }
58    }
59  }
60
61  YLWlastButtonState = YLWreading;
62  REDlastButtonState = REDreading;
63}
64
65void getLEDState() {
66  YLWledState = EEPROM.read(0);
67  digitalWrite(YLW_LED_PIN, YLWledState);
68  REDledState = EEPROM.read(1);
69  digitalWrite(RED_LED_PIN, REDledState);
70}

Call To Action

Hope you find this article useful. Please leave your comments in the comment box. Thank you and have a good day.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.