From 9ab6a7b7ff08f6101b9b17710b766fa9040bd613 Mon Sep 17 00:00:00 2001 From: Zixuan Wang Date: Thu, 21 Jan 2021 23:51:40 -0800 Subject: [PATCH] Improve ccs811 precision (#1428) --- esphome/components/ccs811/ccs811.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/ccs811/ccs811.cpp b/esphome/components/ccs811/ccs811.cpp index 538d7fe1f5..e7928d4d8b 100644 --- a/esphome/components/ccs811/ccs811.cpp +++ b/esphome/components/ccs811/ccs811.cpp @@ -102,10 +102,14 @@ void CCS811Component::send_env_data_() { // temperature has a 25° offset to allow negative temperatures temperature += 25; - // only 0.5 fractions are supported (application note) - auto hum_value = static_cast(roundf(humidity * 2)); - auto temp_value = static_cast(roundf(temperature * 2)); - this->write_bytes(0x05, {hum_value, 0x00, temp_value, 0x00}); + // At page 18 of: + // https://cdn.sparkfun.com/datasheets/BreakoutBoards/CCS811_Programming_Guide.pdf + // Reference code: + // https://github.com/adafruit/Adafruit_CCS811/blob/0990f5c620354d8bc087c4706bec091d8e6e5dfd/Adafruit_CCS811.cpp#L135-L142 + uint16_t hum_conv = static_cast(lroundf(humidity * 512.0f + 0.5f)); + uint16_t temp_conv = static_cast(lroundf(temperature * 512.0f + 0.5f)); + this->write_bytes(0x05, {(uint8_t)((hum_conv >> 8) & 0xff), (uint8_t)((hum_conv & 0xff)), + (uint8_t)((temp_conv >> 8) & 0xff), (uint8_t)((temp_conv & 0xff))}); } void CCS811Component::dump_config() { ESP_LOGCONFIG(TAG, "CCS811");