Sony Arouje

a programmer's log

Connect Arduino to external EEPROM

leave a comment »

Arduino has a tiny built in hard drive called EEPROM, in this area we can write data that should be available after power cycles. Unfortunately the size of this built in EEPROM is 512 byte for Atmega168 and 1kb for Atmega328. If our application is dealing with very small amount of data to persist then built in EEPROM is a good choice.

In some scenarios we might need to keep some logging info or any kind of data that needs more than 1kb then we need to go for external EEPROM. For this post I am using 24LC256, it can store 256kb of data. We can connect upto 8 ICs to a single Arduino by changing the voltage in A0, A1 and A2 pins.

clip_image001

 

Connection to Arduino

connection_bb

 

Here I connect A0 to A2 to GND and that gives me an address of 0x57.

VSS to GND, VCC to 5v, WP to GND, SCL to A5, SDA to A4

High on WP will disable writing, in my scenario I need to write, so I connect to GND.

 

Source Code

To deal with EEPROM reading and writing, I used a library called Extended Database Library (EDB). I also used the samples provided along with EDB but with some modifications.

 

#include <EDB.h> #include <Wire.h> #define disk 0x50 #define TABLE_SIZE 131072 // 1 device struct LogEvent { int id; int temperature; } logEvent; void writer(unsigned long address, byte data) { Wire.beginTransmission(disk); Wire.write((int)(address>>8)); Wire.write((int)(address & 0xFF)); Wire.write(data); Wire.endTransmission(); delay(5); } byte reader(unsigned long address) { byte rdata = 0xFF; Wire.beginTransmission(disk); Wire.write((int)(address>>8)); Wire.write((int)(address & 0xFF)); Wire.endTransmission(); Wire.requestFrom(disk,1); if(Wire.available()) rdata = Wire.read(); return rdata; } EDB db(&writer, &reader); void setup() { Serial.begin(9600); Wire.begin(); randomSeed(analogRead(0)); Serial.println("Creating db..."); EDB_Status result = db.create(2, TABLE_SIZE, (unsigned int)sizeof(logEvent)); if (result != EDB_OK) printError(result); Serial.println("Created db..."); createRecord(1); selectAll(); } void loop() { // put your main code here, to run repeatedly: } void createRecord(int recno) { Serial.println("Creating Records..."); logEvent.id = 1; logEvent.temperature = random(1, 125); EDB_Status result = db.insertRec(recno, EDB_REC logEvent); if (result != EDB_OK) printError(result); Serial.println("DONE"); } void selectAll() { for (int recno = 1; recno <= db.count(); recno++) { EDB_Status result = db.readRec(recno, EDB_REC logEvent); if (result == EDB_OK) { Serial.print("Recno: "); Serial.print(recno); Serial.print(" ID: "); Serial.print(logEvent.id); Serial.print(" Temp: "); Serial.println(logEvent.temperature); } else printError(result); } } void deleteAll() { Serial.print("Truncating table..."); db.clear(); Serial.println("DONE"); } void printError(EDB_Status err) { Serial.print("ERROR: "); switch (err) { case EDB_OUT_OF_RANGE: Serial.println("Recno out of range"); break; case EDB_TABLE_FULL: Serial.println("Table full"); break; case EDB_OK: default: Serial.println("OK"); break; } }

 

 

Happy coding…

Written by Sony Arouje

April 14, 2016 at 1:39 am

Posted in Arduino

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: