EEPROM
So what is EEPROM? EEPROM is like a tiny hard drive. It stores memory, values on the Arduino board. It allows you to save values, values that can be retrieved from a later time, even after powering down the device. The Arduino has 512 bytes of EEPROM. So follow along as you learn how to use EEPROM.
Before actually starting to write the setup code, you need to do this:
#include <EEPROM.h>
Since the EEPROM is an external library, we need to include the library files necessary to run the EEPROM-related code. Let's start with a little example. I want to store a value of 5 in the EEPROM.
EEPROM.write(1,5);
So what is this piece of code? This is a code for writing data into the EEPROM: EEPROM.write. As you guessed, the 5 in the parenthesis in the example stands for the value you want to store. So where did the 1 come from? Here, the 1 stands for the address for the EEPROM. The address is like a mailbox. For EEPROM, there are 513, addresses, 513 mailboxes, 513 different values that can be stored. EEPROM addresses range from 0 to 512. Here is the syntax for writing EEPROM data.
EEPROM.write(address,value);
So how about reading EEPROM data? Say from a previous code, I stored a value of 12 in address 1, and I want to retrieve it and give that value to a variable, x. Here's what I'll do.
x=EEPROM.read(1);
EEPROM.read, the command to read whatever is in the address. The x= is pretty much assigning the value from the EEPROM.read to the variable x. The 1 in the example above stands for the address you want to read EEPROM data from. Here is the syntax:
EEPROM.read(address);
So what do I do if I want to clear EEPROM data? It's pretty simple: just do EEPROM.write(address, 0); Why? 0 is the default for EEPROM data when it has no value. This is just only for one address. In order to clear EEPROM data from the whole thing, you need to do EEPROM.write like 513 times. However, with the help of control structures, we can do a simple code:
for (int i=0; i < 512; i++)
{
EEPROM.write(i,0);
}
Didn't learn about control structures yet? Click here.
Remember to put #include <EEPROM.h> before actually writing any code that has to do with EEPROM.
So why do we need EEPROM? EEPROM can be used to store data. For robots that have personality, you can use EEPROM to read facial features, store personalities, etc. If you are making a game device out of an Arduino, you can use EEPROM to store how much points you have. Possibilities are endless with EEPROM.
Before actually starting to write the setup code, you need to do this:
#include <EEPROM.h>
Since the EEPROM is an external library, we need to include the library files necessary to run the EEPROM-related code. Let's start with a little example. I want to store a value of 5 in the EEPROM.
EEPROM.write(1,5);
So what is this piece of code? This is a code for writing data into the EEPROM: EEPROM.write. As you guessed, the 5 in the parenthesis in the example stands for the value you want to store. So where did the 1 come from? Here, the 1 stands for the address for the EEPROM. The address is like a mailbox. For EEPROM, there are 513, addresses, 513 mailboxes, 513 different values that can be stored. EEPROM addresses range from 0 to 512. Here is the syntax for writing EEPROM data.
EEPROM.write(address,value);
So how about reading EEPROM data? Say from a previous code, I stored a value of 12 in address 1, and I want to retrieve it and give that value to a variable, x. Here's what I'll do.
x=EEPROM.read(1);
EEPROM.read, the command to read whatever is in the address. The x= is pretty much assigning the value from the EEPROM.read to the variable x. The 1 in the example above stands for the address you want to read EEPROM data from. Here is the syntax:
EEPROM.read(address);
So what do I do if I want to clear EEPROM data? It's pretty simple: just do EEPROM.write(address, 0); Why? 0 is the default for EEPROM data when it has no value. This is just only for one address. In order to clear EEPROM data from the whole thing, you need to do EEPROM.write like 513 times. However, with the help of control structures, we can do a simple code:
for (int i=0; i < 512; i++)
{
EEPROM.write(i,0);
}
Didn't learn about control structures yet? Click here.
Remember to put #include <EEPROM.h> before actually writing any code that has to do with EEPROM.
So why do we need EEPROM? EEPROM can be used to store data. For robots that have personality, you can use EEPROM to read facial features, store personalities, etc. If you are making a game device out of an Arduino, you can use EEPROM to store how much points you have. Possibilities are endless with EEPROM.