ESPHome 2026.11.0-dev
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 // Setup power management
40 uint8_t power_management;
41 if (!this->read_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
42 return false;
43 }
44 ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
45 // Set clock source - X-Gyro
46 power_management &= 0b11111000;
47 power_management |= MPU6886_CLOCK_SOURCE_X_GYRO;
48 // Disable sleep
49 power_management &= ~(1 << MPU6886_BIT_SLEEP_ENABLED);
50 // Enable temperature
51 power_management &= ~(1 << MPU6886_BIT_TEMPERATURE_DISABLED);
52 ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
53 if (!this->write_byte(MPU6886_REGISTER_POWER_MANAGEMENT_1, power_management)) {
54 return false;
55 }
56
57 ESP_LOGV(TAG, " Setting up Gyroscope Config");
58 // Set scale - 2000DPS
59 uint8_t gyro_config;
60 if (!this->read_byte(MPU6886_REGISTER_GYRO_CONFIG, &gyro_config)) {
61 return false;
62 }
63 ESP_LOGV(TAG, " Input gyroscope_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
64 gyro_config &= 0b11100111;
65 gyro_config |= MPU6886_SCALE_2000_DPS << 3;
66 ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
67 if (!this->write_byte(MPU6886_REGISTER_GYRO_CONFIG, gyro_config)) {
68 return false;
69 }
70
71 ESP_LOGV(TAG, " Setting up Accelerometer Config");
72 // Set range - 2G
73 uint8_t accel_config;
74 if (!this->read_byte(MPU6886_REGISTER_ACCEL_CONFIG, &accel_config)) {
75 return false;
76 }
77 ESP_LOGV(TAG, " Input accelerometer_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
78 accel_config &= 0b11100111;
79 accel_config |= (MPU6886_RANGE_2G << 3);
80 ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
81 return this->write_byte(MPU6886_REGISTER_ACCEL_CONFIG, accel_config);
82}
83
85 ESP_LOGCONFIG(TAG, "MPU6886:");
86 LOG_I2C_DEVICE(this);
87 LOG_UPDATE_INTERVAL(this);
88 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
89 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
90 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
91 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
92 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
93 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
94 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
95}
96
98 ESP_LOGV(TAG, " Updating");
99 uint16_t raw_data[7];
100 if (!this->read_bytes_16(MPU6886_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
101 this->status_set_warning();
102 return;
103 }
104 auto *data = reinterpret_cast<int16_t *>(raw_data);
105
106 float accel_x = data[0] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
107 float accel_y = data[1] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
108 float accel_z = data[2] * MPU6886_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
109
111
112 float gyro_x = data[4] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
113 float gyro_y = data[5] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
114 float gyro_z = data[6] * MPU6886_SCALE_DPS_PER_DIGIT_2000;
115
116 ESP_LOGD(TAG,
117 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
118 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
119 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
120
121 if (this->accel_x_sensor_ != nullptr)
122 this->accel_x_sensor_->publish_state(accel_x);
123 if (this->accel_y_sensor_ != nullptr)
124 this->accel_y_sensor_->publish_state(accel_y);
125 if (this->accel_z_sensor_ != nullptr)
126 this->accel_z_sensor_->publish_state(accel_z);
127
128 if (this->temperature_sensor_ != nullptr)
129 this->temperature_sensor_->publish_state(temperature);
130
131 if (this->gyro_x_sensor_ != nullptr)
132 this->gyro_x_sensor_->publish_state(gyro_x);
133 if (this->gyro_y_sensor_ != nullptr)
134 this->gyro_y_sensor_->publish_state(gyro_y);
135 if (this->gyro_z_sensor_ != nullptr)
136 this->gyro_z_sensor_->publish_state(gyro_z);
137
138 this->status_clear_warning();
139}
140
141} // namespace esphome::mpu6886
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
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_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:70
sensor::Sensor * gyro_x_sensor_
Definition mpu6886.h:31
sensor::Sensor * accel_y_sensor_
Definition mpu6886.h:28
sensor::Sensor * accel_z_sensor_
Definition mpu6886.h:29
bool setup_registers_()
Configure the sensor; false on an I2C failure.
Definition mpu6886.cpp:32
sensor::Sensor * temperature_sensor_
Definition mpu6886.h:30
sensor::Sensor * gyro_z_sensor_
Definition mpu6886.h:33
sensor::Sensor * accel_x_sensor_
Definition mpu6886.h:27
sensor::Sensor * gyro_y_sensor_
Definition mpu6886.h:32
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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