ESPHome 2026.5.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 {
6namespace ms5611 {
7
8static const char *const TAG = "ms5611";
9
10static const uint8_t MS5611_ADDRESS = 0x77;
11static const uint8_t MS5611_CMD_ADC_READ = 0x00;
12static const uint8_t MS5611_CMD_RESET = 0x1E;
13static const uint8_t MS5611_CMD_CONV_D1 = 0x40;
14static const uint8_t MS5611_CMD_CONV_D2 = 0x50;
15static const uint8_t MS5611_CMD_READ_PROM = 0xA2;
16
18 if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) {
19 this->mark_failed();
20 return;
21 }
22 this->set_timeout(100, [this]() {
23 for (uint8_t offset = 0; offset < 6; offset++) {
24 if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) {
25 this->mark_failed();
26 return;
27 }
28 }
29 });
30}
32 ESP_LOGCONFIG(TAG, "MS5611:");
33 LOG_I2C_DEVICE(this);
34 if (this->is_failed()) {
35 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
36 }
37 LOG_UPDATE_INTERVAL(this);
38 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
39 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
40}
42 // request temperature reading
43 if (!this->write_bytes(MS5611_CMD_CONV_D2 + 0x08, nullptr, 0)) {
44 this->status_set_warning();
45 return;
46 }
47
48 this->set_timeout("temperature", 10, [this]() { this->read_temperature_(); });
49}
51 uint8_t bytes[3];
52 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
53 this->status_set_warning();
54 return;
55 }
56 const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
57
58 // request pressure reading
59 if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) {
60 this->status_set_warning();
61 return;
62 }
63
64 this->set_timeout("pressure", 10, [this, raw_temperature]() { this->read_pressure_(raw_temperature); });
65}
67 uint8_t bytes[3];
68 if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
69 this->status_set_warning();
70 return;
71 }
72 const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
73 this->calculate_values_(raw_temperature, raw_pressure);
74}
75
76// Calculations are taken from the datasheet which can be found here:
77// 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
78// Sections PRESSURE AND TEMPERATURE CALCULATION and SECOND ORDER TEMPERATURE COMPENSATION
79// Variable names below match variable names from the datasheet but lowercased
80void MS5611Component::calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure) {
81 const uint32_t c1 = uint32_t(this->prom_[0]);
82 const uint32_t c2 = uint32_t(this->prom_[1]);
83 const uint16_t c3 = uint16_t(this->prom_[2]);
84 const uint16_t c4 = uint16_t(this->prom_[3]);
85 const int32_t c5 = int32_t(this->prom_[4]);
86 const uint16_t c6 = uint16_t(this->prom_[5]);
87 const uint32_t d1 = raw_pressure;
88 const int32_t d2 = raw_temperature;
89
90 // Promote dt to 64 bit here to make the math below cleaner
91 const int64_t dt = d2 - (c5 << 8);
92 int32_t temp = (2000 + ((dt * c6) >> 23));
93
94 int64_t off = (c2 << 16) + ((dt * c4) >> 7);
95 int64_t sens = (c1 << 15) + ((dt * c3) >> 8);
96
97 if (temp < 2000) {
98 const int32_t t2 = (dt * dt) >> 31;
99 int32_t off2 = ((5 * (temp - 2000) * (temp - 2000)) >> 1);
100 int32_t sens2 = ((5 * (temp - 2000) * (temp - 2000)) >> 2);
101 if (temp < -1500) {
102 off2 = (off2 + 7 * (temp + 1500) * (temp + 1500));
103 sens2 = sens2 + ((11 * (temp + 1500) * (temp + 1500)) >> 1);
104 }
105 temp = temp - t2;
106 off = off - off2;
107 sens = sens - sens2;
108 }
109
110 // Here we multiply unsigned 32-bit by signed 64-bit using signed 64-bit math.
111 // Possible ranges of D1 and SENS from the datasheet guarantee
112 // that this multiplication does not overflow
113 const int32_t p = ((((d1 * sens) >> 21) - off) >> 15);
114
115 const float temperature = temp / 100.0f;
116 const float pressure = p / 100.0f;
117 ESP_LOGD(TAG, "Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
118
119 if (this->temperature_sensor_ != nullptr)
120 this->temperature_sensor_->publish_state(temperature);
121 if (this->pressure_sensor_ != nullptr)
122 this->pressure_sensor_->publish_state(pressure); // hPa
123 this->status_clear_warning();
124}
125
126} // namespace ms5611
127} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
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:510
void status_clear_warning()
Definition component.h:306
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:66
sensor::Sensor * temperature_sensor_
Definition ms5611.h:24
sensor::Sensor * pressure_sensor_
Definition ms5611.h:25
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure)
Definition ms5611.cpp:80
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7