Monday, April 23, 2012

I2C data logging chip with Arduino.

I2C data logging chip.

I was programming the arduino and ran into a limitation regarding the memory inside the device.  The 328 chip only has 2KB of RAM for data strings.  And if you lose power that memory goes away.  It has 1KB of eeprom that is retained between reboots, which is only a day or two of data logging.

I found a chip called the 24LC256 on http://sparkfun.com and found the following web site that described how to use the device.  http://10kohms.com/arduino-external-eeprom-24lc256







The code I used is here:

#include <Wire.h>     

#define disk1 0x50    //Base Address of 24LC256 eeprom chip

void setup(void){
  Serial.begin(115200);
  Wire.begin();   

  unsigned int i = 0;

  for (i=0; i<256; i++){
    writeEEPROM(disk1, i, i);
  }
}

void loop(){
  unsigned int i = 0;

  for (i=0; i<256; i++){
    Serial.println(readEEPROM(disk1, i), DEC);
  }
  delay(1000);
}

void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.send(data);
  Wire.endTransmission();

  delay(5);
}

byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
  byte rdata = 0xFF;

  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8));   // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();

  Wire.requestFrom(deviceaddress,1);

  if (Wire.available()) rdata = Wire.receive();

  return rdata;
}

I used the code from the site above, but modified it to write 256 bytes, storing the index of the byte into that data, then reading those same 256 bytes back out.  I did more than a single byte to test the speed.  Even though you are only reading a single byte at a time, it seems fast.

I2C seems to be an interesting, easy to use protocol.   You can add more I2C circuits into the string of items by just chaining in the +5V, Ground, SDL, and SDA wires from this memory chip to the next chip in the line.  Make sure you give each chip a different address.

A negative thing is that you lose a couple of analog pins to the I2C protocol needs, but in return you gain the ability to add a 100 devices to the chip.

I2C info is here:
i2c implementation
Arduino I2C Expansion I/O « Keith’s Electronics Blog
Arduino Forum - Faster I2C
I2C-Bus: What’s that?
I2C Bus: I2C Primer
Wire \ Libraries \ Wiring ALPHA 1.0
Introduction to I2C | uC Hobby
Arduino and an I2C temperature sensor : Naneau
Wiki article on I2C
Near Future Laboratory » Blog Archive » Arduino and the Two-Wire Interface (TWI/I2C) Including A Short Didactic Parenthetical On Making TWI Work On An Arduino Mini
Arduino Forum - Faster I2C
An interesting thing to do would be to connect up multiple Arduinos using I2C between Arduino’s
or Experiment: I2C communication between two Arduino boards | Absences

No comments:

Post a Comment