ESPHome 2026.6.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
6
7static const char *const TAG = "hmc5883l";
8static const uint8_t HMC5883L_ADDRESS = 0x1E;
9static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
10static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
11static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
12static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
13static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
14static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
15static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
16static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
17static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
18static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
19static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
20static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
21static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_C = 0x0C;
22
24 uint8_t id[3];
25 if (!this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_A, &id[0]) ||
26 !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_B, &id[1]) ||
27 !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_C, &id[2])) {
28 this->error_code_ = COMMUNICATION_FAILED;
29 this->mark_failed();
30 return;
31 }
32
35 }
36
37 if (id[0] != 0x48 || id[1] != 0x34 || id[2] != 0x33) {
38 this->error_code_ = ID_REGISTERS;
39 this->mark_failed();
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->error_code_ = COMMUNICATION_FAILED;
49 this->mark_failed();
50 return;
51 }
52
53 uint8_t config_b = 0;
54 config_b |= this->range_ << 5;
55 if (!this->write_byte(HMC5883L_REGISTER_CONFIG_B, config_b)) {
56 this->error_code_ = COMMUNICATION_FAILED;
57 this->mark_failed();
58 return;
59 }
60
61 uint8_t mode = 0;
62 // Continuous Measurement Mode
63 mode |= 0b00;
64 if (!this->write_byte(HMC5883L_REGISTER_MODE, mode)) {
65 this->error_code_ = COMMUNICATION_FAILED;
66 this->mark_failed();
67 return;
68 }
69}
71 ESP_LOGCONFIG(TAG, "HMC5883L:");
72 LOG_I2C_DEVICE(this);
73 if (this->error_code_ == COMMUNICATION_FAILED) {
74 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
75 } else if (this->error_code_ == ID_REGISTERS) {
76 ESP_LOGE(TAG, "The ID registers don't match - Is this really an HMC5883L?");
77 }
78 LOG_UPDATE_INTERVAL(this);
79
80 LOG_SENSOR(" ", "X Axis", this->x_sensor_);
81 LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
82 LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
83 LOG_SENSOR(" ", "Heading", this->heading_sensor_);
84}
86 uint16_t raw_x, raw_y, raw_z;
87 if (!this->read_byte_16(HMC5883L_REGISTER_DATA_X_MSB, &raw_x) ||
88 !this->read_byte_16(HMC5883L_REGISTER_DATA_Y_MSB, &raw_y) ||
89 !this->read_byte_16(HMC5883L_REGISTER_DATA_Z_MSB, &raw_z)) {
90 this->status_set_warning();
91 return;
92 }
93
94 float mg_per_bit;
95 switch (this->range_) {
97 mg_per_bit = 0.73f;
98 break;
100 mg_per_bit = 0.92f;
101 break;
103 mg_per_bit = 1.22f;
104 break;
106 mg_per_bit = 1.52f;
107 break;
109 mg_per_bit = 2.27f;
110 break;
112 mg_per_bit = 2.56f;
113 break;
115 mg_per_bit = 3.03f;
116 break;
118 mg_per_bit = 4.35f;
119 break;
120 default:
121 mg_per_bit = NAN;
122 }
123
124 // in µT
125 const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
126 const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
127 const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
128
129 float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
130 ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
131
132 if (this->x_sensor_ != nullptr)
133 this->x_sensor_->publish_state(x);
134 if (this->y_sensor_ != nullptr)
135 this->y_sensor_->publish_state(y);
136 if (this->z_sensor_ != nullptr)
137 this->z_sensor_->publish_state(z);
138 if (this->heading_sensor_ != nullptr)
139 this->heading_sensor_->publish_state(heading);
140}
141
142} // 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:729
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
HighFrequencyLoopRequester high_freq_
Definition hmc5883l.h:64
HMC5883LOversampling oversampling_
Definition hmc5883l.h:52
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool z
Definition msa3xx.h:1
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