ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mpu6050.cpp
Go to the documentation of this file.
1#include "mpu6050.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mpu6050 {
5
6static const char *const TAG = "mpu6050";
7
8const uint8_t MPU6050_REGISTER_WHO_AM_I = 0x75;
10const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;
11const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;
12const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;
13const uint8_t MPU6050_CLOCK_SOURCE_X_GYRO = 0b001;
14const uint8_t MPU6050_SCALE_2000_DPS = 0b11;
15const float MPU6050_SCALE_DPS_PER_DIGIT_2000 = 0.060975f;
16const uint8_t MPU6050_RANGE_2G = 0b00;
17const float MPU6050_RANGE_PER_DIGIT_2G = 0.000061f;
18const uint8_t MPU6050_BIT_SLEEP_ENABLED = 6;
20const float GRAVITY_EARTH = 9.80665f;
21
23 uint8_t who_am_i;
24 if (!this->read_byte(MPU6050_REGISTER_WHO_AM_I, &who_am_i) ||
25 (who_am_i != 0x68 && who_am_i != 0x70 && who_am_i != 0x98)) {
26 this->mark_failed();
27 return;
28 }
29
30 ESP_LOGV(TAG, " Setting up Power Management");
31 // Setup power management
32 uint8_t power_management;
33 if (!this->read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
34 this->mark_failed();
35 return;
36 }
37 ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
38 // Set clock source - X-Gyro
39 power_management &= 0b11111000;
40 power_management |= MPU6050_CLOCK_SOURCE_X_GYRO;
41 // Disable sleep
42 power_management &= ~(1 << MPU6050_BIT_SLEEP_ENABLED);
43 // Enable temperature
44 power_management &= ~(1 << MPU6050_BIT_TEMPERATURE_DISABLED);
45 ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
46 if (!this->write_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, power_management)) {
47 this->mark_failed();
48 return;
49 }
50
51 ESP_LOGV(TAG, " Setting up Gyro Config");
52 // Set scale - 2000DPS
53 uint8_t gyro_config;
54 if (!this->read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) {
55 this->mark_failed();
56 return;
57 }
58 ESP_LOGV(TAG, " Input gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
59 gyro_config &= 0b11100111;
60 gyro_config |= MPU6050_SCALE_2000_DPS << 3;
61 ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
62 if (!this->write_byte(MPU6050_REGISTER_GYRO_CONFIG, gyro_config)) {
63 this->mark_failed();
64 return;
65 }
66
67 ESP_LOGV(TAG, " Setting up Accel Config");
68 // Set range - 2G
69 uint8_t accel_config;
70 if (!this->read_byte(MPU6050_REGISTER_ACCEL_CONFIG, &accel_config)) {
71 this->mark_failed();
72 return;
73 }
74 ESP_LOGV(TAG, " Input accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
75 accel_config &= 0b11100111;
76 accel_config |= (MPU6050_RANGE_2G << 3);
77 ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
78 if (!this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config)) {
79 this->mark_failed();
80 return;
81 }
82}
84 ESP_LOGCONFIG(TAG, "MPU6050:");
85 LOG_I2C_DEVICE(this);
86 if (this->is_failed()) {
87 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
88 }
89 LOG_UPDATE_INTERVAL(this);
90 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
91 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
92 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
93 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
94 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
95 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
96 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
97}
98
100 ESP_LOGV(TAG, "Updating");
101 uint16_t raw_data[7];
102 if (!this->read_bytes_16(MPU6050_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
103 this->status_set_warning();
104 return;
105 }
106 auto *data = reinterpret_cast<int16_t *>(raw_data);
107
108 float accel_x = data[0] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
109 float accel_y = data[1] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
110 float accel_z = data[2] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
111
112 float temperature = data[3] / 340.0f + 36.53f;
113
114 float gyro_x = data[4] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
115 float gyro_y = data[5] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
116 float gyro_z = data[6] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
117
118 ESP_LOGD(TAG,
119 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
120 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
121 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
122
123 if (this->accel_x_sensor_ != nullptr)
124 this->accel_x_sensor_->publish_state(accel_x);
125 if (this->accel_y_sensor_ != nullptr)
126 this->accel_y_sensor_->publish_state(accel_y);
127 if (this->accel_z_sensor_ != nullptr)
128 this->accel_z_sensor_->publish_state(accel_z);
129
130 if (this->temperature_sensor_ != nullptr)
131 this->temperature_sensor_->publish_state(temperature);
132
133 if (this->gyro_x_sensor_ != nullptr)
134 this->gyro_x_sensor_->publish_state(gyro_x);
135 if (this->gyro_y_sensor_ != nullptr)
136 this->gyro_y_sensor_->publish_state(gyro_y);
137 if (this->gyro_z_sensor_ != nullptr)
138 this->gyro_z_sensor_->publish_state(gyro_z);
139
140 this->status_clear_warning();
141}
142
143} // namespace esphome::mpu6050
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
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_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:53
sensor::Sensor * accel_y_sensor_
Definition mpu6050.h:26
sensor::Sensor * accel_x_sensor_
Definition mpu6050.h:25
sensor::Sensor * gyro_x_sensor_
Definition mpu6050.h:29
sensor::Sensor * temperature_sensor_
Definition mpu6050.h:28
sensor::Sensor * gyro_y_sensor_
Definition mpu6050.h:30
sensor::Sensor * accel_z_sensor_
Definition mpu6050.h:27
sensor::Sensor * gyro_z_sensor_
Definition mpu6050.h:31
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
const uint8_t MPU6050_REGISTER_GYRO_CONFIG
Definition mpu6050.cpp:10
const uint8_t MPU6050_REGISTER_ACCEL_CONFIG
Definition mpu6050.cpp:11
const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H
Definition mpu6050.cpp:12
const uint8_t MPU6050_RANGE_2G
Definition mpu6050.cpp:16
const uint8_t MPU6050_SCALE_2000_DPS
Definition mpu6050.cpp:14
const float GRAVITY_EARTH
Definition mpu6050.cpp:20
const uint8_t MPU6050_REGISTER_POWER_MANAGEMENT_1
Definition mpu6050.cpp:9
const uint8_t MPU6050_REGISTER_WHO_AM_I
Definition mpu6050.cpp:8
const uint8_t MPU6050_BIT_TEMPERATURE_DISABLED
Definition mpu6050.cpp:19
const float MPU6050_RANGE_PER_DIGIT_2G
Definition mpu6050.cpp:17
const float MPU6050_SCALE_DPS_PER_DIGIT_2000
Definition mpu6050.cpp:15
const uint8_t MPU6050_CLOCK_SOURCE_X_GYRO
Definition mpu6050.cpp:13
const uint8_t MPU6050_BIT_SLEEP_ENABLED
Definition mpu6050.cpp:18
uint16_t temperature
Definition sun_gtil2.cpp:12