Hello. I'm trying to work with EEPROM using I2C (my chip is 24LS256). I use Wire library and I should not use EEPROM library. It works properly with EEPROM library but doesn't work by Wire. This problem has been resolved many times and a lot of solutions are presented in internet, althought not all are correct. Newertheless my code doesn't work and I don't know how make it work. Here is my code for Arduino
Code with EEPROM library works but Wire. I tried to read from the same adress i.e. Wire.requestFrom(devadress,1); I would be vary appriciate if anybody point me the possible reason.
I listening serial port by my PC program. When I use EPROM then answer is correct, I get 4, but in case with Wire, i dont have an answer, because arduino waiting a byte in while(Wire.available()==0);
I'd start by checking the return values from the endTransmission() calls. What return values do you get?
I think the 10ms delay should be long enough, but I can't find a datasheet for a 24LS256 so I can't be sure. Is that the right part number? Do you have a datasheet? I have a datasheet for a 24LC256 and it says 5ms for the write time. I might try a test with 50ms. Polling as described in the datasheet might be a better option.
PS: I believe the EEPROM should have three address pins (A0, A1, A2). How are they wired?
I tried your code and it works. Like Nick said, the while statement can be eliminated, but neither does it cause a problem. I did change the input character that starts the write process and also the way the output was sent so I could read it on the serial monitor. So run the I2C scanner as Nick suggested, check your wiring, address pins on the EEPROM, etc.
Thanks a lot for your helping, guys. I did it as you recomended, but it brought no progress. Howewer, I observed an amazing thing. I run I2C scanner and found 4 devices with addresses 0x50, 0x51, 0x52, 0x53. But is should be only 0x50, judging by wiring. May it be the result of a closing caused by unaccurate soldering? While was removed because Wire.available() does not return result. Here is my corrected code:
#include <Wire.h>
#include <EEPROM.h>
const int a0=70;
const int a1=130;
int ptr = 0;
int mem_cell_num=100;
const int devadress=0x50;
void setup()
{
Wire.begin();
Serial.begin(115200);
}
void loop()
{
byte a=Serial.read();
if (a==0xA1)
{
byte val = 4;
Wire.beginTransmission(devadress);
Wire.write ((int)lowByte(mem_cell_num));
Wire.write ((int)lowByte(mem_cell_num));
Wire.write(val);
if(Wire.endTransmission()!=0)//OK
{
//send error code if fail
Serial.write(0xC0);
Serial.write(13);
};
delay(5);
Wire.beginTransmission(devadress);
Wire.write ((int)lowByte(mem_cell_num));
Wire.write ((int)lowByte(mem_cell_num));
if (Wire.endTransmission()!=0)//OK
{
//send error code if fail
Serial.write(0xC0);
Serial.write(15);
}
if (Wire.requestFrom(devadress,1)==1)//FAIL
{
//send data if success
val=Wire.read();
Serial.write(0xC0);
Serial.write(val);
}
/*
EEPROM.write(ptr, val+4);
val= EEPROM.read(ptr);
ptr = ptr + 1;
if (ptr == 512)
ptr = 0;
if(val>0)
{
Serial.write(0xC0);
Serial.write(val);
}
*/
}
}
Seems like it would just about have to be hardware, like I said, the previous code works for me. At this point it would be good to have a schematic and a picture of the circuit.
I'm still confused about the chip you're using, you say it's 24LS256, I can't find that on Mouser, Digikey, etc. I tested with a 24LC256. Do you have a link to a datasheet or where you got this chip? The scanner only returns 0x50 for me, I have pins 1, 2, 3 (A0, A1, A2) grounded.
Just on a lark, I disconnected the address pins (A0, A1, A2) and let them float. (Not recommended by the datasheet.) Running the scanner multiple times, all I could get was 0x50. Tried two different chips, one a 24LC256 and one a 24FC256, and even tried grabbing onto the address pins, no change. More robust than I might have thought. They must have pulldown resistors.