ESPHome 2026.11.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
6ESPHOME_LOG_TAG(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 if (!this->setup_registers_())
24 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
25}
26
28 uint8_t who_am_i;
29 if (!this->read_byte(MPU6050_REGISTER_WHO_AM_I, &who_am_i) ||
30 (who_am_i != 0x68 && who_am_i != 0x70 && who_am_i != 0x98)) {
31 return false;
32 }
33
34 ESP_LOGV(TAG, " Setting up Power Management");
35 // Setup power management
36 uint8_t power_management;
37 if (!this->read_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, &power_management)) {
38 return false;
39 }
40 ESP_LOGV(TAG, " Input power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
41 // Set clock source - X-Gyro
42 power_management &= 0b11111000;
43 power_management |= MPU6050_CLOCK_SOURCE_X_GYRO;
44 // Disable sleep
45 power_management &= ~(1 << MPU6050_BIT_SLEEP_ENABLED);
46 // Enable temperature
47 power_management &= ~(1 << MPU6050_BIT_TEMPERATURE_DISABLED);
48 ESP_LOGV(TAG, " Output power_management: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(power_management));
49 if (!this->write_byte(MPU6050_REGISTER_POWER_MANAGEMENT_1, power_management)) {
50 return false;
51 }
52
53 ESP_LOGV(TAG, " Setting up Gyro Config");
54 // Set scale - 2000DPS
55 uint8_t gyro_config;
56 if (!this->read_byte(MPU6050_REGISTER_GYRO_CONFIG, &gyro_config)) {
57 return false;
58 }
59 ESP_LOGV(TAG, " Input gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
60 gyro_config &= 0b11100111;
61 gyro_config |= MPU6050_SCALE_2000_DPS << 3;
62 ESP_LOGV(TAG, " Output gyro_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(gyro_config));
63 if (!this->write_byte(MPU6050_REGISTER_GYRO_CONFIG, gyro_config)) {
64 return false;
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 return false;
72 }
73 ESP_LOGV(TAG, " Input accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
74 accel_config &= 0b11100111;
75 accel_config |= (MPU6050_RANGE_2G << 3);
76 ESP_LOGV(TAG, " Output accel_config: 0b" BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(accel_config));
77 return this->write_byte(MPU6050_REGISTER_ACCEL_CONFIG, accel_config);
78}
80 ESP_LOGCONFIG(TAG, "MPU6050:");
81 LOG_I2C_DEVICE(this);
82 LOG_UPDATE_INTERVAL(this);
83 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
84 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
85 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
86 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
87 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
88 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
89 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
90}
91
93 ESP_LOGV(TAG, "Updating");
94 uint16_t raw_data[7];
95 if (!this->read_bytes_16(MPU6050_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
96 this->status_set_warning();
97 return;
98 }
99 auto *data = reinterpret_cast<int16_t *>(raw_data);
100
101 float accel_x = data[0] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
102 float accel_y = data[1] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
103 float accel_z = data[2] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
104
105 float temperature = data[3] / 340.0f + 36.53f;
106
107 float gyro_x = data[4] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
108 float gyro_y = data[5] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
109 float gyro_z = data[6] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
110
111 ESP_LOGD(TAG,
112 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
113 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
114 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
115
116 if (this->accel_x_sensor_ != nullptr)
117 this->accel_x_sensor_->publish_state(accel_x);
118 if (this->accel_y_sensor_ != nullptr)
119 this->accel_y_sensor_->publish_state(accel_y);
120 if (this->accel_z_sensor_ != nullptr)
121 this->accel_z_sensor_->publish_state(accel_z);
122
123 if (this->temperature_sensor_ != nullptr)
124 this->temperature_sensor_->publish_state(temperature);
125
126 if (this->gyro_x_sensor_ != nullptr)
127 this->gyro_x_sensor_->publish_state(gyro_x);
128 if (this->gyro_y_sensor_ != nullptr)
129 this->gyro_y_sensor_->publish_state(gyro_y);
130 if (this->gyro_z_sensor_ != nullptr)
131 this->gyro_z_sensor_->publish_state(gyro_z);
132
133 this->status_clear_warning();
134}
135
136} // namespace esphome::mpu6050
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 * accel_y_sensor_
Definition mpu6050.h:28
sensor::Sensor * accel_x_sensor_
Definition mpu6050.h:27
sensor::Sensor * gyro_x_sensor_
Definition mpu6050.h:31
bool setup_registers_()
Configure the sensor; false on an I2C failure.
Definition mpu6050.cpp:27
sensor::Sensor * temperature_sensor_
Definition mpu6050.h:30
sensor::Sensor * gyro_y_sensor_
Definition mpu6050.h:32
sensor::Sensor * accel_z_sensor_
Definition mpu6050.h:29
sensor::Sensor * gyro_z_sensor_
Definition mpu6050.h:33
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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
ESPHOME_LOG_TAG(TAG, "mpu6050")
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