esp-idf-lib/mpu6050

2.1.12

Latest
uploaded 5 days ago
Driver for MPU6000/MPU6050 6-axis MotionTracking device

Readme

# esp-idf-lib/mpu6050

[![Build examples](https://github.com/esp-idf-lib/mpu6050/actions/workflows/build.yml/badge.svg)](https://github.com/esp-idf-lib/mpu6050/actions/workflows/build.yml)
[![Build docs](https://github.com/esp-idf-lib/mpu6050/actions/workflows/build-docs.yml/badge.svg)](https://github.com/esp-idf-lib/mpu6050/actions/workflows/build-docs.yml)
[![Validation](https://github.com/esp-idf-lib/mpu6050/actions/workflows/validate-component.yml/badge.svg)](https://github.com/esp-idf-lib/mpu6050/actions/workflows/validate-component.yml)

Driver for the MPU6000/MPU6050 6-axis MotionTracking device.

* [Documentation](https://esp-idf-lib.github.io/mpu6050/)
* [Repository](https://github.com/esp-idf-lib/mpu6050)
* [Issues](https://github.com/esp-idf-lib/mpu6050/issues)
* [Discussions and questions](https://github.com/esp-idf-lib/core/discussions)
* [Component page at the ESP Component Registry](https://components.espressif.com/components/esp-idf-lib/mpu6050)

## Installation

```sh
idf.py add-dependency esp-idf-lib/mpu6050
```

## Hardware

The chip answers on `0x68` when `AD0` is tied to ground and on `0x69` when it is
tied to VCC (`MPU6050_I2C_ADDRESS_LOW` and `MPU6050_I2C_ADDRESS_HIGH`);
`mpu6050_init_desc()` accepts these two and rejects anything else. Supply is
2.375–3.46 V, so a module with a regulator and level shifters is the safe
choice for a 3.3 V board and a 5 V one is not usable as is.

The driver sets the port to 400 kHz, the datasheet limit of the chip; some
modules work faster, but that is outside the specification.

The `INT` pin is not touched by the driver: it is configured on the chip, but
which GPIO to use and whether to poll it or attach an interrupt is the
application's decision.

## Usage

```c
#include <mpu6050.h>

mpu6050_dev_t dev = { 0 };
ESP_ERROR_CHECK(mpu6050_init_desc(&dev, MPU6050_I2C_ADDRESS_LOW, 0,
                                  CONFIG_EXAMPLE_SDA_GPIO,
                                  CONFIG_EXAMPLE_SCL_GPIO));
ESP_ERROR_CHECK(mpu6050_init(&dev));

mpu6050_acceleration_t accel;
mpu6050_rotation_t gyro;
ESP_ERROR_CHECK(mpu6050_get_motion(&dev, &accel, &gyro));
printf("%.3f %.3f %.3f g, %.2f %.2f %.2f dps\n",
       accel.x, accel.y, accel.z, gyro.x, gyro.y, gyro.z);
```

`i2cdev_init()` has to run before the first transfer; `mpu6050_init_desc()` only
fills the descriptor and creates its mutex. `mpu6050_init()` wakes the chip,
takes the PLL as the clock source and sets the narrowest ranges, ±2 g and
±250 dps, which give the finest resolution.

## Reading

`mpu6050_get_motion()` reads the accelerometer and the gyroscope, scaled to g
and degrees per second according to the ranges currently set.
`mpu6050_get_acceleration()` and `mpu6050_get_rotation()` do the same for one
sensor at a time, `mpu6050_get_temperature()` returns the die temperature in
degrees Celsius. Raw counts are available as `mpu6050_get_raw_acceleration()` and
`mpu6050_get_raw_rotation()`.

At ±2 g one count is 61 µg, at ±250 dps one count is 0.0076 dps; other ranges
scale linearly, and `mpu6050_set_full_scale_accel_range()` /
`mpu6050_set_full_scale_gyro_range()` switch between them. The low-pass filter
`mpu6050_set_dlpf_mode()` and the sample rate divider `mpu6050_set_rate()`
control what the sensor does between the readings.

## Calibration

`mpu6050_calibrate()` measures the offsets of both sensors and writes them into
the chip: the gyroscope offsets go to the hardware offset registers, the
accelerometer offsets are added to the factory trim. It also returns the
measured offsets — degrees per second and g — for applications that prefer to
subtract them by themselves.

The sensor must lie still during the call; the function resets the chip, fills
the FIFO for 80 ms and then averages the samples, which takes about half a second
(the waits are 100, 100, 200, 15 and 80 ms, quantised to the FreeRTOS tick).
An accelerometer offset along the gravity axis is removed too, so a calibrated
sensor lying on a table reports zero on all three axes. A chip that returns no
samples at all (a reset left it asleep, or the FIFO is broken) is reported as
`ESP_ERR_INVALID_STATE` instead of dividing by zero.

## Self test

`mpu6050_self_test()` runs the datasheet procedure: it samples both sensors with
the self test disabled, enables the built-in self test of all six axes, samples
again, and compares the difference with the factory trim from the chip. Six
percentages go into `destination` (accelerometer XYZ, then gyroscope XYZ); a
value inside 100 ± 14 % means the sensor is within the datasheet tolerance.
The test averages 100 samples twice with a small pause between them, so it
takes about 500 ms at the default 1 kHz FreeRTOS tick and up to about 2 s when
the tick is 100 Hz.

## Interrupts

The chip can raise an interrupt on motion, on free fall and on the absence of
motion. `mpu6050_set_motion_detection_threshold()`,
`mpu6050_set_motion_detection_duration()`, the corresponding free-fall and
zero-motion functions set the conditions, `mpu6050_set_int_enabled()` enables
the sources, and `mpu6050_get_motion_status()` reads which of them fired.

## Limitations

- The DMP (the on-chip motion processor) is not used, so gestures, step
  counting and orientation fusion are not available.
- There is no magnetometer, so there is no absolute heading.
- The I2C master of the chip (for an external magnetometer) is switched off and
  has no helpers beyond the raw register accessors.
- A reading is a few transactions with a mutex each, so another device on the
  bus can interleave between them.
- `mpu6050_free_desc()` deletes the mutex, it does not release the port; call
  `i2cdev_done()` when nothing else uses the bus.

## Examples

- `default` — prints acceleration, rotation and temperature about every 100 ms.

## Support

For questions and discussions about the component, please use
[Discussions](https://github.com/esp-idf-lib/core/discussions)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

## Contributing

Please read [CONTRIBUTING.md](https://github.com/esp-idf-lib/core/blob/main/CONTRIBUTING.md)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

Links

Supports all targets

Maintainers

  • Raghav Jha (@horsemann07)
  • Ruslan V. Uss (@UncleRus) <unclerus@gmail.com>
To add this component to your project, run:

idf.py add-dependency "esp-idf-lib/mpu6050^2.1.12"

download archive

Stats

  • Archive size
    Archive size ~ 41.69 KB
  • Downloaded in total
    Downloaded in total 1.3k times
  • Weekly Downloads Weekly Downloads (All Versions)
  • Downloaded this version
    This version: 11 times

Badge

esp-idf-lib/mpu6050 version: 2.1.12
|