How to wait for next n bytes in I2C exchange? I'm trying to get data from a sensor, but don't see how I can get data back. I'm looking at the data on the I2C bus with a logic analyzer, and I'm sending what I expect, but I seem to cut off the slave before he can respond.  My understanding from the datasheet is that I need to send this command to wake it up and request a measurement:

0xD8 0x30 0x0A

and then I need to send this command to get the data:

0xD8 0x06 0xD9

#define HEARTBEAT_PIN            (13)

#define 7BIT_ADDR                (0x6C)
#define ADDR_WRITE               (0xD8)
#define ADDR_READ                (0xD9)

#define MAX_NUM_RX_BYTES         (8)
#define MAX_NUM_TX_BYTES         (8)


static uint8_t tx_buffer[MAX_NUM_TX_BYTES];
static uint8_t rx_buffer[MAX_NUM_RX_BYTES];
static uint8_t new_rx_data;

void setup(void) {
  // Clear any module memory
  memset(tx_buffer, 0, MAX_NUM_TX_BYTES);
  memset(rx_buffer, 0, MAX_NUM_RX_BYTES);
  new_rx_data = false;

  Wire.begin();  
}

void loop(void) {
  RequestMeasurement();
  delay(10);
  GetData();
  delay(500);
}

void RequestMeasurement(void) {
  // MR: 0xD8 0x30 0x0A
  // Internal (to IC) register address?
  tx_buffer[0] = 0x30;
  // Request-measurement command
  tx_buffer[1] = 0x0A;
  Wire.beginTransmission(7BIT_ADDR);
  Wire.write(tx_buffer, 2);
  Wire.endTransmission(true);
}

void GetData(void) {
  // GD: 0xD8 0x06 0xD9 0x?? 0x?? 0x?? 0x?? 0x?? (0x?? =&gt; slave data)
  // Slave address + 'write' indication bit
  // Internal (to IC) register address?
  tx_buffer[0] = 0x06;
  // Slave address + 'read' indication bit
  tx_buffer[1] = ADDR_READ;

  Wire.beginTransmission(7BIT_ADDR);
  Wire.write(tx_buffer, 2);
  Wire.endTransmission(true);

  // HOW DO I LEAVE THE BUS OPEN LONG ENOUGH
  // FOR THE SLAVE DEVICE TO RESPOND WITH THE DATA?
  //Wire.requestFrom(7BIT_ADDR, 3, true); 
}
First off; when talking about I2C bus, an important point to clarify: did you connect pullup resistors to both SDA and SCL lines (A4 and A5 respectively)? Without those, the I2C bus will not work properly.

Next, I2C protocol is quite low-level and a bit complex; but fortunately, the Wire library that comes with Arduino helps a lot and hides some of this complexity.

When using Wire library, there is a first point to keep in mind:

The device address is 7 bits only, the 8th bit is used to define the direction of data (read or write). You don't need to care about this direction bit as Wire deals with it for you.

In your situation, the device address is 0x6C, ie 1101100 in binary.

The commands you mentioned that you want to send to the device, 0xD8 0x30 0x0A and 0xD8 0x06 0xD9 already include the device address and the write bit (0):

0xD8 is 1101 1000 ie 1101100 plus the 0 direction bit at the end.

Please note that 0xD8 0x06 0xD9 is made of 2 commands actually:


0xD8 0x06 is a write command to tell the device hich register we want to read
0xD9 is just the device address followed by the read bit (1): this is the way the I2C master tells a slave that the former will now wait for data sent from the latter.


It is important to split this command in 2 because each will use a specific API from Wire library.

Writing to your device with the Wire library will be done this way:

// First command: start measurement
Wire.beginTransmission(0x6C);
Wire.write(0x30);
Wire.write(0x0A);
Wire.endTransmission();

// Second command: tell the device what register we want to read
Wire.beginTransmission(0x6C);
Wire.write(0x0A);
Wire.endTransmission();


Then you can start reading from your device with a different API:

Wire.requestFrom(0x6C, 3);
byte b1 = Wire.read();
byte b2 = Wire.read();
byte b3 = Wire.read();
// Now rebuild value from the 3 bytes just read...


Note that Wire.requestFrom(0x6C, 3); will send the 0xD9 that is calculated from the device address 0x6C and the read bit. SO you don't have (and you should not) send 0xD9 yourself, Wire doesit for you.

Please note that I use Wire.endTransmission() without the stop argument. Not sure if this is mandatory, but it worked fine on my I2C experiments.