Using EEPROM with Arduino - Internal & External
Today we will learn how to use EEPROM with an Arduino to add nonvolatile memory to our projects.
Article with code: https://dbot.ws/eeprom
Nonvolatile memory refers to memory that retains its data even after being powered down. While there are many ways to accomplish this one of the simplest is by using EEPROM - Electrically Erasable Read-Only Memory.
The Arduino already has some EEPROM built-in, not too much, but for many applications it will be more than enough. You can use it to retain settings in your project. If you need more nonvolatile memory you can add an external EEPROM chip.
Today I will show you how to do both.
The Arduino's internal EEPROM is very easy to work with, thanks to the EEPROM library included within the Arduino IDE. The library comes with several small example sketches, and we’ll see how to use those sketches in our first demo.
We will also work with the AT24LC256, also called just the 24LC256 or 24C256. It’s an EEPROM chip that can store 256 Kilobits (or 32 KB) of data, and it uses the I2C bus for easy interfacing.
We will use the external EEPROM to record servo motor movements and then play them back. This would be perfect for an automated display. Of course, you can build upon the sample code to add EEPROM to almost any project.
Here is what we will cover today:
00:00 - Introduction
02:46 - Understanding EEPROMs
07:16 - Using Internal EEPROM
16:02 - Using External EEPROM
I’ll also cover some of the limitations of using EEPROMs, they are not entirely perfect. The biggest limitation is that they have a limited number of write operations, I’ll show you how you can reduce writes to the internal EEPROM by using the ”update” method.
"Never trust a computer you can’t throw out a window." — Steve Wozniak
As I work on projects I often come back to your videos to refresh my learning.
Today I refreshed back to this video on eeprom.
One question: How do you decide on the address to use? I am guessing the logical idea would be start at 0 and progress upward. Just curious if this is the rule or could you skip around inside the space available? First byte at 0, another unrelated byte at 20 for example?
Thanks in advance!
You can read and write anywhere you like, you just need to set the address to where you want to start. Just remember that you'll need to access the data in the same place that you put it.
Experience is what you get when you don't get what you want.
In addition to the useful answer @will has provided, it is worth bearing in mind that each bit of most 'EE' type memories get slightly 'worn' each time they are reprogrammed. The effect of this 'wear' is that their data retention time of that address tends to decrease, and ultimately may become useless. The rate at which this wear degrades the memory depends upon the individual device design, so analysing a reliable data sheet is also a requirement.
Imagine you have a program that records a value every time it it receives a pulse on a pin. If the value is always recorded in the same location, then this location will be progressively worn or degraded.
Hence, if the number of times this value is expected to be recorded in the lifetime of the unit, is higher than the expected rewrite count specification , then your system will fail prematurely, if it always writes to the same place. For some memory types, the rewrite count specification might only be (say) 100 times, so it could quickly wear out that location.
Hence, you need a smarter plan that writes to a number of different locations, spreading the wear. Obviously, this also means that your system will need to keep track of the where the latest value was written to, both to be able to retrieve the latest value, and as a reference point to figure out where the next write should be located.
Hope this helps. Best wishes, Dave
Two projects I have in mind is to read inputs, calculate a address and store the address in eeprom, allowing the board to have address selected instead of being set in software. The other idea is a servo controller, use a pot to adjust the two positions needed and store that in eeprom.
As for lifetime cycles of writes or updates, I will do more when testing than likely in the lifetime use. Once the servo limits are set and stored they would only be changed if the motor is replaced.
Both projects will use a byte for each entry so space should not be a issue even on a attiny.