Let’s go through an example that stores values in EEPROM and then reads them back. First, we’ll create a sketch that stores the values in the EEPROM memory area. Follow these steps:
1. Open the Arduino IDE, and select Sketch > Import Library > EEPROM.
2. Enter this code into the editor window under the #include <EEPROM.h> directive that was
added by the library import:
Click here to view code image
#include <EEPROM.h> void setup() {
Serial.begin(9600);
Serial.println("Storing data in EEPROM..."); int test[] = {10, 20, 30, 40, 50};
int i;
for(i = 0; i < 5; i++) {
EEPROM.write(i, test[i]); }
Serial.println("Data written to EEPROM"); }
void loop() { }
3. Save the sketch code as sketch1203.
4. Click the Upload icon to verify, compile, and upload the sketch code to your Arduino unit. 5. Open the serial monitor to run the sketch code and view the output.
The sketch1203 code uses the write function from the EEPROM library to store the array values in the first five spaces in EEPROM memory. You should see the output shown in Figure 12.4 in the serial monitor.
FIGURE 12.4 Storing integer values in EEPROM memory.
The next set of steps creates a second sketch to read the values that you stored in the EEPROM memory:
1. Open the Arduino IDE and select Sketch > Import Library > EEPROM.
2. Enter this code into the editor window under the #include directive that was added by the
library import:
Click here to view code image
#include <EEPROM.h> void setup() {
Serial.begin(9600); int test2[5];
int i;
Serial.println("Reading data from EEPROM..."); for(i = 0; i < 5; i++) {
Serial.println(test2[i]); }
}
void loop() { }
3. Save the sketch code as sketch1204.
4. Click the Upload icon to verify, compile, and upload the sketch code to your Arduino unit. 5. Open the serial monitor to run the sketch and view the output.
6. Disconnect your Arduino unit so that it loses power; then reconnect it back to your
workstation.
7. Open the serial monitor again to run the sketch and view the output.
The output from the sketch1204 code should display the five data values that you stored from the sketch1203 code (as shown in Figure 12.5), even after removing power to the Arduino.
FIGURE 12.5 Retrieving data from the EEPROM memory area. By The Way: Storing More Complicated Data Structures
Because the EEPROM read and write functions can retrieve only 1 byte of data, they’re somewhat limited in what they can store (only values from 0 to 255). However, some enterprising Arduino users have created a library of functions that allow you to easily store other data types, including character arrays and data structures, using special functions. This library is called the EEPROM Extended library, or EEPROMex for short. Unfortunately, it’s not included as part of the standard Arduino IDE package, so you have to download and install it separately. You’ll learn about how to do that in the next hour.
Summary
This hour focused on alternative ways to store data in your Arduino sketches. First, it showed just how the Arduino uses the SRAM memory area to store the variables that you declare in your sketches. After that, it showed how you can use the heap data area in SRAM to create and use dynamic
variables that can change in size as your sketch runs. Next, it walked through how to store static
values in flash memory to help free up space in SRAM. You must use special data types and functions to store data in the flash memory area with the program code. Finally, the hour discussed how to use the EEPROM memory that is built in to the Arduino CPU itself. You can use the standard EEPROM library to read and write individual bytes of data in EEPROM. This data remains intact even after the power is removed from the Arduino unit.
The next hour covers how to work with the different libraries available for the Arduino and how to create your own library of functions that you can share with others.
Workshop
Quiz
1. Where can you create dynamic variables that can change in size as you run your Arduino
sketches?
A. The SRAM stack data area B. The flash memory area C. The SRAM heap data area D. The EEPROM memory area
2. Data values stored in flash are lost when power is removed from the Arduino. True or false?
3. How do you retrieve a value stored in the EEPROM memory area?
Answers
1. C. You can create dynamic variables using the malloc or calloc functions in the SRAM heap
data area.
2. False. The flash memory area retains the program and data information stored there after power
is removed. When power is restored to the Arduino, the program runs, and the data values you stored in flash will also still be there.
3. You must use the EEPROM.read function available in the EEPROM library for the Arduino.
You cannot use a standard C function to read or write data to the EEPROM memory area.
Q&A
Q. If the EEPROM memory area on my Arduino already contains data stored in it, can I read the data?
A. Yes, but you might not be able to make any sense out of it! You can read the data stored in the
EEPROM 1 byte at a time, but you’d have to determine if the data stored was a character,
number, or data structure. That could be close to impossible to figure out if you weren’t the one who stored the data.
Q. Should I erase the EEPROM memory area by placing 0s (zeros) in all the memory locations when I’m done using it?
A. It depends on the sensitivity of your data. Remember, the more you write the EEPROM
memory, the fewer times you have before it becomes unreliable. If there isn’t a requirement to erase data from the EEPROM, I wouldn’t bother doing it. You can overwrite old data with new data at any time.
Hour 13. Using Libraries
What You’ll Learn in This Hour:
What an Arduino library is
How to use standard Arduino libraries How to use contributed libraries
Creating your own Arduino libraries
As you start writing larger Arduino sketches, you may find yourself reusing the same pieces of code for different sketches. In Hour 10, “Creating Functions,” you saw how to create functions to help cut down on the amount of code you had to write for a single sketch, but if you wanted to share that code between multiple sketches, you’d still have to copy and paste it into each sketch. This hour shows you how to reuse functions without having to copy code by using libraries.