Can't read/write to EEProm chip

I'm using an Arduino Mega 2560 with a 24LF256 EEProm chip that I'm trying to write and read from. I've got the chip connected as is described on page 437 of the "Arduino Cookbook" and I'm using the program that is on the following pages. I've gone over this a million times for validity, both the hookup and the program. I've also put a scope probe on both analog 4 and 5 pins. Both pins stay at a solid ground potential.When the program runs, it stops waiting for the read to occur.

Does anyone have any clue to what I'm doing wrong? I've tried several other EEProm chips and no change. Also, I removed the chip and the lines always stay at a ground level.

Terry

tneckar:
Does anyone have any clue to what I'm doing wrong?

So far, I'll we've got is "it doesn't work" and a page number to a book we may or may not have. Going to need more information than that.

Here is a link to the schematic: ( I can't figure out how to get a picture to work)

https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-13/figure-13-9

Here is the code:

#include <string.h>
#include <Wire.h>

const byte EEPROM_ID = 0x50;

int thisByte= 33;

void setup()
{  
  Serial.begin(9600);
  Serial.print("Starting test\n");
  
  Wire.begin();
  Serial.println("Writing 1024 bytes to EEPROM");
  
  for(int i=0; i<1024; i++)
  {
    EEPROM_Write(i, thisByte);
    thisByte++;
    if(thisByte == 126)
    thisByte = 33;
  }
  
  Serial.println("Reading 1024 bytes from EEPROM");
  
  int thisByte = 33;
  for(int i=0; i<1024; i++)
  {
    char c = EEPROM_Read(i);
    if(c != thisByte)
    {
      Serial.println("read error");
      break;
    }
    else
    {
      Serial.println("Something");
      Serial.print(c);
    }
    thisByte++;
    if(thisByte == 126)
    {
      Serial.println();
      thisByte = 33;
    }
  }
  Serial.println("EEPROM read OK");  
}

void loop()
{
  
}

void EEPROM_Write(unsigned int address, byte data)
{
  Wire.beginTransmission(EEPROM_ID);
  Wire.write((int)highByte(address));
  Wire.write((int)lowByte(address));
  Wire.write(data);
  Wire.endTransmission();
  delay(5);
}

byte EEPROM_Read(unsigned int address)
{
  byte data = 0;
  Wire.beginTransmission(EEPROM_ID);
  Wire.write((int)highByte(address));
  Wire.write((int)lowByte(address));
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_ID, (byte)1);
  Serial.println("Waiting");
  while(Wire.available() == 0);
  data = Wire.read();
  return data;
}

I hope this helps. I have another Arduino Mega on order so it should be here this week, just in case.

Terry

There's a thread of mine from yesterday on how to make them work, but mine is a smaller and only uses an 8 bit register.

Things i would try:

Get an I2C scanner and verify that the chip is found and it is on the address you think it is.

First, as casemod says confirm the eeprom i2c address with an address scanner tool.

I can't comment on your connections to the eeprom, but there are errors in the code.

Your syntax for the write and read functions is leaving out the eeprom address. Here is the correct code for the functions

void EEPROM_Write(int device_addr, unsigned int address, byte data)
{
  Wire.beginTransmission(device_addr);
  Wire.write((int)highByte(address));
  Wire.write((int)lowByte(address));
  Wire.write(data);
  Wire.endTransmission();
  delay(5);
}

byte EEPROM_Read(int device_addr , unsigned int address)
{
  byte data = 0;
  Wire.beginTransmission(device_addr);
  Wire.write((int)highByte(address));
  Wire.write((int)lowByte(address));
  Wire.endTransmission();
  Wire.requestFrom(device_addr, 1);   //leave out the declaration of (byte) before the 1
  Serial.println("Waiting");
  while(Wire.available() == 0);
  data = Wire.read();
  return data;
}

When you call these functions from the code, add the EEPROM_ID

 EEPROM_Write(EEPROM_ID, i , thisByte);
char c = EEPROM_Read(EEPROM_ID, i);

I found several scanner tools and all came up with the same results:

I2C Scanner
Scanning...
No I2C devices found

So my guess is that the Arduino Mega is broken. Luckily I have one on order.
I'll keep you posted.

Thanks guys,
Terry

Terry--

i just want to check that you are aware that the SDA/SCL I2C pins on the mega are are 20/21 and not the A4/A5 you show in the linked schematic which is for a UNO..

That was it! I just "assumed" that the Mega only added pins and functionality, not that it reassigned pin functions. Next time I'll be more careful. Thanks for your help!! I thought I was going crazy.

Terry