newbie Q on getting data from connected i2c device

Does anyone here know how to read data from an i2c device? I can't seem to figure it out. I'm looking at the i2c API here

https://mbientlab.com/androiddocs/latest/i2c.html

And the i2c interface info for my sensor here (page 9):

http://www.robotshop.com/media/files/pdf/MS5837-30BA.pdf

and it looks easy enough, but I am unsure of the values to use for
i2cModule.readData((byte) 0x1c, (byte) 0xd, (byte) 1).onComplete(new CompletionHandler<byte[]>()

Thanks in advance for any advice and help you can provide.

Comments

  • Based on page 9 of the pdf, the device address is 1110110x where x=1 for read and x=0 for write.  You can probably use 0x0 for register address, and set numBytes to 1.
  • Thank you Eric. 1110110x in hex is 0x76, so that's what I'm using here, correct? Still banging away for now. If you have any examples, or if anyone else has connected a i2c sensor to their metawear please share! I'll do the same when I get it working (but I'm pretty slow).

    i2cModule.readData((byte) 0x76, (byte) 0x0, (byte) 1).onComplete(new CompletionHandler<byte[]>()
  • No, the spec sheet states that the device address is in the form 1110110x where x=1 for read and x=0 for write, not the address is 0x76.
  • edited February 2017
    Thanks again Eric. I've got the read/write working for i2c on the board using your example code. But I do not have the same success with my i2c sensor. You don't have an i2c bus scanning util by any chance do you?

    I've also found some C and .java examples for that sensor, but based on different APIs. They write to 0xE1 to do an initial Reset (as per DS) and then read 2btyes from the Prom 0xA2, as per DS. The 7 bit device address ix 0x76, but I don't need that for metaware.

    How can I use i2cModule.writeData to write just one value?
    i2cModule.writeData((byte)0x1E) <-- I think I want this
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Here's how the other examples do it:
    {
    // Create I2C bus
    I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
    // Get I2C device, MS5837_30BA01 I2C address is 0x76
    I2CDevice device = Bus.getDevice(0x76);

    // Reset command
    device.write((byte)0x1E);
    Thread.sleep(500);

    byte[] data = new byte[2];
    // Read 12 bytes of calibration data
    // Read pressure sensitivity
    device.read(0xA2, data, 0, 2);
    int C1 = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF));




  • We do not have a i2c scanner.

    Based on the example java code, you try using 0x76 for deviceAddr, and 0x0 for registerAddr.
  • OK, thanks. Now, just wanted to close the loop on this... didn't work (it's where I started, actually). Ultimately, after eliminating all the other variables, my conclusion is that I hit the issue described here, http://community.mbientlab.com/discussion/comment/3304/#Comment_3304:

    " is
    that the MetaWear API Read performs a register read and not a raw read. 
    Basically, the register read operation first writes an address to read from, and then it reads."

    My sensors are i2c only (no SPI support) but I may look into http://community.mbientlab.com/discussion/comment/3350/#Comment_3350 or a different sensor.

    At the end of the day, though, it would be good if you could add support in the API for "raw" read/write, something like this:
    // Create I2C bus
    I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
    // Get I2C device, MS5837_30BA01 I2C address is 0x76
    I2CDevice device = Bus.getDevice(0x76);

    // Reset command
    device.write((byte)0x1E);

    byte[] data = new byte[2];
    // Read 12 bytes of calibration data
    // Read pressure sensitivity
    device.read(0xA2, data, 0, 2);
    That said, I tried everything I could, but if there's some kind of hack to achieve this from existing code, please let me know.

This discussion has been closed.