ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ens160_base.cpp
Go to the documentation of this file.
1// ENS160 sensor with I2C interface from ScioSense
2//
3// Datasheet: https://www.sciosense.com/wp-content/uploads/documents/SC-001224-DS-7-ENS160-Datasheet.pdf
4//
5// Implementation based on:
6// https://github.com/sciosense/ENS160_driver
7
8// For best performance, the sensor shall be operated in normal indoor air in the range -5 to 60°C
9// (typical: 25°C); relative humidity: 20 to 80%RH (typical: 50%RH), non-condensing with no aggressive
10// or poisonous gases present. Prolonged exposure to environments outside these conditions can affect
11// performance and lifetime of the sensor.
12// The sensor is designed for indoor use and is not waterproof or dustproof. It should be protected from
13// water, condensation, dust, and aggressive gases. Note that the status will only be stored in non-volatile
14// memory after an initial 24 h of continuous operation. If unpowered before the conclusion of that period,
15// the ENS160 will resume "Initial Start-up" mode after re-powering.
16
17#include "ens160_base.h"
18#include "esphome/core/log.h"
19#include "esphome/core/hal.h"
20
22
23ESPHOME_LOG_TAG(TAG, "ens160");
24
25// Datasheet specifies 10ms, but some users report that 10ms is not sufficient for the
26// sensor to boot and be ready for commands. 11ms seems to be a safe value.
27static const uint8_t ENS160_BOOTING = 11;
28
29static const uint16_t ENS160_PART_ID = 0x0160;
30
31static const uint8_t ENS160_REG_PART_ID = 0x00;
32static const uint8_t ENS160_REG_OPMODE = 0x10;
33static const uint8_t ENS160_REG_CONFIG = 0x11;
34static const uint8_t ENS160_REG_COMMAND = 0x12;
35static const uint8_t ENS160_REG_TEMP_IN = 0x13;
36static const uint8_t ENS160_REG_DATA_STATUS = 0x20;
37static const uint8_t ENS160_REG_DATA_AQI = 0x21;
38static const uint8_t ENS160_REG_DATA_TVOC = 0x22;
39static const uint8_t ENS160_REG_DATA_ECO2 = 0x24;
40
41static const uint8_t ENS160_REG_GPR_READ_0 = 0x48;
42static const uint8_t ENS160_REG_GPR_READ_4 = ENS160_REG_GPR_READ_0 + 4;
43
44static const uint8_t ENS160_COMMAND_NOP = 0x00;
45static const uint8_t ENS160_COMMAND_CLRGPR = 0xCC;
46static const uint8_t ENS160_COMMAND_GET_APPVER = 0x0E;
47
48static const uint8_t ENS160_OPMODE_RESET = 0xF0;
49static const uint8_t ENS160_OPMODE_IDLE = 0x01;
50static const uint8_t ENS160_OPMODE_STD = 0x02;
51
52static const uint8_t ENS160_DATA_STATUS_STATAS = 0x80;
53static const uint8_t ENS160_DATA_STATUS_STATER = 0x40;
54static const uint8_t ENS160_DATA_STATUS_VALIDITY = 0x0C;
55static const uint8_t ENS160_DATA_STATUS_NEWDAT = 0x02;
56static const uint8_t ENS160_DATA_STATUS_NEWGPR = 0x01;
57
58// helps remove reserved bits in aqi data register
59static const uint8_t ENS160_DATA_AQI = 0x07;
60
62 // check part_id
63 uint16_t part_id;
64 if (!this->read_bytes(ENS160_REG_PART_ID, reinterpret_cast<uint8_t *>(&part_id), 2)) {
65 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
66 return;
67 }
68 if (part_id != ENS160_PART_ID) {
69 this->mark_failed(LOG_STR("Sensor reported an invalid ID. Is this a ENS160?"));
70 return;
71 }
72
73 // set mode to reset
74 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_RESET)) {
75 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
76 return;
77 }
78 delay(ENS160_BOOTING);
79
80 // check status
81 uint8_t status_value;
82 if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
83 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
84 return;
85 }
86 this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
87
88 if (this->validity_flag_ == INVALID_OUTPUT) {
89 this->mark_failed(LOG_STR("Invalid Device Status - No valid output"));
90 return;
91 }
92
93 // set mode to idle
94 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_IDLE)) {
95 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
96 return;
97 }
98 delay(ENS160_BOOTING);
99
100 // clear command
101 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_NOP)) {
102 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
103 return;
104 }
105 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_CLRGPR)) {
106 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
107 return;
108 }
109 delay(ENS160_BOOTING);
110
111 // read firmware version
112 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_GET_APPVER)) {
113 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
114 return;
115 }
116 delay(ENS160_BOOTING);
117
118 uint8_t version_data[3];
119 if (!this->read_bytes(ENS160_REG_GPR_READ_4, version_data, 3)) {
120 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
121 return;
122 }
123 this->firmware_ver_major_ = version_data[0];
124 this->firmware_ver_minor_ = version_data[1];
125 this->firmware_ver_build_ = version_data[2];
126
127 // set mode to standard
128 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_STD)) {
129 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
130 return;
131 }
132
133 // read opmode and check standard mode is achieved before finishing Setup
134 uint8_t op_mode;
135 if (!this->read_byte(ENS160_REG_OPMODE, &op_mode)) {
136 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
137 return;
138 }
139
140 if (op_mode != ENS160_OPMODE_STD) {
141 this->mark_failed(LOG_STR("Device failed to achieve Standard Operating Mode"));
142 return;
143 }
144}
145
147 uint8_t status_value, data_ready;
148
149 if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
150 ESP_LOGW(TAG, "Error reading status register");
151 this->status_set_warning();
152 return;
153 }
154
155 // verbose status logging
156 ESP_LOGV(TAG,
157 "ENS160 Status Register: 0x%02x\n"
158 " STATAS bit 0x%x\n"
159 " STATER bit 0x%x\n"
160 " VALIDITY FLAG 0x%02x\n"
161 " NEWDAT bit 0x%x\n"
162 " NEWGPR bit 0x%x",
163 status_value, (ENS160_DATA_STATUS_STATAS & (status_value)) == ENS160_DATA_STATUS_STATAS,
164 (ENS160_DATA_STATUS_STATER & (status_value)) == ENS160_DATA_STATUS_STATER,
165 (ENS160_DATA_STATUS_VALIDITY & status_value) >> 2,
166 (ENS160_DATA_STATUS_NEWDAT & (status_value)) == ENS160_DATA_STATUS_NEWDAT,
167 (ENS160_DATA_STATUS_NEWGPR & (status_value)) == ENS160_DATA_STATUS_NEWGPR);
168
169 data_ready = ENS160_DATA_STATUS_NEWDAT & status_value;
170 this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
171
172 switch (validity_flag_) {
173 case NORMAL_OPERATION:
174 if (data_ready != ENS160_DATA_STATUS_NEWDAT) {
175 ESP_LOGD(TAG, "ENS160 readings unavailable - Normal Operation but readings not ready");
176 return;
177 }
178 break;
179 case INITIAL_STARTUP:
180 if (!this->initial_startup_) {
181 this->initial_startup_ = true;
182 ESP_LOGI(TAG, "ENS160 readings unavailable - 1 hour startup required after first power on");
183 }
184 return;
185 case WARMING_UP:
186 if (!this->warming_up_) {
187 this->warming_up_ = true;
188 ESP_LOGI(TAG, "ENS160 readings not available yet - Warming up requires 3 minutes");
189 this->send_env_data_();
190 }
191 return;
192 case INVALID_OUTPUT:
193 ESP_LOGE(TAG, "ENS160 Invalid Status - No valid output");
194 this->status_set_warning();
195 return;
196 }
197
198 // read new data
199 uint16_t data_eco2;
200 if (!this->read_bytes(ENS160_REG_DATA_ECO2, reinterpret_cast<uint8_t *>(&data_eco2), 2)) {
201 ESP_LOGW(TAG, "Error reading eCO2 data register");
202 this->status_set_warning();
203 return;
204 }
205 if (this->co2_ != nullptr) {
206 this->co2_->publish_state(data_eco2);
207 }
208
209 uint16_t data_tvoc;
210 if (!this->read_bytes(ENS160_REG_DATA_TVOC, reinterpret_cast<uint8_t *>(&data_tvoc), 2)) {
211 ESP_LOGW(TAG, "Error reading TVOC data register");
212 this->status_set_warning();
213 return;
214 }
215 if (this->tvoc_ != nullptr) {
216 this->tvoc_->publish_state(data_tvoc);
217 }
218
219 uint8_t data_aqi;
220 if (!this->read_byte(ENS160_REG_DATA_AQI, &data_aqi)) {
221 ESP_LOGW(TAG, "Error reading AQI data register");
222 this->status_set_warning();
223 return;
224 }
225 if (this->aqi_ != nullptr) {
226 // remove reserved bits, just in case they are used in future
227 data_aqi = ENS160_DATA_AQI & data_aqi;
228 this->aqi_->publish_state(data_aqi);
229 }
230
231 this->status_clear_warning();
232
233 // set temperature and humidity compensation data
234 this->send_env_data_();
235}
236
238 if (this->temperature_ == nullptr && this->humidity_ == nullptr)
239 return;
240
241 float temperature = NAN;
242 if (this->temperature_ != nullptr)
243 temperature = this->temperature_->state;
244
245 if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
246 ESP_LOGW(TAG, "Invalid external temperature - compensation values not updated");
247 return;
248 } else {
249 ESP_LOGV(TAG, "External temperature compensation: %.1f°C", temperature);
250 }
251
252 float humidity = NAN;
253 if (this->humidity_ != nullptr)
254 humidity = this->humidity_->state;
255
256 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
257 ESP_LOGW(TAG, "Invalid external humidity - compensation values not updated");
258 return;
259 } else {
260 ESP_LOGV(TAG, "External humidity compensation: %.1f%%", humidity);
261 }
262
263 uint16_t t = (uint16_t) ((temperature + 273.15f) * 64.0f);
264 uint16_t h = (uint16_t) (humidity * 512.0f);
265
266 uint8_t data[4];
267 data[0] = t & 0xff;
268 data[1] = (t >> 8) & 0xff;
269 data[2] = h & 0xff;
270 data[3] = (h >> 8) & 0xff;
271
272 if (!this->write_bytes(ENS160_REG_TEMP_IN, data, 4)) {
273 ESP_LOGE(TAG, "Error writing compensation values");
274 this->status_set_warning();
275 return;
276 }
277}
278
280 ESP_LOGCONFIG(TAG, "ENS160:");
281 if (!this->is_failed()) {
282 ESP_LOGD(TAG, "Setup successful");
283 }
284 ESP_LOGI(TAG, "Firmware Version: %d.%d.%d", this->firmware_ver_major_, this->firmware_ver_minor_,
285 this->firmware_ver_build_);
286
287 LOG_UPDATE_INTERVAL(this);
288 LOG_SENSOR(" ", "CO2 Sensor:", this->co2_);
289 LOG_SENSOR(" ", "TVOC Sensor:", this->tvoc_);
290 LOG_SENSOR(" ", "AQI Sensor:", this->aqi_);
291
292 if (this->temperature_ != nullptr && this->humidity_ != nullptr) {
293 LOG_SENSOR(" ", " Temperature Compensation:", this->temperature_);
294 LOG_SENSOR(" ", " Humidity Compensation:", this->humidity_);
295 } else {
296 ESP_LOGCONFIG(TAG, " Compensation: Not configured");
297 }
298}
299
300} // namespace esphome::ens160_base
uint8_t h
Definition bl0906.h:2
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
void status_clear_warning()
Definition component.h:296
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
virtual bool write_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
enum esphome::ens160_base::ENS160Component::ValidityFlag validity_flag_
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
ESPHOME_LOG_TAG(TAG, "ens160")
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t temperature
Definition sun_gtil2.cpp:12