13static constexpr uint32_t STATUS_CHECK_INTERVAL_ID = 0;
15static const uint16_t SCD30_CMD_GET_FIRMWARE_VERSION = 0xd100;
16static const uint16_t SCD30_CMD_START_CONTINUOUS_MEASUREMENTS = 0x0010;
17static const uint16_t SCD30_CMD_ALTITUDE_COMPENSATION = 0x5102;
18static const uint16_t SCD30_CMD_AUTOMATIC_SELF_CALIBRATION = 0x5306;
19static const uint16_t SCD30_CMD_GET_DATA_READY_STATUS = 0x0202;
20static const uint16_t SCD30_CMD_READ_MEASUREMENT = 0x0300;
23static const uint16_t SCD30_CMD_STOP_MEASUREMENTS = 0x0104;
24static const uint16_t SCD30_CMD_MEASUREMENT_INTERVAL = 0x4600;
25static const uint16_t SCD30_CMD_FORCED_CALIBRATION = 0x5204;
26static const uint16_t SCD30_CMD_TEMPERATURE_OFFSET = 0x5403;
27static const uint16_t SCD30_CMD_SOFT_RESET = 0xD304;
29void SCD30Component::setup() {
31 Wire.setClockStretchLimit(150000);
35 uint16_t raw_firmware_version[3];
36 if (!this->
get_register(SCD30_CMD_GET_FIRMWARE_VERSION, raw_firmware_version, 3)) {
37 this->
mark_failed(LOG_STR(
"Unable to read sensor firmware version"));
40 ESP_LOGD(TAG,
"SCD30 Firmware v%0d.%02d", (uint16_t(raw_firmware_version[0]) >> 8),
41 uint16_t(raw_firmware_version[0] & 0xFF));
50 if (!this->
write_command(SCD30_CMD_TEMPERATURE_OFFSET, temp_offset)) {
51 this->
mark_failed(LOG_STR(
"Error setting temperature offset"));
68 this->
mark_failed(LOG_STR(
"Error setting update interval"));
78 this->
mark_failed(LOG_STR(
"Error setting altitude compensation"));
87 this->
mark_failed(LOG_STR(
"Error setting automatic self calibration"));
96 this->
mark_failed(LOG_STR(
"Error starting continuous measurements"));
101 this->
set_interval(STATUS_CHECK_INTERVAL_ID, 500, [
this]() {
107void SCD30Component::dump_config() {
108 ESP_LOGCONFIG(TAG,
"scd30:");
109 LOG_I2C_DEVICE(
this);
111 ESP_LOGCONFIG(TAG,
" Altitude compensation: OFF");
116 " Automatic self calibration: %s\n"
117 " Ambient pressure compensation: %dmBar\n"
118 " Temperature offset: %.2f °C\n"
119 " Update interval: %ds",
122 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
123 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
124 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
127void SCD30Component::update() {
128 uint16_t raw_read_status;
129 if (!this->
read_data(raw_read_status) || raw_read_status == 0x00) {
131 ESP_LOGW(TAG,
"Data not ready yet!");
136 ESP_LOGW(TAG,
"Error reading measurement!");
142 uint16_t raw_data[6];
148 union uint32_float_t {
153 uint32_float_t co2{.uint32 = temp_c_o2_u32};
156 uint32_float_t
temperature{.uint32 = temp_temp_u32};
159 uint32_float_t humidity{.uint32 = temp_hum_u32};
161 ESP_LOGD(TAG,
"Got CO2=%.2fppm temperature=%.2f°C humidity=%.2f%%", co2.value,
temperature.value, humidity.value);
162 PUBLISH_STATE(this->co2_sensor_, co2.value);
163 PUBLISH_STATE(this->temperature_sensor_,
temperature.value);
164 PUBLISH_STATE(this->humidity_sensor_, humidity.value);
175 uint16_t is_data_ready;
176 if (!this->
read_data(&is_data_ready, 1)) {
179 return is_data_ready == 1;
182bool SCD30Component::force_recalibration_with_reference(uint16_t co2_reference) {
183 ESP_LOGD(TAG,
"Performing CO2 force recalibration with reference %dppm.", co2_reference);
184 if (this->
write_command(SCD30_CMD_FORCED_CALIBRATION, co2_reference)) {
185 ESP_LOGD(TAG,
"Force recalibration complete.");
188 ESP_LOGE(TAG,
"Failed to force recalibration with reference.");
194uint16_t SCD30Component::get_forced_calibration_reference() {
195 uint16_t forced_calibration_reference = 0;
197 if (!this->
get_register(SCD30_CMD_FORCED_CALIBRATION, forced_calibration_reference)) {
198 ESP_LOGE(TAG,
"Unable to read forced calibration reference.");
200 return forced_calibration_reference;