Introduction: Arduino EEPROM Settings Initialization
Every Arduino has a small built in memory called EEPROM. You can use this to store settings for your project where the chosen values will be kept between power cycles and they will be there next time you power up the Arduino. I have a cool trick that will teach you how you can init a set of default values on your first run so stick around to find out how.
Step 1: What Is an EEPROM?
An EEPROM is a tiny memory storage, whose values are kept even while the Arduino board is power off. This acts like a tiny hard drive so you can store you parameters for the next time you power up the device. Depending on the type of Arduino board, you will have a different amount of storage available on each, so for example the Uno has 1024 bytes, the Mega has 4096 bytes and the LilyPad has 512 bytes.
It is important to note that all EEPROMs have a limited number of write cycles. Atmel specifies a life expectancy of around 100 000 write/erase cycles for the EEPROM on the Arduino. This may sound like a lot of writes, but it can be easy to reach this limit if you are reading and writing in a loop. Once a location has been written and erased too many times it can start to become unreliable. It may not return the correct data, or return the value from a neighboring bit.
Step 2: Import the Library
To use this memory, we first include the provided library by the Arduino. The library provides two methods: read and write for the according actions. The read function accepts the address that we want to read from, while the write function accepts both the address and the value we want to write.
In our example, the goal is to have an array of settings ready on each start of the Arduino, so we start by defining the array that we gonna use for the storage and defining the addresses for each of the settings we want to store. In a chip where we have 1024 bytes available, the address locations will be from 0 to 1023.
Step 3: Set the Initialization Flag
The trick for the initial setting of default values for the settings is to use one of the addresses as an indicator whether or not the settings have been initialized. I’ve used the last address location for this as it is often not used for anything else. The loadSettings function will first check this location if the value stored there is a “T” character and if not, it will go setting by setting, writing the initial values for each of them. Once done, it will now set the value of the location where we keep track of initialized settings to the “T” character and next time when we power on the Arduino, we will no longer init the values, but instead read the saved data into our array.
Step 4: Updating Settings
For updating the values we can either use the write function as we had it on the initialization, but a better way is to use the provided update function. What this function does is that it first checks if the value we are trying to save is the same one already in the EEPROM and if it is then it does not update. By doing so, it tries to minimize the number of write operations in order to extend the life of the EEPROM.
Step 5: Enjoy!
I hope that this Instructable was helpful to you and that you managed to learn something. The source code is available on my GitHub page and the link is below. If you have any suggestions please leave them down in the comments and don’t forget to subscribe to my YouTube channel for more similar videos.