Welcome back to another episode of the Arduino 101 series. Remember that in
this video series we are going topic by topic into more depth of the Arduino programming.
Arduino coding can be quite easy but when you get into timers, registers, internal hardware and C
programming, that could get a bit difficult and that’s what we are learning in this video series.
Today topic is the EEPROM memory. What is it, how to use it, how to write large
values on to multiple memory addresses and so on… So, guys, let’s get started.
What’s up my friends welcome back. Here is the Arduino UNO and remember that this board is
using the ATmega328 microcontroller. Inside a microcontroller, at a microscopic level
we have sometimes a CPU, maybe the ADC converters or DAC converters, we have oscillators, serial
buses and so on, but we also have memory. Usually, we have 3 types of memory. SRAM, Flash and EEPROM.
The Arduino UNO and the NANO, which are using the ATmega328, have 1024 bytes of EEPROM memory.
But the Arduino MEGA for example has 4096 bytes. Different microcontrollers could have a different
amount of memory. But what is this EEPROM memory anyway? It stands for Electronically Erasable
Programable Read-Only Memory. Yeah, that’s a long name. So why do we have to mention that
it’s electrically erasable since any memory can be erased right? Well, that’s because we first
had the EPROM which was kind of the same but you have to put the chip below UV light in order to
erase it and then put it back on your board. But the EEPROM can be erased using electrical signals.
To store data this memory is using a lot of MOSFET transistors. Each transistor can store a 1 or a 0,
which is basic binary. Actually, for each bit, the EEPROM needs two transistors.
8 bits create a byte so for 1024 bytes we need more or less 16 384 transistors
which is not that much if you think about it. This is a cross section of a EEPROM MOSFET.
This transistor also has a floating gate so this is how we store a 1 or a 0. To program it we apply
positive at the gate so the charge will go upwards filling the floating gate at the same time.
Since we have insulators, the charge will stay here and that represents a 1.
To erase it we place 0V at the gate so the charge will go away, and no charge represents a 0.
Usually, EEPROM memories have I2C or SPI communication in order to get
the data in or out. But that’s for external memories. For example, this RTC module here,
also has this IC and this is an external EEPROM memory, the AT 24 C32.
But the Arduino microcontroller already has such a memory inside it and we can use it very easy.
We could imagine this memory as a string of data where each cell is for 1 byte or 8 bits.
To write something, we have to send the address of that cell and the value that we want to write.
To read something we only have tos end the address and it will give us the value. The addresses will
go from 0 to 1023 for the Arduino UNO. As you can see, we can only write 1 byte at a time with
binary values from this to this. So basically we can store a number from 0 to 255 on each cell.The
datasheet of the microcontroller tells us that the lifespan of these memory cells
could be of around 100.000 read writes, and it would take around 3.3ms to write data to a cell.
I know that sounds fast but this is actually a very slow memory compared to others.
Also, by default, the cell has a data of a full byte which is 255 in decimal.
Ok, now connect your Arduino to your PC and let’s see how to use the EEPROM memory with the Arduino.
For that we need to import the EEPROM library using this line in the code.
This library has these functions. Let’s start with the EEPROM dot read. Inside the parenthesis
we have to mention the address of the memory cell we want to read from 0 to 1023. For this example,
I make these lines and I equal this brightness variable to EEPROM dot read of the address 0.
I print that on the monitor. I connect my Arduino and upload the code.
I run the monitor and as you can see I read 255 because that’s the default value of a new memory.
Now let’s use the EEPROM dot write function to change the value on position 0.
This time, between the parenthesis we must add first the address and then the value. Remember,
very important, the value must be between 0 and 255 because each address represents a cell of
8 bits so we can’t write higher values than that. If you try to write for example the value 300, it
would have an overflow and it would actually write 300 – 255, so it will write 45 instead of 300.
Anyway I type this line of code and I place it inside the void Setup
In order to only run it once. EEPROM dot write, 0 for the address and let’s say 25 for the value.
Then in the void loop I use again the EEPROM dot read to get the value on position 0.
Upload the code and I run the monitor and as you can see, this time we read 25 instead of 255
so we have successfully write something to the memory and also read it.
But this is the interesting part. I disconnect the Arduino from the USB so everything is powered off.
I connect it back and run the monitor again and the value is still there.
That’s why we use this memory, in order to store values that we need every time
the Arduino is unplugged. We can store a value basically forever.
For example, on my portable soldering iron project, the user was able to change
the default temperature. You must store that on to the EEPROM memory, otherwise
each time the user would turn off the iron, the value will go back to its initial state.
For my homemade ESC I was also storing the configuration on the EEPROM memory
in order to use it any time. These are just a few examples where you could use the EEPROM memory.
You can also use the other functions as you can see here.
For example, the EEPROM dot clear will empty the entire memory. With EERPOM dot update,
we update a value only if that value is different and like that we increase the memory life.
Ok, now we know the basics on how to write and read from the Arduino
EEPROM. But is it possible to write an integer or a float value to this memory.
Those variables would occupy 2 or 4 bytes but we can only write 1 at a time.
For that let’s use these functions, EEPROM dot put or get. We start with EEPROM dot get.
Inside the parentheses we must place the address where the data would start and the second part
is the data variable where we want to store the received value. So, if this variable is an integer
type, the function will automatically know to read 2 bytes instead of just 1 byte. If the variable is
defined as float, it will read 4 bytes and so on so we don’t have to care about the data length.
So I create an integer variable and name it brightness for example.
Then lets imagine that previously, using the EEPROM dot put function we have written the
value 821 starting on address 0. So now I use the function EEPROM dot get and for the address
I add a 0 and for the data I write the brightness variable I’ve created before. The function will
read 2 bytes starting from address 0 and store the received data inside the brightness variable.
If I run the program as you can see, the brightness data
so we have successfully read the data which in this case is an integer and occupies 2 bytes.
In the same way, to write data of more than 1 byte, we use EEPROM dot put.
Inside the parenthesis we first write the starting address and then the value to write. For example,
I create a float value and name it sensor_read and equal it to 70000.
Now I write that value on address 0 using the EEPROM dot put. Now once again I use
the EEPROM dot get function and add the address 0 and a float type variable named brightness.
I run the code and now I read 70000 which is a float type and occupies 4 bytes.
The same will work in case of a decimal point variable such as this one for example.
I can also write the value 56.7 if I want. The function will automatically
detect that for me because it knows that I’m working with float variables.
As you can see this time I read the float number. So it is very important to correctly define the
variable before we run the function. We can also write a string of characters. For example,
I create this string variable and equal it to this text. I write it on address 22 for example.
Then using this code, I can read that text from the memory. For more examples,
in Arduino IDE go to file, examples, EEPROM and read all these examples codes.
You will also have a tutorial on Electronoobs.com with text, code bits and more explanation.
Also, if you don’t want to use the EERPOM dot put or get functions,
you can create your own functions, something like this.
I found this example online and the functions are called EEPROM write anything and read anything.
Basically, it will first measure the length of the data you want to write or read and then It
uses the normal EEPROM dot read or write function to get or write a longer data than just 1 byte.
Get the code from below and test it out. Also, remember that you can always buy a EEPROM
memory IC in order to have more external memory since 1024 bytes is not that much.
But this memory is very useful and I use it in pretty much all my projects.
I hope that you now know what is the EEPROM memory and how to use it with Arduino. If you have
learned something new, give me a like or comment below. Thanks again and see you later guys.