Wire library and EEPROM

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

#include <Wire.h>
#include <EEPROM.h> 

int ptr = 0;
int mem_cell_num=100;
const int devadress=0x50;


void setup()
{

   Wire.begin(); // ??????????? ? ???? i2c
   Serial.begin(115200);
}

void loop()
{
   byte a=Serial.read(); 
  if (a==0xA1)
  {

     byte val = 0;
     
     Wire.beginTransmission(devadress);
     Wire.write((int)highByte(mem_cell_num)); 
     Wire.write((int)lowByte(mem_cell_num)); 
     Wire.write(val+4);
     Wire.endTransmission();
     delay(10);
     
     Wire.beginTransmission(devadress);
     Wire.write((int)highByte(mem_cell_num)); 
     Wire.write((int)lowByte(mem_cell_num)); 
     Wire.endTransmission();
     Wire.requestFrom(devadress+1,1); 
     
     while(Wire.available()==0);
     val=Wire.read();
     Serial.write(0xC0);
     Serial.write(val);  
     
    /*      
     EEPROM.write(ptr, val+4);
     val= EEPROM.read(ptr);
        
     if(val>0)
     {
        Serial.write(0xC0);
        Serial.write(val);  
     }
    */
  }
}

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.

Shouldn't requestFrom() use the same device address as the other calls? Why devadress+1?

     Wire.requestFrom(devadress+1,1);

I don't think the (int) casts are necessary, but I'm not sure they'd be a problem either.

Yes, it should. I also tried so. Still doesn't work. Adresess 0x55, 0x57, 0x54, 0x56 are also checked with the same result.

Ah right I see where you said that now.

So what output do you get?

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?

     while(Wire.available()==0);

That line is not necessary. See: Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

There was a recent thread about reading stuff back. Read this:

http://forum.arduino.cc//index.php?topic=192952

Please post your amended code. You should requestFrom the same address you wrote to.

Please run the I2C scanner on this page and report the results.

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.

#include <Wire.h>
#include <EEPROM.h> 

int ptr = 0;
int mem_cell_num=100;
const int devadress=0x50;

void setup()
{
    Wire.begin(); // ??????????? ? ???? i2c
    Serial.begin(115200);
}

void loop()
{
    byte a=Serial.read(); 
    if (a=='g')
    {
        byte val = 0;

        Wire.beginTransmission(devadress);
        Wire.write((int)highByte(mem_cell_num)); 
        Wire.write((int)lowByte(mem_cell_num)); 
        Wire.write(val+4);
        Wire.endTransmission();
        delay(10);

        Wire.beginTransmission(devadress);
        Wire.write((int)highByte(mem_cell_num)); 
        Wire.write((int)lowByte(mem_cell_num)); 
        Wire.endTransmission();
        Wire.requestFrom(devadress,1); 

        while(Wire.available()==0);
        val=Wire.read();
        Serial.print(mem_cell_num);
        Serial.print(' ');
        Serial.println(val);  

        /*      
         EEPROM.write(ptr, val+4);
         val= EEPROM.read(ptr);
         
         if(val>0)
         {
         Serial.write(0xC0);
         Serial.write(val);  
         }
         */
    }
}

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.

Oh, sorry. There was a misprint. I meant 24LC256 .

How are the address pins wired?

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.

A0, A1, A2 connected each other and ground.

Can you describe what pull-up resistors you have connected to SDA and SCL?

4,7 kilohm

Let's see a photo of what you've done. A nice sharp one.