adafruit / Adafruit_CCS811

Arduino driver for CCS811 digital gas sensor
MIT License
65 stars 36 forks source link

Wrong bits are cleared in the raw ADC reading #26

Closed adams13x13 closed 3 years ago

adams13x13 commented 3 years ago

A bug

The two upper bits get lost in the raw ADC reading of the sensor. Function Adafruit_CCS811::readData() in the file Adafruit_CCS811.cpp:103, released version 1.0.4.

_rawADCreading = (((uint16_t)buf[6] << 8) & 3) | ((uint16_t)buf[7]);

Correct solution

_rawADCreading = ((uint16_t)(buf[6] & 3) << 8) | ((uint16_t)buf[7]);

Explanation

What was meant, is to take the lowest 2 bits of the buf[6], shift left and or with the value buf[7]. But what is written, is to take buf[6], shift left, take the lowest two bits (that are zero) and then or with the buf[7].