ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
hmc5883l.cpp
Go to the documentation of this file.
1#include "hmc5883l.h"
2#include "esphome/core/log.h"
4
5#include <numbers>
6
8
9ESPHOME_LOG_TAG(TAG, "hmc5883l");
10static const uint8_t HMC5883L_ADDRESS = 0x1E;
11static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
12static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
13static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
14static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
15static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
16static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
17static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
18static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
19static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
20static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
21static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
22static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
23static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_C = 0x0C;
24
26 uint8_t id[3];
27 if (!this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_A, &id[0]) ||
28 !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_B, &id[1]) ||
29 !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_C, &id[2])) {
30 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
31 return;
32 }
33
36 }
37
38 if (id[0] != 0x48 || id[1] != 0x34 || id[2] != 0x33) {
39 this->mark_failed(LOG_STR("The ID registers don't match - Is this really an HMC5883L?"));
40 return;
41 }
42
43 uint8_t config_a = 0;
44 config_a |= this->oversampling_ << 5;
45 config_a |= this->datarate_ << 2;
46 config_a |= 0b0 << 0; // Measurement Mode: Normal(high impedance on load)
47 if (!this->write_byte(HMC5883L_REGISTER_CONFIG_A, config_a)) {
48 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
49 return;
50 }
51
52 uint8_t config_b = 0;
53 config_b |= this->range_ << 5;
54 if (!this->write_byte(HMC5883L_REGISTER_CONFIG_B, config_b)) {
55 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
56 return;
57 }
58
59 uint8_t mode = 0;
60 // Continuous Measurement Mode
61 mode |= 0b00;
62 if (!this->write_byte(HMC5883L_REGISTER_MODE, mode)) {
63 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
64 return;
65 }
66}
68 ESP_LOGCONFIG(TAG, "HMC5883L:");
69 LOG_I2C_DEVICE(this);
70 LOG_UPDATE_INTERVAL(this);
71
72 LOG_SENSOR(" ", "X Axis", this->x_sensor_);
73 LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
74 LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
75 LOG_SENSOR(" ", "Heading", this->heading_sensor_);
76}
78 uint16_t raw_x, raw_y, raw_z;
79 if (!this->read_byte_16(HMC5883L_REGISTER_DATA_X_MSB, &raw_x) ||
80 !this->read_byte_16(HMC5883L_REGISTER_DATA_Y_MSB, &raw_y) ||
81 !this->read_byte_16(HMC5883L_REGISTER_DATA_Z_MSB, &raw_z)) {
82 this->status_set_warning();
83 return;
84 }
85
86 float mg_per_bit;
87 switch (this->range_) {
89 mg_per_bit = 0.73f;
90 break;
92 mg_per_bit = 0.92f;
93 break;
95 mg_per_bit = 1.22f;
96 break;
98 mg_per_bit = 1.52f;
99 break;
101 mg_per_bit = 2.27f;
102 break;
104 mg_per_bit = 2.56f;
105 break;
107 mg_per_bit = 3.03f;
108 break;
110 mg_per_bit = 4.35f;
111 break;
112 default:
113 mg_per_bit = NAN;
114 }
115
116 // in µT
117 const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
118 const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
119 const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
120
121 float heading = atan2f(0.0f - x, y) * 180.0f / std::numbers::pi_v<float>;
122 ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
123
124 if (this->x_sensor_ != nullptr)
125 this->x_sensor_->publish_state(x);
126 if (this->y_sensor_ != nullptr)
127 this->y_sensor_->publish_state(y);
128 if (this->z_sensor_ != nullptr)
129 this->z_sensor_->publish_state(z);
130 if (this->heading_sensor_ != nullptr)
131 this->heading_sensor_->publish_state(heading);
132}
133
134} // namespace esphome::hmc5883l
BedjetMode mode
BedJet operating mode.
uint32_t get_loop_interval() const
void mark_failed()
Mark this component as failed.
void start()
Start running the loop continuously.
Definition helpers.cpp:835
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
HighFrequencyLoopRequester high_freq_
Definition hmc5883l.h:59
HMC5883LOversampling oversampling_
Definition hmc5883l.h:52
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
bool z
Definition msa3xx.h:1
ESPHOME_LOG_TAG(TAG, "hmc5883l")
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6