I2C Force data readings

I am attempting to get readings off a sensor using an Arduino 2560 using a certain I2C communication protocol described in the picture however my code only displays a single value of 130 regardless of what the code actually is. The sensor can pick up temperature and force. The address of the force's data is the 0x57 and the sensor address is 00000001. How do I remedy this? Any help is appreciated!
My code:

#include <Wire.h>

void setup() {
  Serial.begin(9600);                // begin serial comm. 
  Wire.begin();                      //join i2c bus as master 
}

void loop() {
  Serial.println("Force Reading:"); 
  Wire.beginTransmission(0x01);      // Establish contact with 00000001b
  Wire.write(0x57);                  // Tell 00000001b to retrieve data type 0x57
  Wire.endTransmission();            //Ends transmission.
  Wire.requestFrom(0x01, 5);         // Transfer data type 0x57 to Arduino

    int data = Wire.read();          // Read the transferred data
    Serial.println(data);            // Print the data to the serial monitor

  delay(1000);
}

It looks like you are close. You only read 1 byte, so you only get the fist value and 0x82 = 130.
First off, when setting the register address, the spec shows no STOP so you should do (although, since you are getting the correct first byte, it doesn't really matter)

Wire.beginTransmission(0x01); 
  Wire.write(0x57);
  Wire.endTransmission(false);            //no STOP
  Wire.requestFrom(0x01, 5);         // Transfer data type 0x57 to Arduino

Then, you need to wait for the data to come back, it won't happen instantaneously

while ( Wire.available < 5 ) {
  // wait
}

Then, you can read in your 5 bytes and it looks like your data will be the last 4 bytes

byte data[5];
for(int i=0; i < 5; ++i ) {
  data[i] = Wire.read();
  Serial.println(data[i], HEX );
}
long value = data[4] + data[3] << 8 + data[2] << 16 + data[1] << 24;

0x01 is not a valid I2C slave address. Consult the data sheet and run the IDE example I2C_sniffer to verify the real slave address of your device.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.