In this section, we'll learn about storing data in the non-volatile memory known as EEPROM. This allows data to be retained when power is disconnected and accessed later.

Transcript

At this point in the Arduino workshop, you may have noticed the elephant in the room. We've been talking so much about information and data, but so far everything you do on an Arduino is lost as soon as power is disconnected. So how do you go about storing information so that it isn't lost after a reset? We use EEPROM. EEPROM is a form of solid-state, non-volatile memory, which is commonly used in systems to store small amounts of data for microcontrollers. It's like a quick, tiny hard drive embedded on the chip. Most EEPROM units use low-level communication protocols such as I2C, SPI, or OneWire, which makes them easy to integrate into projects. Bear in mind that the typical size of an EEPROM chip is between 512 bytes and 4 kilobytes, so it only allows a small amount of data to be stored at a time.

Storing information in the EEPROM is simple. You specify the address you wish to access, and perform the desired function, be it reading, writing, or erasing. Some chips may come with their own built-in EEPROM, such as the ATMEGA328. It has some built-in EEPROM, whereas others do not and require an external EEPROM chip. However, you can always add more EEPROM to your project using the appropriate communication protocol. The Arduino UNO has 1024 bytes, or 1 kilobyte, of on-board EEPROM to use, and can be easily accessed using the Arduino EEPROM library. Something to keep in mind when using EEPROM is that it has a rated life expectancy. Because of the way that EEPROM units function, you can only write to each address a certain amount of times before it will start to fail and become unreliable. For the ATMEGA328, the EEPROM on board that, it is rated at 100,000 write cycles.

Some units may exceed this, however ATMEL only guarantees 100,000. This might sound like a lot, but if you're constantly updating information for a project that is constantly running, it won't be long before that EEPROM starts to fail, depending on how often you're writing to it. Because the life cycle applies to individual byte-sized addresses, you can counter this using a process known as wear levelling. Wear levelling involves keeping track of the number of times that you have written to an address, and then when it reaches the limit, you change to a fresh address. Unless you're using large numbers of addresses, this approach can greatly extend the life of your EEPROM. Now that we've discussed what EEPROM is, let's take a look at how we can store data using the ATMEGA328 EEPROM on the Arduino UNO. We'll be using a standard push button, as shown in previous sections, to increment a counter, and then storing that in EEPROM.

And we're going to be using a library, which we haven't looked at before, but we're going to take a more in-depth look at it in the next chapter. However, a library is simply a modulate bit of code that we can import into our program, and it gives us a whole bunch of extra functionality and built-in functions that we can use to supplement and perform operations or functions that would be otherwise long and tedious to write out again and again. There's no point reinventing the wheel, right, so we can use a library to do relatively simple and standard applications. So let's take a look at the code. At the very start, we're using hash include, which puts the EEPROM. h header file in the library as part of our code. Then we've got some pin definitions, just our LED pin and button pin. We're using the onboard LED just as some visual feedback. And then we're actually using pretty much most of this is just the toggle button function that we wrote, or the code that we wrote when we were taking a look at if statements.

It's all exactly the same. We've got the last button state, last press, debounce time, only this time we've got this variable called counter, which we'll get to in a moment. First of all, we set up our pin modes, we initialize the serial port to get some feedback. And now we say counter is equal to EEPROM. read. And EEPROM. read takes one argument, the address of the EEPROM chip that you wish to read. So we're reading from address zero, the very first address on board the EEPROM. So in our void setup, counter will equal EEPROM. read, so it will read whatever value was last put in there and update counter so that we can keep our counter incrementing even after it's been powered off. Then after that, and this is the slightly tricky bit, is we use EEPROM. write. Now EEPROM. write, much like digital read and digital write, it takes two arguments.

It needs an address to write to, and it needs a value to write to that address, bearing in mind that the addresses are only a byte, they're only eight bits. So be careful not to exceed that, otherwise you're going to get some random issues. So we're writing to address zero, the value zero, which seems a bit weird because we've just taken counter and said that we want to read that from EEPROM. read. But what this allows us to do is later on we're going to be saying that counter is equal to a value, and then writing that to address zero, but at the moment we have no way to actually reset this counter and reset the value that's in address zero. So by putting that line in there, but before we redefine counter, it means that if we reset our board or disconnect power, it's going to set counter to whatever the EEPROM. read is, and then it's going to erase, or set to zero, the first bit of the EEPROM address zero.

So when we reset again, when it powers back up, counter will be read from the EEPROM, which is zero, and counter therefore will equal zero, and it will reset. However after a single reset, that has no effect because of the logic how the code is written. So in our loop, as you know with the if statements, we'll gloss over this bit a little bit as we've covered it in a previous section on if statements. We're checking to see the button state, then we're performing some debouncing, updating our last button state, and the important parts here is the counter that we're incrementing by one every time it's pressed, so every time we detect a press on the button, increment counter by one. Then we're writing to address zero of our EEPROM, and we're writing the value of counter. So it's going to increment each time, and each time we press it, it'll write that value to the EEPROM to save it, to store it there so that if power is disconnected, we can access that value again and keep going from where we left off.

Then we write to the LED just as some visual recognition, and that's that, and then we're going to print the value of counter to the serial monitor. And then all we do if it is a release is to turn the LED off for visual feedback. So let's take a look. We're going to plug this into our Arduino and take a look. I don't know if you guys can see that, very good. So now we need to upload the code to our board. Make sure you've got the correct COM port selected like I did not as given by the error, and then it'll upload to your board. And the reason we're using the serial monitor is whilst we can write to the EEPROM all well and good, if you've got a more complex piece of code where it's hard to see exactly what's going where, often you can lose track and you might not actually be writing correctly to the EEPROM address. So by printing it out to a serial monitor, we can physically see what's been written to the address.

So when we upload this sketch and connect power to the board, we can open up the serial monitor and see that when we push the button, the counter will increment. So it's at 24, fantastic, 25, 26, 27, 28, which means that I've actually tested this out on the board beforehand and incremented a few times to make sure that everything's running well and it's remembered that. So if we hit reset on our board, once it's powered up again, we can see that we'll continue where it left off. We've disconnected power, that's what reset does, but it hasn't forgotten that variable because it's been stored in the EEPROM and upon powering up again, it reads the value from the EEPROM and updates counter. But as I said, if we reset it and then reset it again, we can see that it'll clear counter because of that logic we built into the setup and it'll increment again from zero, one, two, three, four, and so on and so forth.

So it's really easy to use the EEPROM. It follows a similar format as the digital read or analog read, digital write and analog write where you have, if you're reading, you simply read from a place. In this case, it is the address and the address will be the number of addresses available on the EEPROM chip. So if you have 1,024 possible addresses, then you're going to need to nominate those between zero and 1,023 or a 10 bit address. So then we can take a look and we can just see that EEPROM write is really, really simple. We're writing to the address and we're giving it a value to write, much the same as digital write or analog write. So that wraps up EEPROM and that concludes this chapter where we've taken a look at using arrays, using operators, bitwise logic and EEPROM and it's a shorter chapter with fewer sections in it, but there's a whole lot to cover and understanding all of these concepts can take a few different projects and getting hands on with the Arduino to use some of these concepts and functions. So I encourage you to build some of your own projects using what we've learned here and we'll see you again at chapter five.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.