This library contains two types of class; first one is eeWrite which writes to EEPROM, second one is eeRead which reads from EEPROM. First; you should include the Arduino.h; Main include file for the Arduino SDK and define the EEPROM address; within this library we will use the 0x50; which is addressing i2C adr = 0b1010 000 0 . It is actually depends on your wiring and EEPROM. You should read your EEPROM Datasheet before addressing. Let's see the connections and wires in diagram;
Connection Diagram
I'm using Fritzing for simple diagrams. Fritzing is an open-source hardware initiative that makes electronics accessible as a creative material for anyone.
Wiring Description
I tried to make a simple the diagram with dfferent colored wires for better understanding.
Red wire: +5V DC Positive supply voltage
Blue wire: GND Ground
Black wire: Write Protect for EEPROM; when tied to GND, allows normal writevoperations. When tied high to VCC, all write operations to the upper quandrant (8/16K bits) of memory are inhibited.
Purple wire: I2C Addressing; 0X50 in this sample.
Orange wire: I2C Clock Signal - SCL - Clock Signal of the Controller
Yellow wire: I2C Data Signal - SDA - Data Signal of the Controller
Includes and Definitons
As i described before DEVICE address for this wiring and as Datasheet of EEPROM points; i2C adr = 0b1010 000 0 which equals 0x50. We define the address and include core Arduino library.
#include <Arduino.h>
#define DEVICE 0x50
EEPROM Write Function
eeWrite function; writes the values to EEPROM;
template <class T> int eeWrite(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
Wire.beginTransmission(DEVICE);
Wire.write((int)(ee >> 8)); // MSB
Wire.write((int)(ee & 0xFF)); // LSB
for (i = 0; i < sizeof(value); i++)
Wire.write(*p++);
Wire.endTransmission();
return i;
}
EEPROM Read Function
eeRead function; reads the values from EEPROM;
template <class T> int eeRead(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
int i;
Wire.beginTransmission(DEVICE);
Wire.write((int)(ee >> 8)); // MSB
Wire.write((int)(ee & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(DEVICE,sizeof(value));
for (i = 0; i < sizeof(value); i++)
if(Wire.available())
*p++ = Wire.read();
return i;
}
Using Library
For using this library; you should include another core Arduino library <Wire.h>,
#include <Wire.h>
#include <korayEEPROM.h>
After includes i strongly recommend you to create a structure for writing multiple values at once;
struct config
{
long TestLongOne;
long TestLongTwo;
float TestFloat;
bool TestBool;
int TestInt;
} config;
EEPROM Write
Then just fill this structure with values that you want to write to EEPROM and call eeWrite function;
config.TestLongOne = 125487;
config.TestLongTwo = 458762;
config.TestBool = false;
config.TestInt = 54;
config.TestFloat = 4.47;
eeWrite(0,config);
EEPROM Read
Just call eeRead function; your values will be read and assign to config structure and after that you can get your values;
eeRead(0,config);
Serial.println("Your EEPROM Values");
Serial.print("TestLongOne : ");
Serial.println(config.TestLongOne);
Serial.print("TestLongTwo : ");
Serial.println(config.TestLongTwo);
Serial.print("TestBool : ");
Serial.println(config.TestBool);
Serial.print("TestInt : ");
Serial.println(config.TestInt);
Serial.print("TestFloat : ");
Serial.println(config.TestFloat);
Arduino External EEPROM Library Download
Download this Arduino Library, extract the downloaded zip file and put it in your Arduino Libraries Folder; which is located under your documents folder; \Documents\Arduino\libraries
. After that it will be visible at your Arduino IDE Software; under File|Examples|Examples from Custom Libraries.