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
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!