ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mpu6886.cpp
Go to the documentation of this file.
1#include "mpu6886.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mpu6886 {
5
6ESPHOME_LOG_TAG(TAG, "mpu6886");
7
8const uint8_t MPU6886_REGISTER_WHO_AM_I = 0x75;
10const uint8_t MPU6886_REGISTER_GYRO_CONFIG = 0x1B;
11const uint8_t MPU6886_REGISTER_ACCEL_CONFIG = 0x1C;
12const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H = 0x3B;
13const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO = 0b001;
14const uint8_t MPU6886_SCALE_2000_DPS = 0b11;
15const uint8_t MPU6886_WHO_AM_I_IDENTIFIER = 0x19;
16const float MPU6886_SCALE_DPS_PER_DIGIT_2000 = 0.060975f;
17const uint8_t MPU6886_RANGE_2G = 0b00;
18const float MPU6886_RANGE_PER_DIGIT_2G = 0.000061f;
19const uint8_t MPU6886_BIT_SLEEP_ENABLED = 6;
21const float GRAVITY_EARTH = 9.80665f;
22// See https://github.com/m5stack/M5-Schematic/blob/master/datasheet/MPU-6886-000193%2Bv1.1_GHIC.PDF.pdf
23// p. 43
24const float TEMPERATURE_SENSITIVITY = 326.8;
25const float TEMPERATURE_OFFSET = 25.0;
26
28 if (!this->setup_registers_())
29 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
30}
31
33 uint8_t who_am_i;
34 if (!this->read_byte(MPU6886_REGISTER_WHO_AM_I, &who_am_i) || who_am_i != MPU6886_WHO_AM_I_IDENTIFIER) {
35 return false;
36 }
37
38 ESP_LOGV(TAG, " Setting up Power Management");
39 // Clock source X-Gyro, sleep off, temperature on
40 if (!this->update_register(MPU6886_REGISTER_POWER_MANAGEMENT_1,
43 return false;
44 }
45
46 ESP_LOGV(TAG, " Setting up Gyroscope Config");
47 // Set scale - 2000DPS
48 if (!this->update_register(MPU6886_REGISTER_GYRO_CONFIG, 0b00011000, MPU6886_SCALE_2000_DPS << 3)) {
49 return false;
50 }
51
52 ESP_LOGV(TAG, " Setting up Accelerometer Config");
53 // Set range - 2G
54 return this->update_register(MPU6886_REGISTER_ACCEL_CONFIG, 0b00011000, MPU6886_RANGE_2G << 3);
55}
56
57void MPU6886Component::dump_config() {
58 ESP_LOGCONFIG(TAG, "MPU6886:");
59 LOG_I2C_DEVICE(this);
60 LOG_UPDATE_INTERVAL(this);
61 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
62 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
63 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
64 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
65 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
66 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
67 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
68}
69
70void MPU6886Component::update() {
71 ESP_LOGV(TAG, " Updating");
72 uint16_t raw_data[7];
73 if (!this->read_bytes_16(MPU6886_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
74 this->status_set_warning();
75 return;
76 }
77 auto *data = reinterpret_cast<int16_t *>(raw_data);
78
79 float accel_x = data[0] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
80 float accel_y = data[1] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
81 float accel_z = data[2] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
82
84
85 float gyro_x = data[4] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
86 float gyro_y = data[5] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
87 float gyro_z = data[6] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
88
89 ESP_LOGD(TAG,
90 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
91 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
92 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
93
94 PUBLISH_STATE(this->accel_x_sensor_, accel_x);
95 PUBLISH_STATE(this->accel_y_sensor_, accel_y);
96 PUBLISH_STATE(this->accel_z_sensor_, accel_z);
97
98 PUBLISH_STATE(this->temperature_sensor_, temperature);
99
100 PUBLISH_STATE(this->gyro_x_sensor_, gyro_x);
101 PUBLISH_STATE(this->gyro_y_sensor_, gyro_y);
102 PUBLISH_STATE(this->gyro_z_sensor_, gyro_z);
103
104 this->status_clear_warning();
105}
106
107} // namespace esphome::mpu6886
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:87
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool setup_registers_()
Configure the sensor; false on an I2C failure.
Definition mpu6886.cpp:32
const uint8_t MPU6886_REGISTER_WHO_AM_I
Definition mpu6886.cpp:8
const float TEMPERATURE_OFFSET
Definition mpu6886.cpp:25
const uint8_t MPU6886_BIT_TEMPERATURE_DISABLED
Definition mpu6886.cpp:20
ESPHOME_LOG_TAG(TAG, "mpu6886")
const uint8_t MPU6886_REGISTER_ACCEL_XOUT_H
Definition mpu6886.cpp:12
const uint8_t MPU6886_REGISTER_GYRO_CONFIG
Definition mpu6886.cpp:10
const float MPU6886_RANGE_PER_DIGIT_2G
Definition mpu6886.cpp:18
const uint8_t MPU6886_RANGE_2G
Definition mpu6886.cpp:17
const float MPU6886_SCALE_DPS_PER_DIGIT_2000
Definition mpu6886.cpp:16
const float TEMPERATURE_SENSITIVITY
Definition mpu6886.cpp:24
const uint8_t MPU6886_WHO_AM_I_IDENTIFIER
Definition mpu6886.cpp:15
const uint8_t MPU6886_BIT_SLEEP_ENABLED
Definition mpu6886.cpp:19
const uint8_t MPU6886_REGISTER_POWER_MANAGEMENT_1
Definition mpu6886.cpp:9
const uint8_t MPU6886_CLOCK_SOURCE_X_GYRO
Definition mpu6886.cpp:13
const uint8_t MPU6886_SCALE_2000_DPS
Definition mpu6886.cpp:14
const float GRAVITY_EARTH
Definition mpu6886.cpp:21
const uint8_t MPU6886_REGISTER_ACCEL_CONFIG
Definition mpu6886.cpp:11
uint16_t temperature
Definition sun_gtil2.cpp:12