Go Down

Topic: Cannot Read/Write to an 24LC256 EEPROM chip (Read 2081 times) previous topic - next topic

kookoo_gr

Dec 31, 2013, 04:16 pm Last Edit: Dec 31, 2013, 04:40 pm by kookoo_gr Reason: 1
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 http://www.youtube.com/watch?v=J3nuIL2dBak&feature=c4-overview-vl&list=PLE0C7D8863F6DACCE

Code: [Select]

#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.

Code: [Select]

#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

el_supremo

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

el_supremo

In the first sketch, try adding
Code: [Select]
  while(!Serial);
after Serial.begin

Pete

robtillaart

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 - http://playground.arduino.cc/Main/LibraryForI2CEEPROM - that keeps track of the write latency.  you can give it a try,.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kookoo_gr

With some testing i got some results

if i move the command

Code: [Select]

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

Riva


With some testing i got some results

if i move the command

Code: [Select]

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
Code: [Select]
  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.
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

kookoo_gr

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

kookoo_gr

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.

Riva

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)
I might seem like an idiot but remember it takes one to know one.
Forum user generally fall into three categories (the good, the bad & the a**hole) what category do you fit in?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy