-->
Page 1 of 3

reading i2c device (air quality sensor)

PostPosted: Mon Oct 17, 2016 1:17 pm
by ErikLem
Recently I've bought 2 air quality sensors, a IAQ core-c and a IAQ core P. I was hoping to use them together with the ESP to see if they would give any usable output.

At first I connected them to a Arduino nano, to see if they would work. I found some code here :


Code: Select all#include "Wire.h"

#define iaqaddress 0x5A

uint16_t predict;
uint8_t statu;
int32_t resistance;
uint16_t tvoc;

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

void loop()
{     
  readAllBytes();
  checkStatus();

  Serial.print("CO2:");
  Serial.print(predict);
  Serial.print(", Status:");
  Serial.print(statu, HEX);
  Serial.print(", Resistance:");
  Serial.print(resistance);
  Serial.print(", TVoC:");
  Serial.println(tvoc);

  delay(2000);

}
void readAllBytes()
{
  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());
}
void checkStatus()
{
  if(statu == 0x10)
  {
    Serial.println("Warming up...");
  }
  else if(statu == 0x00)
  {
    Serial.println("Ready"); 
  }
  else if(statu == 0x01)
  {
    Serial.println("Busy"); 
  }
  else if(statu == 0x80)
  {
    Serial.println("Error"); 
  }
  else
  Serial.println("No Status, check module"); 
}


It worked like a charm, so i hooked the sensor to a esp-12 and uploaded the exact same code.
Unfortunately, with the ESP, I don't get any proper results from the sensors. The above code only gives me "No Status, check module", so aparantly, the module is not returning one of the expected statusses

Acording to the data sheet, it should give me one of these:

0x00: OK (data valid)
0x10: RUNIN (module in warm up phase)
0x01: BUSY (re-read multi byte data!)
0x80: ERROR (if constant: replace sensor)

I tried to go back to just a minimal script to get the status, the status should be the 3th byte returned by the sensor, so I wrote this:

Code: Select all#include "Wire.h"

#define iaqaddress 0x5A

uint8_t a;
uint8_t b;
uint8_t c;

void setup()
{
  Serial.begin(115200);
 
  Wire.begin();

  Wire.beginTransmission(iaqaddress);
  Wire.write(0xB5); // Write reset command
  Wire.endTransmission();
  delay(15);  // Default = 15ms
 
}

void loop()
{     

 Wire.requestFrom(iaqaddress, 3);
  delay(1000);
   
  a = Wire.read();
  b = Wire.read();
  c = Wire.read();

   Serial.println(a,HEX);
   Serial.println(b,HEX);
   Serial.println(c,HEX);

}


unfortunately, all I get is FF FF FF

Any idea why the sensor would work on the arduino, but not on the ESP?

If I run a i2c scanner, the sensor is detected with address 0x5A
both the arduino and the ESP feed it with 3.3v (as required)
Other i2c devices do work on the same ESP without any problems

Re: reading i2c device (air quality sensor)

PostPosted: Mon Oct 17, 2016 1:34 pm
by danbicks
You could try:

Wire.begin();
Wire.setClock(100000); // Force bus speed 100 Khz

With Wifi Scanner are the pins set for other than 4 & 5 default I2c pins or do you have the device on other pins? if so you would need to declare these!

Worth a check.

Dans

Re: reading i2c device (air quality sensor)

PostPosted: Tue Oct 18, 2016 1:20 pm
by ErikLem
I'm using the default pins (4 & 5)
I added the Wire.setClock(100000), but it did not change anything.

I also toyed around with the clock stretch limit, it seems like the problem is related to that area.
With Wire.setClockStretchLimit(1000) you're supposed to set it (had to upgrade the ESP arduino library from 2.0.0 to 2.3.0 before that command was recognized), but even with that I was not able to get a proper response from the module.
all the datasheet says about it is this:
Clock stretching pauses a transaction by holding the clock line
low. The transaction cannot continue until the line is released
to high again. Although the module could send the bytes of
data at a fast rate, it could happen that the module is busy at
the request time. It can then hold the clock line low after
reception and acknowledgement of a byte to force the master
into a wait state until the iAQ-Core module is ready for the next
byte transfer in a type of handshake procedure. (See official I²C
specification and user manual UM10204,
http://www.nxp.com/documents/user_manual/UM10204.pdf)


so I'm still a bit puzzled what to do next...

Re: reading i2c device (air quality sensor)

PostPosted: Tue Oct 18, 2016 1:51 pm
by danbicks
Just noticed!

Why such a big delay after request to read 3 bytes? does the data sheet request 1 second after a get reading or something??

Wire.requestFrom(iaqaddress, 3);
delay(1000);

Looks like a cool module, keep us posted

Dans