Basic Operations With EEPROM Using Arduino and ConnDuino - ConnDuino Projects
Basic Operations With EEPROM Using Arduino and ConnDuino - ConnDuino Projects
HOME (/) PROJECTS (/PROJECTS) PROGRAMMING (/PROGRAMMING) BLOG (/BLOG) ABOUT (/ABOUT)
Home (/) > Programming (/programming) > Basic opera ons with EEPROM using Arduino and ConnDuino
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
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
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
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.
Pin Description
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
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)
(/sites/default/files/pictures/programming_eeprom/Conndiuino_eeprom.jpg)
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:
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).
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;
}
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
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:
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
…
#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
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
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;
}
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)
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.
Erik
reply (/comment/reply/33/73)
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)
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.
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 *
CAPTCHA
Log in
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