I2C communication problem

I have interfaced gas sensor through I2C interface to arduino due board. When I compiled this program, I got all values 0. Please help me to read the sensor data. I have attached the sensor datasheet document. Below is my code what I have written to read the sensor data, if there is any mistake please give me the solution.

iAQ-core_Datasheet_EN_v1.pdf (613 KB)

sarathkumark77:
Hi All,

I have interfaced gas sensor through I2C interface to arduino due board. When I compiled this program, I got all values 0. Please help me to read the sensor data. I have attached the sensor datasheet document. Below is my code what I have written to read the sensor data, if there is any mistake please give me the solution.

#include "Wire.h"

#include <inttypes.h>
#define iaqaddress 0x5A
#define datareg 0xB5

void setup() {
Serial.begin(9600);
Wire.begin();
}

void loop()
{
uint16_t predict;
uint8_t statu;
int32_t resistance;
uint16_t tvoc;

Wire.beginTransmission(iaqaddress);
   Wire.write(datareg);
   Wire.endTransmission();
   Wire.requestFrom(iaqaddress,9);
   
      predict = (Wire.read()<< 8 | Wire.read();  
      statu = Wire.read();
      resistance = (Wire.read()& 0x00)| (Wire.read()<<16)| (Wire.read()<<8| Wire.read();
      tvoc = (Wire.read()<<8 | Wire.read();

Serial.println(predict);
       Serial.println(statu);
       Serial.println(resistance);
       Serial.println(tvoc);
       delay (1000);

}





Thanks in advance.

Regards,
sarathkumark77

Your problem is here:

       predict = (Wire.read()<< 8) | Wire.read();

Wire.read() returns a byte (8bits), when you take a byte and shift it left 8 bits you get zero.

   predict = Wire.read(); // store a byte into a word
   predict = (predict<<8)|Wire.read(); // shift the word value 8 bits left, add next byte

these two are wrong also:

  resistance = (Wire.read()& 0x00)| (Wire.read()<<16)| (Wire.read()<<8| Wire.read();
  tvoc = (Wire.read()<<8 | Wire.read();

try this instead:

  Wire.read();  // discard value
  resistance = Wire.read();
  resistance = (resistance <<8) | Wire.read();
  resistance = (resistance <<8) | Wire.read();
  tvoc = Wire.read();
  tvoc = (tvoc<<8) | Wire.read();

Chuck.