READ/WRITE to 24LC256 EEPROM via I2C bus?

edited December 2017 in Raspberry PI

Hello, after successfully talking to 3 MCP23017 IO expanders on the I2C bus of my RPi3, i went on to explore the world of external storage, specifically the 24LC256. I've been using the processing.io library, but i've had no luck in correct reading/writing of data onto the chip. Can this be done with this library or do i need to get to lower level wiring library stuff? I'm not really that experienced with the datasheet commands reading,

here's that piece of code so far :

   import processing.io.*;
    I2C compass;

    void setup() {
      compass = new I2C(I2C.list()[0]);

      compass.beginTransmission(0x51);
      compass.write(0x00);
      compass.write(0x00);
      compass.endTransmission();

      delay(100);

      compass.beginTransmission(0x51);
      compass.write(0x00);
      byte[] in = compass.read(1);
      println(in);
    }

and the result i get from this is 0x30 instead of 0x00...

any help would be much appreciated!! Vangelis

P.S. yes using i2cdetect shows me the eeprom being on the 0x51 address :D

Answers

  • Answer ✓

    Very technical question... not sure how much feedback you will get. I can suggest exploring the source code of the lib in GitHub. That is, if you want to put that extra effort and learn about the commands and function call structure and the actual library design. I think that could help. Don't forget to post in stackoverflow and if you do, you can cross-link your posts. Good luck!

    Kf

  • Lesson learned :D I tried to do the same thing with an arduino a few months back, i ended up reading and writing to an SD card building my own file system, took a lot of effort to build and i guess it's not worth it, i guess i'm better off using a usb memory stick and saving my stuff there (i really want a read-only fs on the pi!)

    thank you for your answer kfajer!

  • Answer ✓

    @vargos21

    What about the following?

    import processing.io.*;
    I2C eeprom;
    
    void setup() {
      erprom = new I2C(I2C.list()[0]);
    
      eeprom.beginTransmission(0x51);
      eeprom.write(0x00); // high-byte of address
      eeprom.write(0x00); // low-byte of address
      eeprom.write(0xAA); // data (test)
      eeprom.endTransmission();
    
      delay(100);
    
      eeprom.beginTransmission(0x51);
      eeprom.write(0x00); // high-byte of address
      eeprom.write(0x00); // low-byte of address
      byte[] in = eeprom.read(1);
      println(in);
    }
    
  • Answer ✓

    @vargos21 Does this actually work? (I haven't even compile-tested)

  • edited December 2017

    EXCELLENT as always gohai thank you! what would this world be without you!! :)

    not only did it work, but it also allows me to write more than one byte at a time (so writing a whole 64byte page will be faster than one-by-one byte :D )

    here's my sample code (gohai's general answer works perfectly!)

    import processing.io.*;
    I2C eeprom;
    
    void setup() {
      eeprom = new I2C(I2C.list()[0]);
    
      eeprom.beginTransmission(0x51);
      eeprom.write(0x00);
      eeprom.write(0x00);
      eeprom.write('H');
      eeprom.write('E');
      eeprom.write('L');
      eeprom.write('L');
      eeprom.write('O');
      eeprom.endTransmission();
    
      delay(100);
    
      eeprom.beginTransmission(0x51);
      eeprom.write(0x00);
      eeprom.write(0x00);
      char[] in = char(eeprom.read(5));
      println(in);
    }
    

    and the result of the code was:

    [0] 'H'
    [1] 'E'
    [2] 'L'
    [3] 'L'
    [4] 'O'
    

    thanks again! )))

Sign In or Register to comment.