100% found this document useful (1 vote)
92 views

Basic Operations With EEPROM Using Arduino and ConnDuino - ConnDuino Projects

This document discusses using EEPROM memory with an Arduino board to store data that needs to persist when power is removed. It describes EEPROM's read-only nature but ability to rewrite each location a limited number of times. A 24LC256 EEPROM chip is used, which has a 32KB storage capacity and over 1 million rewrite cycles. The pinout of the 24LC256 is shown and wiring to the Arduino board via the I2C interface is explained.

Uploaded by

brahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
92 views

Basic Operations With EEPROM Using Arduino and ConnDuino - ConnDuino Projects

This document discusses using EEPROM memory with an Arduino board to store data that needs to persist when power is removed. It describes EEPROM's read-only nature but ability to rewrite each location a limited number of times. A 24LC256 EEPROM chip is used, which has a 32KB storage capacity and over 1 million rewrite cycles. The pinout of the 24LC256 is shown and wiring to the Arduino board via the I2C interface is explained.

Uploaded by

brahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

HOME (/) PROJECTS (/PROJECTS) PROGRAMMING (/PROGRAMMING) BLOG (/BLOG) ABOUT (/ABOUT)

ConnDuino Projects (/)

Home (/) > Programming (/programming) > Basic opera ons with EEPROM using Arduino and ConnDuino

Basic operations with EEPROM using Arduino and


ConnDuino Like 1

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 1/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

February

4
2015

In:
EEPROM (/tags/eeprom)
24LC256 (/tags/24lc256)
arduino (/tags/arduino)
connDUINO
(/tags/connduino)
i2c (/tags/i2c)
TWI (/tags/twi)
programming
(/tags/programming)
Posted By: admin
Comments: 2

A temperature controller should remember the desired temperature level a er


powering it off and on. An alarm clock, the next me it should ring. Several projects
should remember some user preferences. Other projects need more space to store
their strings. To these requirements, EEPROM seems a convenient and economical
solu on. This ar cle makes an introduc on to EEPROM modules, how to wire them,
understand them and effec vely take advantage of them in our programs with an
Arduino or any compa ble board, like the ConnDuino that is discussed through this
site. To be more specific, this ar cle is about EEPROM modules that use the I2C (also
called TWI) interface, although a sec on about the Atmega328 built-in EEPROM is
also included. This will be the first in a series of ar cles, dealing with EEPROM.

