Every microcontroller has three types of memory: the Flash, the SRAM, and the EEPROM. The Atmega of Arduino is not an exception.
The Flash memory is where we memorize our sketch program.
SRAM is needed when we create and manipulate the variables at runtime.
EEPROM is the part of memory that doesn’t lose data once our board’s power is unplugged.
The flash memory and the EEPROM are non-volatile.
The Arduino Uno Board has a memory as follows:
0 1 2 | Flash 32k bytes (of which .5k is used for the bootloader) SRAM 2k bytes EEPROM 1k byte |
The Arduino Mega2560 Board has a memory as follows:
0 1 2 | Flash 256k bytes (of which 8k is used for the bootloader) SRAM 8k bytes EEPROM 4k byte |
Today I’m gonna show you how to use the EEPROM memory (of Arduino Uno Board but it’s the same as the other) to store your configurations and preferences into it.
You have the possibility to use these methods:
-read();
-write();
-update();
-clear();
1) read (): reads a byte from the EEPROM. You can read it in a range between 0 and 255
How do you use it in your code?
The address can be between 0 and 255
0 | EEPROM.read(address); |
2) write(): writes a byte to the EEPROM. You can write it in a range between 0 and 255.
How to use it in your code?
0 | EEPROM.write(address,Value); |
3) update(): overwrites a byte to the EEPROM only if the value is updated.
0 | Â EEPROM.update(address, value); |
4) clear() : Sets all the bytes to 0
0 1 2 | Â Â for (int i = 0 ; i < EEPROM.length() ; i++) { Â Â EEPROM.write(i, 0); Â } |
For the other methods see the Arduino Reference Guide.
In this example, we will see how to memorize an initial servos angle into the EEPROM through the use of three switches, and how to recall their values once we will re-power our Arduino board.
The rest of our project is very similar to the third tutorial on “how to drive a servo motor”.
This is the code you have to deploy into the microcontroller.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | //EEPROM EXAMPLE BY GIUSEPPE RANDAZZO. #include <Servo.h> #include <EEPROM.h> Servo ServoOne,ServoTwo,ServoThree; int addr =0; int InitialServoAngle =0; int ServoSetted =0; int ValueReadFromEEPROM =0; int ActualValueServoOne =0; int ActualValueServoTwo =0; int ActualValueServoThree =0; void setup() { pinMode(2, INPUT); pinMode(3, INPUT); pinMode(4, INPUT); pinMode(5, INPUT); pinMode(6, INPUT); pinMode(7, INPUT); ServoOne.attach(9); ServoTwo.attach(10); ServoThree.attach(11); Serial.begin(9600); } void loop() { if (digitalRead(2)==0){ EEPROM.write(addr,0); } if (digitalRead(3)==0){ EEPROM.write(addr,70); } if (digitalRead(4)==0){ EEPROM.write(addr,140); } if (ServoSetted==0){ ValueReadFromEEPROM=EEPROM.read(addr); ServoOne.write(ValueReadFromEEPROM); ServoTwo.write(ValueReadFromEEPROM); ServoThree.write(ValueReadFromEEPROM); ServoSetted=1; } if (digitalRead(5)==0){ if (ServoOne.read()==ValueReadFromEEPROM){ for(ActualValueServoOne=ValueReadFromEEPROM; ActualValueServoOne<=140; ActualValueServoOne++){ Serial.println(ActualValueServoOne,DEC); ServoOne.write(ActualValueServoOne); delay(100); } } else{ for(ActualValueServoOne=140; ActualValueServoOne>=ValueReadFromEEPROM; ActualValueServoOne--){ Serial.println(ActualValueServoOne,DEC); ServoOne.write(ActualValueServoOne); delay(100); } } } if (digitalRead(6)==0){ if (ServoTwo.read()==ValueReadFromEEPROM){ for(ActualValueServoTwo=ValueReadFromEEPROM; ActualValueServoTwo<=140; ActualValueServoTwo++){ Serial.println(ActualValueServoTwo,DEC); ServoTwo.write(ActualValueServoTwo); delay(100); } } else{ for(ActualValueServoTwo=140; ActualValueServoTwo>=ValueReadFromEEPROM; ActualValueServoTwo--){ Serial.println(ActualValueServoTwo,DEC); ServoTwo.write(ActualValueServoTwo); delay(100); } } } if (digitalRead(7)==0){ if (ServoThree.read()==ValueReadFromEEPROM){ for(ActualValueServoThree=ValueReadFromEEPROM; ActualValueServoThree<=140; ActualValueServoThree++){ Serial.println(ActualValueServoThree,DEC); ServoThree.write(ActualValueServoThree); delay(100); } } else{ for(ActualValueServoThree=140; ActualValueServoThree>=ValueReadFromEEPROM; ActualValueServoThree--){ Serial.println(ActualValueServoThree,DEC); ServoThree.write(ActualValueServoThree); delay(100); } } } } |
The Electrical Scheme is defined as follows:

Fritzing electrical scheme