Thursday, July 18, 2019

0000 0000 0000 0100

AT24C256 EEProm Module


This week's lucky component is the AT24C256 external EEProm module. I have a few of these in "raw chip" form, which work fine, but the joy is doubled at least through the use of the module version which has the requisite connections, resistors and jumpers as shown below.

Bought at a bargain price (AUD 1.26), these are great not only for development but also the programming of the EEProm chips prior to using them in a project. 

The jumper labelled (WP) as shown above enables writing to the EEProm. The other jumpers set the I2C address of the module at 0x50. If you move the A0 jumper for instance, the address will shift to 0x51, and so on, which allows multiply daisy-chained modules with separate addresses.

Connecting SDA with A4 on the Nano, and SCL with A5 enables communication between the EEProm and the Arduino.

Next you're going to need some some code! It's nice to use an I2C scanner to confirm the correct EEProm address and that everything is ready to go for reading and writing.

I then modified this code to produce a simple sketch to write and then read "HELLO!" (well, what else?) - here is my version:

#include <Wire.h>

#define EEPROM_ADDR 0x50

char mymessage[6] = "HELLO!";
char readitnow = "";

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Serial.println("Writing EEProm");
  Serial.println();
  delay(1500);

  writeonce();

  Serial.println();
  Serial.println();

  Serial.println("Reading EEProm");
  Serial.println();
  delay(1500);

  readonce();
}

void writeonce() {

  for (int i = 0; i < 6; i++) {
    i2c_eeprom_write_byte(EEPROM_ADDR, i, mymessage[i]);
    Serial.print("Address: ");
    Serial.print(i);
    Serial.print("   Value: ");
    Serial.println(mymessage[i]);
    delay(500);
  }
}

void readonce() {

  for (int i = 0; i < 6; i++) {
    readitnow = i2c_eeprom_read_byte(EEPROM_ADDR, i);
    Serial.print("Address: ");
    Serial.print(i);
    Serial.print("   Value: ");
    Serial.println(readitnow);
    delay(500);
  }
}


void loop()
{
}

void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
{
  int rdata = data;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));    // Address High Byte
  Wire.write((int)(eeaddress & 0xFF));  // Address Low Byte
  Wire.write(rdata);
  Wire.endTransmission();
}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));    // Address High Byte
  Wire.write((int)(eeaddress & 0xFF));  // Address Low Byte
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress, 1);
  if (Wire.available()) rdata = Wire.read();
  return rdata;
}

Speaking (er, writing) of using an EEProm in a project, next week I think I'll look at using a small memory attiny chip to play some music using the external memory of this EEProm chip. Can't wait!


No comments:

Post a Comment