ESPHome 2026.11.0b6
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 // Clock source X-Gyro, sleep off, temperature on
36 if (!this->update_register(MPU6050_REGISTER_POWER_MANAGEMENT_1,
39 return false;
40 }
41
42 ESP_LOGV(TAG, " Setting up Gyro Config");
43 // Set scale - 2000DPS
44 if (!this->update_register(MPU6050_REGISTER_GYRO_CONFIG, 0b00011000, MPU6050_SCALE_2000_DPS << 3)) {
45 return false;
46 }
47
48 ESP_LOGV(TAG, " Setting up Accel Config");
49 // Set range - 2G
50 return this->update_register(MPU6050_REGISTER_ACCEL_CONFIG, 0b00011000, MPU6050_RANGE_2G << 3);
51}
52void MPU6050Component::dump_config() {
53 ESP_LOGCONFIG(TAG, "MPU6050:");
54 LOG_I2C_DEVICE(this);
55 LOG_UPDATE_INTERVAL(this);
56 LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_);
57 LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_);
58 LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_);
59 LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_);
60 LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_);
61 LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_);
62 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
63}
64
65void MPU6050Component::update() {
66 ESP_LOGV(TAG, "Updating");
67 uint16_t raw_data[7];
68 if (!this->read_bytes_16(MPU6050_REGISTER_ACCEL_XOUT_H, raw_data, 7)) {
69 this->status_set_warning();
70 return;
71 }
72 auto *data = reinterpret_cast<int16_t *>(raw_data);
73
74 float accel_x = data[0] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
75 float accel_y = data[1] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
76 float accel_z = data[2] * MPU6050_RANGE_PER_DIGIT_2G * GRAVITY_EARTH;
77
78 float temperature = data[3] / 340.0f + 36.53f;
79
80 float gyro_x = data[4] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
81 float gyro_y = data[5] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
82 float gyro_z = data[6] * MPU6050_SCALE_DPS_PER_DIGIT_2000;
83
84 ESP_LOGD(TAG,
85 "Got accel={x=%.3f m/s², y=%.3f m/s², z=%.3f m/s²}, "
86 "gyro={x=%.3f °/s, y=%.3f °/s, z=%.3f °/s}, temp=%.3f°C",
87 accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z, temperature);
88
89 PUBLISH_STATE(this->accel_x_sensor_, accel_x);
90 PUBLISH_STATE(this->accel_y_sensor_, accel_y);
91 PUBLISH_STATE(this->accel_z_sensor_, accel_z);
92
93 PUBLISH_STATE(this->temperature_sensor_, temperature);
94
95 PUBLISH_STATE(this->gyro_x_sensor_, gyro_x);
96 PUBLISH_STATE(this->gyro_y_sensor_, gyro_y);
97 PUBLISH_STATE(this->gyro_z_sensor_, gyro_z);
98
100}
101
102} // namespace esphome::mpu6050
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 mpu6050.cpp:27
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