Cannot Read/Write to an 24LC256 EEPROM chip

I am trying to write and read data to an 24LC256, I2C EEPROM along with my Arduino Leonardo. The following code is copied from a video tutorial found at youtube Arduino Tutorial #8: I2C Communication - YouTube

#include <Wire.h>
#define ADDRESS 0x50

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   delay(15);
   eepromWrite(0,0,125);             //write 125 to external rom at address 00
   delay(5);
   Serial.println(eepromRead(0,0));                 //read from external eeprom at address 00
}

void loop()
{
  
}

void eepromWrite(byte highAddress, byte lowAddress, byte data)
{
  Wire.beginTransmission(ADDRESS);
  Wire.write(highAddress);
  Wire.write(lowAddress);
  Wire.write(data);
  Wire.endTransmission();
}

byte eepromRead(byte highAddress, byte lowAddress)
{
  Wire.beginTransmission(ADDRESS);
  Wire.write(highAddress);
  Wire.write(lowAddress);
  Wire.endTransmission();
  Wire.requestFrom(ADDRESS,1);          //request one byte of data from eeprom at address 0x50
  while(!Wire.available())
     {
     }
  return Wire.read();
}

When running the code, at the serial monitor i have no output at all. I ran an I2C scanner and i get the message

I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).

which means that the chip is connected correctly. I tried various sketches for reading/writing to the eeperom but the closest thing to a result that appeared at the serial monitor was with this code.

#include <Wire.h>
void eepromWrite(byte address, byte source_addr, byte data) {
Wire.beginTransmission(address);
Wire.write(source_addr);
Wire.write(data);
Wire.endTransmission();
}
byte eepromRead(int address, int source_addr) {
Wire.beginTransmission(address);
Wire.write(source_addr);
Wire.endTransmission();
Wire.requestFrom(address, 1);
if(Wire.available())
return Wire.read();
else
return 0xFF;
}
void setup() {
Wire.begin();
Serial.begin(9600);
for(int i = 0; i < 10; i++) {
eepromWrite(B01010000, i, 'a'+i);
delay(100);
}
Serial.println("Bytes written to external EEPROM !");
}
void loop() {
for(int i = 0; i < 10; i++) {
byte val = eepromRead(B01010000, i);
Serial.print(i);
Serial.print("\t");
Serial.print(val);
Serial.print("\n");
delay(1000);
}
}

but then again the Serial monitor was showing the following data

1 105
2 61
3 255
4 255
5 255
6 255
7 255
8 255
9 255
0 255

Any help is much appreciated

Your first sketch works for me.

The second one won't work because it is only writing one address byte in the read and write functions.

Pete

In the first sketch, try adding

  while(!Serial);

after Serial.begin

Pete

Be aware there is a latency after a write to an EEPROM of IIRC 5 millis()

Please use CTRL-T in the IDEfor proper indenting (makes code better readable)

I wrote a lib for I2C eeprom with test sketch - Arduino Playground - LibraryForI2CEEPROM - that keeps track of the write latency. you can give it a try,.

With some testing i got some results

if i move the command

 Serial.println(eepromRead(0,0));                 //read from external eeprom at address 00

from the setup function at the loop function then the program works but my problem is that i want to read the eeprom once and not loop the read command

kookoo_gr:
With some testing i got some results

if i move the command

 Serial.println(eepromRead(0,0));                 //read from external eeprom at address 00

from the setup function at the loop function then the program works but my problem is that i want to read the eeprom once and not loop the read command

As el_supremo has suggested, try putting

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

after Serial.begin in setup(). The Leonardo can take a little longer to initialize the serial port.

the suggestion did not work, the serial monitor is blank even when i waited for full minute

problem solved. The original sketch works perfectly as it should. If i change the delay at the setup() from delay(5) to e.g. delay(5000) the value of the eeprom will be displayed at serial monitor even if i do not use the while(!Serial) command.

Glad you got it sorted in the end. I have some SRAM chips that work fine on my UNO every time but on my MEGA I need to slow the SPI clock speed if using burst data mode (I think the Arduino is almost to slow for the device)