Reading and Writing Data to Arduino’s EEPROM

Arduino is an electronic board that contains a microcontroller, complete with minimum system for its working cycle. Well, mostly Arduino board using microcontroller made by Intel. Typically, microcontroller chip on Arduino board (Uno for example) is using AVR ATmega328. Actually you can replace it with ATmega168 or even ATMega8, others AVR old-family, which have same physical shape and the number of pinouts. The difference is its RAM capacity. ATmega328 has 32 KB of RAM, ATMega168 has 16kb. So, how much ‘the youngest’ ATMega8 RAM comes on …? J

Back to topic, this time we will discuss about reading and writing of data to Arduino’s internal EEPROM. If RAM is volatile (lost when the power supply is turned off), then EEPROM is the opposite, which is non-volatile. ATmega328 microcontroller has 1 Kb of EEPROM (ATmega128 and ATMega8 have 512 Byte). Not bad for a data storage for a little application settings or program constants. Do not compare it to your flashdisk that can reach over 128 Gb? It’s not so much comparison, 1KB vs 128 Gb J

Arduino - EEPROM Read/Write

To read and write data to the EEPROM is very easy because there is already Arduino’s built-in special library. The library name is ‘EEPROM.h’. With this library you can simply write a few lines of code just for handling EEPROM Read/Write because everything is done by the library itself. It’s very helpful to simplify your project. If you ever write a C program to ATMega using CodeVision AVR or AVR Studio, you'll know what I mean he he he.

In the library there are two main functions that is used to read and write data to the EEPROM, namely:

- EEPROM.write (addr, val) to write data to the EEPROM.
Parameter 'addr' is the address of the EEPROM ranging from 0 - 1023, while the parameter 'val' is the value / data in the EEPROM address. Because only 8 bit data width, the data collected can only be 0 - 255. I will explain in more detail about this below.
- EEPROM.read (address) to read / fetch data from EEPROM.
Parameter 'address' is the address of the EEPROM will be read.

Just simple, right? You simply need to 'memorize' two functions above when you’re working with EEPROM. And don't forget to define the EEPROM.h library at the top of your sketch. For more understanding, let us practice two functions above in following sketch. But first, you should create a prototype like following circuit as practice material. You simply provide 1 piece of Arduino Uno, 1 pcs of potentiometers and 3 pieces of jumper cables. Note the image below


Arduino - EEPROM Sketch Handler


#include <EEPROM.h>

int address = 0;
byte value;
int addr = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
int val = analogRead(0) / 4;
EEPROM.write(addr, val);

address=addr;
value = EEPROM.read(address);

Serial.print(address);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();

address = address + 1; 
if (address == 512)
address = 0;

addr = addr + 1;
if (addr == 512)
addr = 0;

delay(500);

}
The essence of the sketch above is read analog voltage at pin A0, writes the results in the EEPROM (divided by 4 first that can be stored in EEPROM), read back from EEPROM and send data to serial port to be visible on the laptop through terminal application client (ex: Serial Monitor window in Arduino IDE or Hyper Terminal application)

Notice to the line 1. This line is to define a library in the sketch (mandatory). Line 11 is intended to divide the analog voltage data with number 4. Why is divided by 4? Because we know that the resolution of the analog voltage reading on the analog pins (A0 - A5) is 10 bits while the EEPROM data width is 8 bit only (please read below for further details about this). Line 12 is writing data to the EEPROM from sensor readings and lines 14 is reading back the data that has been written in the EEPROM and save it to a ‘address' variable. Next line (15 - 18) is to send the EEPROM data readout to a laptop through USB port (emulated as virtual serial port). I hope you already know about how to read and write data to Arduino’s EEPROM sketch as explanations and examples above.
Previous
Next Post »