From the Wikipedia (h p://en.wikipedia.org/wiki/EEPROM) we get a nice defini on


for EEPROM:

As you can see, this memory type is defined as read-only. This doesn’t mean that we
http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 2/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

EEPROM (also written E2PROM and pronounced "e-e-prom", "double-


e prom", "e-squared", or simply "e-prom") stands for Electrically
Used
Erasable Programmable Read-Only Memory and is a type of non- Construction
volatile memory used in computers and other electronic devices to
store small amounts of data that must be saved when power is Machine
removed, e.g., calibration tables or device configuration.
Top Deals at
Factory Price.
cannot ever write anything to them. This would make them useless for any purpose. Contact Directly &
We can write data to them, but each individual memory address may be rewri en up Get Live Quotes!
to a specified number of mes. Typically specifica ons call for a 100 000 write count.
alibaba.com

With this limita on, it seems ineffec ve to use EEPROM for high frequency write
opera ons. For example, if you need to write at the same memory address once per
second, you could destroy this overused memory cell a er: 100 000 / 3600 = 28
hours! However, most applica on don’t require that frequent updates. Examples of
perfectly suitable uses are:

Storing configura on data, that the device needs to read when it is powered on,
and that remain unmodified for its lifecycle.
Storing user preferences that may change during the devices lifecycle without ever
reaching the 100 000 limit.
Storing a daily log of data. The 100 000 limit would be reached a er 274 years.
This is considered safe.

The EEPROM modules we are going to use for this ar cle are the “24LC256” chips.
These come in 8-pin PDIP configura on, and are capable to store up to 256 000 bits
of data which is equal to 32KB. They use the I2C interface and their datasheet

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 3/19
14/04/2018 (h p://www.alldatasheet.com/datasheet-
Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

pdf/pdf/537892/MICROCHIP/24LC256I/P.html) indicates the number of erase-write


cycles to be more the one million.

They may be powered by voltages down to 2,5V and up to 5,5V. Thus, they are usable
with both the Arduino Uno, Mega and the other 5V boards, as well as with the Due
and Lilypad, that operate at 3.3V. Similarly, referring to our board, ConnDuino, they
may be used in either 5V or 3.3V configura on.

Pinout and Wiring


The pinout for the “24LC256” EEPROM module is shown in the figure below.
Descrip ons for each pin are given in the following table.

24LC256 EEPROM pin description

Pin Description

Vcc Connect to positive voltage, in the range 2.5V~5.5V

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 4/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

Pin Description

Vss To ground

To the SDA bus of the I2C interface. In Arduino UNO


SDA
this is A4.

To the SCL bus of the I2C interface. In Arduino UNO


SCL
this is A5.

These three pins define the address of the device in


the I2C interface. All three define the 3-bit value
[A2][A1][A0], which may represent a number A between 0
and 7. When any of these pins is grounded, its bit is
A0, A1, A2
assumed to be zero. Practically, the final I2C address
of the device should be 0x50 + A (in hex format). When
all of them are grounded, the address is 0x50. This
config is used in ConnDuino.

Write protect when pulled up. To be able to write any


WP
data, this pin should be connected to ground.

Wiring an “24LC256” EEPROM module with an Arduino Uno may be accomplished as


shown in the next figure:

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 5/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

(/sites/default/files/pictures/programming_eeprom/EEPROM%20connect_bb.png)

Connec ng an “24LC256” EEPROM module to ConnDuino requires just to plug the


module to the exis ng socket on the board:

(/sites/default/files/pictures/programming_eeprom/Conndiuino_eeprom.jpg)

Atmega microprocessor built-in EEPROM

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 6/19
14/04/2018 As indicated in the datasheet (hoperations
Basic p://www.atmel.com/Images/doc8161.pdf), the Projects
with EEPROM using Arduino and ConnDuino | ConnDuino

Atmega328p microprocessor is equipped with a built-in EEPROM. Its size is 1KB, and
the erase/write count is specified at 100 000.

The size is rather small, but for simple projects that need to store some configura on
op ons this should be adequate. Extra care should be taken however, not to come
close to the specified erase/write cycles, since a failure of the built-in EEPROM could
mean an en re Atmega328p replacement.

To use the built-in EEPROM, you have to include the EEPROM library in your sketch. In
this library two func ons are implemented:

byte EEPROM.read( int address)

void EEPROM.write(int address, byte value)

The first func on returns a byte from the built-in EEPROM while the second writes a
byte to it. Argument ‘address’ is the loca on where the byte should be read from or
wri en to. The first address is 0 and the final one should be 1023, since this is a 1KB
EEPROM.

The following sketch writes an array of 10 bytes to the built-in EEPROM, during setup
and then reads the stored values back, summing them up every 3 seconds. The data
are wri en sequen ally, at addresses star ng from 800 and beyond:

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 7/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects
#include <EEPROM.h>
const int START_ADDRESS = 800;
long sum; //the sum of read values
void setup(){
Serial.begin(9600);
sum = 0;
byte values[10] = {1, 3, 5, 12, 43, 23, 5, 124, 99, 55};
for (int i=0; i<10; i++){
EEPROM.write(i+START_ADDRESS, values[i]);
}
}

void loop(){
delay(3000);
for (int i=0; i<10; i++){
byte readByte = EEPROM.read(i+START_ADDRESS);
sum += readByte;
}
Serial.println(sum);
}

A more advanced library that supports transparent wri ng and reading of any data
type, not only single bytes, is EEPROMex.Details can be found here
(h p://playground.arduino.cc/Code/EEPROMex).

Using external I2C EEPROM in a sketch program


EEPROMs organize their space in a per byte base. This means you can write or read
single bytes. You should be in posi on to interpret these bytes to meaningful data
types, like char, int, float etc. But, many data types consist of more than one byte.
Typically, the common data types in C/C++ have the following lengths in the Arduino
environment:

char: 1 byte
bool: 1 byte
int: 2 bytes
http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 8/19
14/04/2018 long: 4 bytes Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

float: 4 bytes
double: 4 bytes on UNO/Mega, 8 bytes on Due!

In the Arduino environment the following data types exist also, with the specified
lengths:

byte: 1 byte
uint8_t : 1 byte
uint16_t: 2 bytes

Custom data types, like structs and classes or compound data like unions and arrays
have a variable size that could be as low as 1 byte, and up to thousands, restricted
only by the available memory.

Any mul -byte object should be wri en or read as a sequence of bytes. Of course
decomposing variables to bytes and recomposing them, any me we want to write or
read something in EEPROM sounds more than laborious. Hopefully, for simple
objects, it can be accomplished in an easy and almost transparent way, using the two
rou nes shown below. They use the Wire library for handling the I2C bus
communica on.

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 9/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects
template <class T>
uint16_t writeObjectSimple(uint8_t i2cAddr, uint16_t addr, const T& value)
{
const uint8_t* p = (const uint8_t*)(const void*)&value;
uint16_t i;
for (i = 0; i < sizeof(value); i++){
Wire.beginTransmission(i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.write(*p++);
Wire.endTransmission();
addr++;
delay(5);
}
return i;
}

template <class T>


uint16_t readObjectSimple(uint8_t i2cAddr, uint16_t addr, T& value){
uint8_t* p = (uint8_t*)(void*)&value;
uint8_t objSize = sizeof(value);
uint16_t i;
for (i = 0; i < objSize; i++){
Wire.beginTransmission (i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.endTransmission();
Wire.requestFrom(i2cAddr, (uint8_t) 1);
if(Wire.available()){
*p++ = Wire.read();
}
addr++;
}
return i;
}

As you can see, these are template func ons. When the sketch is compiled, they are
realized, replacing their T argument with the correct data type, the user calls them
with. If called with many different data types, many instances of these func ons will
be created. Using templates, the replica on of the same code for many different data
types, that should be handled the same way, is avoided. However, the compiled
sketch is not reduced in size. Inside the processor different
http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs versions of the required 10/19
14/04/2018 func ons are created, as if Basic
we have wri with
operations enEEPROM
them for any
using special
Arduino data-type
and ConnDuino we need.
| ConnDuino Projects

An inconvenience though is that template func ons should be defined in a separate


header file.

The writeObjectSimple func on writes a value of any type, to the specified


memory address. The func on finds the size of the data type and sends it byte-by-
byte to the Wire object, that is responsible for the I2C interface communica on. The
I2C address is specified with the first argument. Using the func on is straigh orward.
In the following code snippet, two float values are wri en to the I2C EEPROM (with
I2C address 0x50), consecu vely, star ng at memory address 24000:

float f1 = 25.5; //the 1st variable to be written


float f2 = 45.05; //the 2nd one
uint16_t addr = 24000;
addr += writeObjectSimple (0x50, addr, f1);
addr += writeObjectSimple (0x50, addr, f2);

The readObjectSimple func on reads a value of any type, from the specified
memory address. The func on finds the size of the data type and requests it byte-by-
byte from the Wire object, that is responsible for the I2C interface communica on.
The I2C address is specified with the first argument. Using the func on is again
simple. In the following code snippet the two float values that wri en before, are
read back from EEPROM:

float f1=0; //the 1st variable to be read


float f2=0; //the 2nd one

uint16_t addr = 24000;


addr += readObjectSimple (0x50, addr, f1);
addr += readObjectSimple (0x50, addr, f2);
Serial.println(f1);
Serial.println(f2);

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 11/19
14/04/2018 What will happen when someone tries to
Basic operations withread back
EEPROM a value
using Arduinousing different
and ConnDuino data type
| ConnDuino Projects

than the one used for wri ng it? For example, if a stored float is a empted to be read
as long? The code will run without problems at during reading, but the result will be
meaningless. The read long will get some arbitrary value (not the truncated float)
because different data-types have completely different byte representa ons. If we
are lucky this value may cause a crash to the program or result in a profound error in
our output. If not, we may find ourselves in the unfortunate posi on, to treat
meaningless output as correct one. With a few words: you should know where and
with what data type each object is stored.

Concluding, these func ons are simple and safe. You can use them with any data
type, having any size, and the result will be as expected. No worries about crossing
the EEPROM page borders or about the Wire library buffer limit, since only one byte
is wri en or read each me. They are great for any simple or complex opera ons and
they have a small footprint to the sketch size. The only drawback is that they are
slow. For simple uses they probably are fine. But, EEPROM support the so called
block read/write opera ons, that enable the transfer of mul ple bytes in one pass.
This subject will be covered in a following ar cle.

Example
The following sketch writes an array of 10 int’s to the I2C EEPROM, during setup and
then reads the stored values back, summing them up every 3 seconds. The data are
wri en sequen ally, at addresses star ng from 831 and beyond. Using this address,
the first int crosses a page border, but the data are wri en correctly, because single
bytes are send to EEPROM each me. Output to the serial port should like this:

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 12/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects
2000
4000
6000
8000

The sketch file should contain the following:

#include "eeprom_routines.h"
#include <Wire.h>
#define START_ADDRESS 831//memory address of the 1st stored byte
long sum; //the sum of read values, updated in loop
//--------------------------------------------------------------
void setup(){
Serial.begin(9600);
Wire.begin();
sum = 0; //initialize sum
//define the values
int values[10] = {1026, 3, 5, 22, 43, 23, 500, 224, 99, 55};
int addr = START_ADDRESS; //this will increment after each write
for (int i=0; i<10; i++){
addr += writeObjectSimple(0x50, addr, values[i]);
}
}
//--------------------------------------------------------------
void loop(){
delay(3000);
int addr = START_ADDRESS; //this will increment after each read
int readInt; //the read value

for (int i=0; i<10; i++){


addr += readObjectSimple(0x50, addr, readInt);
sum += readInt;
}
Serial.println(sum);
}

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 13/19
14/04/2018 The eeprom_routines.hBasic
header file should
operations contain
with EEPROM using the template
Arduino func | ConnDuino
and ConnDuino ons Projects

men oned above. It should contain the following:

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 14/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects
#ifndef EEPROM_ROUTINES
#define EEPROM_ROUTINES
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
template <class T>
uint16_t writeObjectSimple(uint8_t i2cAddr, uint16_t addr, const T& value)
{
const uint8_t* p = (const uint8_t*)(const void*)&value;
uint16_t i;
for (i = 0; i < sizeof(value); i++){
Wire.beginTransmission(i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.write(*p++);
Wire.endTransmission();
addr++;
delay(5); //max time for writing in 24LC256
}
return i;
}

template <class T>


uint16_t readObjectSimple(uint8_t i2cAddr, uint16_t addr, T& value){
uint8_t* p = (uint8_t*)(void*)&value;
uint8_t objSize = sizeof(value);
uint16_t i;
for (i = 0; i < objSize; i++){
Wire.beginTransmission (i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.endTransmission();
Wire.requestFrom(i2cAddr, (uint8_t)1);
if(Wire.available()){
*p++ = Wire.read();
}
addr++;
}
return i;
}
#endif

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 15/19
14/04/2018 In a following ar cle, the page
Basic write andwith
operations read opera
EEPROM ons
using willand
Arduino be ConnDuino
covered.| ConnDuino
This will Projects
enable sending and ge ng mul ple bytes to EEPROM, making things considerably
faster. Applica ons to character string and array storage will be shown.

Useful links
EEPROM Library (h p://arduino.cc/en/Reference/EEPROM)

Arduino EEPROMex library (h p://playground.arduino.cc/Code/EEPROMex)

Adding External I2C EEPROM to Arduino (24LC256)


(h p://www.hobbytronics.co.uk/arduino-external-eeprom)

24LC256 product page (h p://www.microchip.com/wwwproducts/Devices.aspx?


dDocName=en010823)

Add new comment (/articles/admin/basic-operations-eeprom-using-arduino-and-


connduino#comment-form)

Comments
Erik (Not Veri ed) Permalink (/comment/73#comment-73), May 19th, 2015

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 16/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

EEPROM (/comment/73#comment-73)

What a great explana on of using an EEPROM ! By far the best I looked at, thank you!

Also I am looking forward to your weather sta on and watering system projects. I have a long
interest in weather/weather info gathering. Just build an arduino system to capture the serial data
from the Peet Bros weather sta on so I can nog do with that data whatever I want, build my own
'weather picture' for example. great stuff.

Gree ngs from The Netherlands!

Erik

reply (/comment/reply/33/73)

Admin Permalink (/comment/117#comment-117), June 9th, 2015

EEPROM library (/comment/117#comment-117)

Thank you Erik. Weather data is my thing too.

For more powerful uses of EEPROM i have built a library that i will publish as soon as i find some
extra me. It is capable to write/read arrays, bitmap pictures, fonts and of course strings. It is
quite impressive, par cularly when coupled with a TFT screen for rendering text using a
calligraphic font.

reply (/comment/reply/33/117)

Add new comment


http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 17/19
14/04/2018 Your name Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

Subject

Comment *

No HTML tags allowed. More informa on about text formats (/filter/ ps)
Web page addresses and e-mail
addresses turn into links automa cally.
Lines and paragraphs break automa cally.

CAPTCHA

This ques on is for tes ng whether or not you are a human visitor and to prevent automated spam
submissions.

I'm not a robot


reCAPTCHA
Privacy - Terms

Save Preview

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 18/19
14/04/2018 Basic operations with EEPROM using Arduino and ConnDuino | ConnDuino Projects

User login
Username *

Password *

Request new password (/user/password)

CAPTCHA

This ques on is for tes ng whether or not you are a human


visitor and to prevent automated spam submissions.

I'm not a robot


reCAPTCHA
Privacy - Terms

Log in

Site created by: Minas Lemonis (mailto:[email protected]) ↑

Credits: Ported to Drupal by Drupalizing, a Project of More than Themes. Designed by Site5 WordPress Themes.

http://www.connduino.com/articles/admin/basic-operations-eeprom-using-arduino-and-connduino#sthash.qAH9BfZ4.dpbs 19/19

You might also like