ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ms5611.cpp
Go to the documentation of this file.
1#include "ms5611.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::ms5611 {
6
7static const char *const TAG = "ms5611";
8
9static const uint8_t MS5611_ADDRESS = 0x77;
10static const uint8_t MS5611_CMD_ADC_READ = 0x00;
11static const uint8_t MS5611_CMD_RESET = 0x1E;
12static const uint8_t MS5611_CMD_CONV_D1 = 0x40;
13static const uint8_t MS5611_CMD_CONV_D2 = 0x50;
14static const uint8_t MS5611_CMD_READ_PROM = 0xA2;
15
17 if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) {
18 this->mark_failed();
19 return;
20 }
21 this->set_timeout(100, [this]() {
22 for (uint8_t offset = 0; offset < 6; offset++) {
23 if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) {
24 this->mark_failed();
25 return;
26 }
27 }
28 });
29}
31 ESP_LOGCONFIG(TAG, "MS5611:");
32 LOG_I2C_DEVICE(this);
33 if (this->is_failed()) {
34 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
35 }
36 LOG_UPDATE_INTERVAL(this);
37 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
38 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
39}
41 // request temperature reading
42 if (!this->write_bytes(MS5611_CMD_CONV_D2 + 0x08, nullptr, 0)) {
43 this->status_set_warning();
44 return;
45 }
46
47 this->set_timeout("temperature", 10, [this]() { this->read_temperature_(); });
48}
50 uint8_t bytes[3];
51 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
52 this->status_set_warning();
53 return;
54 }
55 const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
56
57 // request pressure reading
58 if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) {
59 this->status_set_warning();
60 return;
61 }
62
63 this->set_timeout("pressure", 10, [this, raw_temperature]() { this->read_pressure_(raw_temperature); });
64}
66 uint8_t bytes[3];
67 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
68 this->status_set_warning();
69 return;
70 }
71 const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
72 this->calculate_values_(raw_temperature, raw_pressure);
73}
74
75// Calculations are taken from the datasheet which can be found here:
76// https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Data+Sheet%7FMS5611-01BA03%7FB3%7Fpdf%7FEnglish%7FENG_DS_MS5611-01BA03_B3.pdf%7FCAT-BLPS0036
77// Sections PRESSURE AND TEMPERATURE CALCULATION and SECOND ORDER TEMPERATURE COMPENSATION
78// Variable names below match variable names from the datasheet but lowercased
79void MS5611Component::calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure) {
80 const uint32_t c1 = uint32_t(this->prom_[0]);
81 const uint32_t c2 = uint32_t(this->prom_[1]);
82 const uint16_t c3 = uint16_t(this->prom_[2]);
83 const uint16_t c4 = uint16_t(this->prom_[3]);
84 const int32_t c5 = int32_t(this->prom_[4]);
85 const uint16_t c6 = uint16_t(this->prom_[5]);
86 const uint32_t d1 = raw_pressure;
87 const int32_t d2 = raw_temperature;
88
89 // Promote dt to 64 bit here to make the math below cleaner
90 const int64_t dt = d2 - (c5 << 8);
91 int32_t temp = (2000 + ((dt * c6) >> 23));
92
93 int64_t off = (c2 << 16) + ((dt * c4) >> 7);
94 int64_t sens = (c1 << 15) + ((dt * c3) >> 8);
95
96 if (temp < 2000) {
97 const int32_t t2 = (dt * dt) >> 31;
98 int32_t off2 = ((5 * (temp - 2000) * (temp - 2000)) >> 1);
99 int32_t sens2 = ((5 * (temp - 2000) * (temp - 2000)) >> 2);
100 if (temp < -1500) {
101 off2 = (off2 + 7 * (temp + 1500) * (temp + 1500));
102 sens2 = sens2 + ((11 * (temp + 1500) * (temp + 1500)) >> 1);
103 }
104 temp = temp - t2;
105 off = off - off2;
106 sens = sens - sens2;
107 }
108
109 // Here we multiply unsigned 32-bit by signed 64-bit using signed 64-bit math.
110 // Possible ranges of D1 and SENS from the datasheet guarantee
111 // that this multiplication does not overflow
112 const int32_t p = ((((d1 * sens) >> 21) - off) >> 15);
113
114 const float temperature = temp / 100.0f;
115 const float pressure = p / 100.0f;
116 ESP_LOGD(TAG, "Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
117
118 if (this->temperature_sensor_ != nullptr)
119 this->temperature_sensor_->publish_state(temperature);
120 if (this->pressure_sensor_ != nullptr)
121 this->pressure_sensor_->publish_state(pressure); // hPa
122 this->status_clear_warning();
123}
124
125} // namespace esphome::ms5611
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
void read_pressure_(uint32_t raw_temperature)
Definition ms5611.cpp:65
sensor::Sensor * temperature_sensor_
Definition ms5611.h:23
sensor::Sensor * pressure_sensor_
Definition ms5611.h:24
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
Definition ms5611.cpp:79
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7