0

I'm building a library to retrieve data from an adafruit sensor (sht31d)

The temperature reading appears accurate, but the humidity doesn't change.

My debug:

 temp: 23.36 humid: 0.39

The following method is called in main.c

(method from sht31.c):

bool readTempHum(void)
{
  uint8_t readbuffer[6];
  writeCommand(SHT31_MEAS_HIGHREP);
  _delay_ms(500);

  for (uint8_t i=0; i<6; i++)
  {
    readbuffer[i] = read8(SHT31_DEFAULT_ADDR);
  }

  uint16_t ST, SRH;
  ST = readbuffer[0];
  ST <<= 8;
  ST |= readbuffer[1];

  SRH = readbuffer[3];
  SRH <<= 8;
  SRH |= readbuffer[4];

  double stemp = ST;
  stemp *= 175;
  stemp /= 0xffff;
  stemp = -45 + stemp;

  temp = stemp;

  double shum = SRH;
  shum *= 100;
  shum /= 0xFFFF;

  humidity = shum;

  return true;
}

To view the full implementation please click here

Thanks

Womble
  • 345
  • 3
  • 13
  • "_far to consistent to be correct_", what is the expected output? – Linus May 19 '16 at 14:45
  • Is pin 2 of `sht31` connected to `VSS`? – LPs May 19 '16 at 14:52
  • @Linus Just more variation over time. If I place my finger on the sensor to make it warmer it changes value abruptly rather than lerping to the value. This is unusual from my experience with sensors. – Womble May 19 '16 at 14:54
  • @SensationSama have you wired your sensor like [this](http://johnny-five.io/img/breadboard/humidity-sht31d.png)? – Linus May 19 '16 at 14:56
  • @LPs Yes, pin 2 is grounded – Womble May 19 '16 at 14:56
  • Why did you commented the sleep before command and readout of values? Are you sure that sensor is not giving back a nack? – LPs May 19 '16 at 14:58
  • @LPs I wasn't importing the delay library. Just now I tried with the delay and it remains the same. – Womble May 19 '16 at 15:13

1 Answers1

1

It actually looks correct. Both temperature and humidity are encoded in two bytes and if you do all the calculations by hand it looks ok (in this case temperature only):

ST = readbuffer[0]; // 100
ST <<= 100; // 100 << 8 = 25600
ST |= readbuffer[1]; // 25600 | 1 = 25601

double stemp = ST; // 25601
stemp *= 175; // 25601 * 175 = 4480000
stemp /= 0xffff; // 4480000 / 65535 = 68,36
stemp = -45 + stemp; // -45 + 68,36 = 23,36

So it have nothing to do with ASCII encoding. Just give your sensor a bit of time.

Also if you look at the sensor datasheet you can see that its response time lies between 2 and 8 seconds.

DennisS
  • 253
  • 1
  • 15
  • Attempting to display the sensor data in a readable format is the ASCII issue. I suspect the temperature may be correct but the humidity always reports the same value, which is surely wrong. – Womble May 19 '16 at 15:05
  • In that case try to display it in hex: `sprintf(buf,"%x", readbuffer[i]);` – DennisS May 19 '16 at 15:07
  • I think are not correct. 3rd byte have to be CRC8, that cannot be 1 as value – LPs May 19 '16 at 15:09
  • %x results in `: 65: 1: 1: 1: 1: 1` – Womble May 19 '16 at 15:10
  • @LPs bytes 2 and 5 were used for CRC, but I removed that functionality – Womble May 19 '16 at 15:11
  • @SensationSama In that case try the simplest thing possible: breath on your sensor for a couple of seconds. Humidity should surely change. – DennisS May 19 '16 at 15:27
  • @LPs The original Adafruit library included `if (readbuffer[2] != crc8(readbuffer, 2)) return false;` I don't believe that removing this check would effect the data – Womble May 19 '16 at 20:39
  • @DennisS The humidity does not change at all when I do this. Otherwise I would have assumed it was working, indeed. – Womble May 19 '16 at 20:41
  • 1
    I meant that data printed by your loop cannot be correct because of 3rd byte is 1, that is obviously not the CRC8 of 0x65 0x01. – LPs May 19 '16 at 20:45
  • @LPs As stated in the OP `The resulting values can't be right` so back to square one. – Womble May 20 '16 at 15:31