Search code examples
arduinoi2c

I2C Data Conversion -- Arduino


Im trying to read the pressure from a sensor using I2C. Per the datasheet, I am writing a start command (0xAA) and then reading the data in the pressure registers as bytes. (Reference picture below):

Registers of Pressure Data

Here is the code that I am using:

#include <Adafruit_I2CDevice.h>

#define I2C_ADDRESS 0x29
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);


void setup() {
  while (!Serial) { delay(10); }
  Serial.begin(115200);
  Serial.println("I2C device read and write test");

  if (!i2c_dev.begin()) {
    Serial.print("Did not find device at 0x");
    Serial.println(i2c_dev.address(), HEX);
    while (1);
  }
  Serial.print("Device found on address 0x");
  Serial.println(i2c_dev.address(), HEX);
}

void loop(){
  uint8_t buffer[7];
  buffer[0] = 0xAA;
  i2c_dev.write_then_read(buffer, 1, buffer, 2, false);
  Serial.println();
  Serial.print(buffer[0], HEX);
  Serial.print(" ");
  Serial.print(buffer[1]);
  Serial.print(" ");
  Serial.print(buffer[2]);
  Serial.print(" ");
  Serial.print(buffer[3]);
  Serial.println();
  delay(500);
}

The first Register (buffer[0]) is the status register, which is supposed to return 0x40 if the operation succeeded without any errors. However, I am getting 0x60 when I read the first register. Is my code incorrect?

Here is the datasheet for the pressure sensor: https://www.farnell.com/datasheets/3208001.pdf


Solution

  • 0x40 is completed reading without error.

    In datasheet you can read that "When the Busy bit in the Status byte is zero, this indicate that valid data is ready, and a full Data Read of all 7 bytes may be performed"

    So i think you just need to wait some time (and poll status bit) and then read data.