import mpu6050
The mpu6050 module reads InvenSense MPU-6050 compatible 6-axis IMU boards over I2C. It exposes acceleration in g, angular velocity in degrees per second, the on-chip temperature sensor, and per-sensor software calibration offsets. Create one MPU6050 object for each physical IMU. Two modules can share the same I2C bus when one board uses address 0x68 and the other uses 0x69.
Quick example
import mpu6050 body = mpu6050.MPU6050(sda=21, scl=22, address=0x68) arm = mpu6050.MPU6050(sda=21, scl=22, address=0x69) body.configure(accel_range=4, gyro_range=500, dlpf=3, sample_rate=100) reading = body.read() print(reading["accel"]["x"], reading["gyro"]["z"]) arm.calibrate(samples=100, delay_ms=5) print(arm.accel())
importmpu6050
MPU6050 accelerometer and gyroscope module.
The mpu6050 module reads InvenSense MPU-6050 compatible 6-axis IMU boards over I2C. It exposes acceleration in g, angular velocity in degrees per second, the on-chip temperature sensor, and per-sensor software calibration offsets. Create one MPU6050 object for each physical IMU. Two modules can share the same I2C bus when one board uses address 0x68 and the other uses 0x69.
mpu6050.MPU6050(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> mpu6050_sensor
mpu6050.MPU6050(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> mpu6050_sensorCreate and initialize an independent MPU6050 IMU.
The constructor creates the I2C device handle, verifies the WHO_AM_I register, wakes the sensor, and applies the default configuration. Use address 0x68 for the normal AD0-low board address and 0x69 when AD0 is pulled high.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO pin used for I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO pin used for I2C SCL. |
address |
int |
positional or keyword | No | Optional I2C address. Defaults to 0x68. Use 0x69 for a second MPU6050 on the same bus. |
frequency |
int |
positional or keyword | No | Optional I2C bus speed in hertz. Defaults to 400000. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port number. Defaults to 0. |
mpu6050_sensor mpu6050_sensor object.
mpu6050.MPU6050().configure(accel_range=None, gyro_range=None, dlpf=None, sample_rate=None) -> bool
mpu6050.MPU6050().configure(accel_range=None, gyro_range=None, dlpf=None, sample_rate=None) -> boolConfigure accelerometer range, gyroscope range, digital low-pass filter, and output sample rate for this sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
accel_range |
int |
positional or keyword | No | Optional value. Defaults to None. |
gyro_range |
int |
positional or keyword | No | Optional value. Defaults to None. |
dlpf |
int |
positional or keyword | No | Optional value. Defaults to None. |
sample_rate |
int |
positional or keyword | No | Optional value. Defaults to None. |
mpu6050.MPU6050().read(raw=False) -> dict
mpu6050.MPU6050().read(raw=False) -> dictRead acceleration, gyroscope, and temperature in one I2C transaction. By default returns compact engineering values. Pass raw=True to include raw counts, corrected counts, ranges, filter settings, sample rate, and address.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional value. Defaults to False. |
mpu6050.MPU6050().accel(raw=False) -> dict
mpu6050.MPU6050().accel(raw=False) -> dictRead acceleration only from this sensor. By default returns x, y, z, and unit. Pass raw=True for raw counts and range metadata.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional value. Defaults to False. |
mpu6050.MPU6050().gyro(raw=False) -> dict
mpu6050.MPU6050().gyro(raw=False) -> dictRead angular velocity only from this sensor. By default returns x, y, z, and unit. Pass raw=True for raw counts and range metadata.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional value. Defaults to False. |
mpu6050.MPU6050().temperature() -> float
mpu6050.MPU6050().temperature() -> floatRead the on-chip temperature in degrees Celsius.
mpu6050.MPU6050().ready() -> bool
mpu6050.MPU6050().ready() -> boolReturn True when the data-ready status bit is set.
mpu6050.MPU6050().calibrate(samples=None, delay_ms=None) -> dict
mpu6050.MPU6050().calibrate(samples=None, delay_ms=None) -> dictEstimate software offsets while the board is still and level.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
delay_ms |
int |
positional or keyword | No | Optional value. Defaults to None. |
mpu6050.MPU6050().set_offsets(accel_x=None, accel_y=None, accel_z=None, gyro_x=None, gyro_y=None, gyro_z=None) -> bool
mpu6050.MPU6050().set_offsets(accel_x=None, accel_y=None, accel_z=None, gyro_x=None, gyro_y=None, gyro_z=None) -> boolSet software offsets manually for this sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
accel_x |
int |
positional or keyword | No | Optional value. Defaults to None. |
accel_y |
int |
positional or keyword | No | Optional value. Defaults to None. |
accel_z |
int |
positional or keyword | No | Optional value. Defaults to None. |
gyro_x |
int |
positional or keyword | No | Optional value. Defaults to None. |
gyro_y |
int |
positional or keyword | No | Optional value. Defaults to None. |
gyro_z |
int |
positional or keyword | No | Optional value. Defaults to None. |
mpu6050.MPU6050().offsets() -> dict
mpu6050.MPU6050().offsets() -> dictReturn this sensor's software calibration offsets.
mpu6050.MPU6050().clear_offsets() -> bool
mpu6050.MPU6050().clear_offsets() -> boolClear this sensor's software calibration offsets.
mpu6050.MPU6050().sleep(enabled=True) -> bool
mpu6050.MPU6050().sleep(enabled=True) -> boolPut this sensor into or out of sleep mode.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
enabled |
bool |
positional or keyword | No | Optional value. Defaults to True. |
mpu6050.MPU6050().wake() -> bool
mpu6050.MPU6050().wake() -> boolWake this sensor and select the normal gyro PLL clock.
mpu6050.MPU6050().reset() -> bool
mpu6050.MPU6050().reset() -> boolSoft-reset this sensor and restore its current configuration.
mpu6050.MPU6050().id() -> int
mpu6050.MPU6050().id() -> intRead the WHO_AM_I register.
mpu6050.MPU6050().info() -> dict
mpu6050.MPU6050().info() -> dictReturn I2C settings, range settings, sleep state, and offsets for this sensor.
mpu6050.MPU6050().close() -> bool
mpu6050.MPU6050().close() -> boolRelease this MPU6050 device handle.
mpu6050.MPU6050().Example
mpu6050.MPU6050().Exampleimu = mpu6050.MPU6050(sda=21, scl=22, address=0x68) imu.configure(accel_range=4, gyro_range=500) print(imu.read())
mpu6050.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> bool
mpu6050.begin(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> boolLegacy: initialize the module-level default MPU6050 sensor.
Prefer ``imu = mpu6050.MPU6050(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO pin used for I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO pin used for I2C SCL. |
address |
int |
positional or keyword | No | Optional I2C address. Defaults to 0x68. Boards with AD0 high usually use 0x69. |
frequency |
int |
positional or keyword | No | Optional I2C bus speed in hertz. Defaults to 400000. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port number. Defaults to 0. |
bool True when the sensor answers WHO_AM_I and accepts the default configuration.
mpu6050.configure(accel_range: int = None, gyro_range: int = None, dlpf: int = None, sample_rate: int = None) -> bool
mpu6050.configure(accel_range: int = None, gyro_range: int = None, dlpf: int = None, sample_rate: int = None) -> boolLegacy: configure the default sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
accel_range |
int |
positional or keyword | No | Optional accelerometer full-scale range in g. Use 2, 4, 8, or 16. Defaults to 2. |
gyro_range |
int |
positional or keyword | No | Optional gyroscope full-scale range in degrees per second. Use 250, 500, 1000, or 2000. Defaults to 250. |
dlpf |
int |
positional or keyword | No | Optional digital low-pass filter setting from 0 through 6. Defaults to 3. |
sample_rate |
int |
positional or keyword | No | Optional output sample rate in hertz. Values are clamped to the MPU6050 divider range. Defaults to 100. |
bool True when the registers are updated.
mpu6050.read(raw: bool = None) -> dict
mpu6050.read(raw: bool = None) -> dictLegacy: read acceleration, gyroscope, and temperature from the default sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional bool. Defaults to False. When True, include raw register counts, corrected counts, range settings, filter setting, sample rate, and address. |
dict Dict with ``ok``, ``accel`` containing x/y/z in g, ``gyro`` containing x/y/z in degrees per second, ``temperature``, ``temperature_c``, and ``address``. With ``raw=True``, returns the full hardware/debug payload.
mpu6050.accel(raw: bool = None) -> dict
mpu6050.accel(raw: bool = None) -> dictLegacy: read acceleration only from the default sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional bool. Defaults to False. When True, include raw axis counts and range metadata. |
dict Dict with ``ok``, ``x``, ``y``, ``z``, and ``unit``. Values are in g. With ``raw=True``, returns raw counts, g aliases, and ``range_g``.
mpu6050.gyro(raw: bool = None) -> dict
mpu6050.gyro(raw: bool = None) -> dictLegacy: read angular velocity only from the default sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
raw |
bool |
positional or keyword | No | Optional bool. Defaults to False. When True, include raw axis counts and range metadata. |
dict Dict with ``ok``, ``x``, ``y``, ``z``, and ``unit``. Values are in degrees per second. With ``raw=True``, returns raw counts, dps aliases, and ``range_dps``.
mpu6050.temperature() -> float
mpu6050.temperature() -> floatLegacy: read the default sensor's on-chip temperature.
float Temperature in degrees Celsius.
mpu6050.ready() -> bool
mpu6050.ready() -> boolLegacy: check whether the default sensor has new data.
bool True when new sensor data is ready.
mpu6050.calibrate(samples: int = None, delay_ms: int = None) -> dict
mpu6050.calibrate(samples: int = None, delay_ms: int = None) -> dictLegacy: estimate software offsets for the default sensor.
Keep the sensor motionless with Z facing up. The native module averages raw samples, treats X/Y acceleration and all gyro axes as zero, and treats Z as one g.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of samples to average. Defaults to 100. |
delay_ms |
int |
positional or keyword | No | Optional delay between samples in milliseconds. Defaults to 5. |
dict Dict with ``ok``, sample count, delay, and the six raw offset values.
mpu6050.set_offsets(accel_x: int = None, accel_y: int = None, accel_z: int = None, gyro_x: int = None, gyro_y: int = None, gyro_z: int = None) -> bool
mpu6050.set_offsets(accel_x: int = None, accel_y: int = None, accel_z: int = None, gyro_x: int = None, gyro_y: int = None, gyro_z: int = None) -> boolLegacy: set software offsets manually for the default sensor.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
accel_x |
int |
positional or keyword | No | Optional raw accelerometer X offset. |
accel_y |
int |
positional or keyword | No | Optional raw accelerometer Y offset. |
accel_z |
int |
positional or keyword | No | Optional raw accelerometer Z offset. |
gyro_x |
int |
positional or keyword | No | Optional raw gyroscope X offset. |
gyro_y |
int |
positional or keyword | No | Optional raw gyroscope Y offset. |
gyro_z |
int |
positional or keyword | No | Optional raw gyroscope Z offset. |
bool True after the offsets are stored in memory.
mpu6050.offsets() -> dict
mpu6050.offsets() -> dictLegacy: return the default sensor's software calibration offsets.
dict Dict with raw accelerometer and gyroscope offsets.
mpu6050.clear_offsets() -> bool
mpu6050.clear_offsets() -> boolLegacy: clear software calibration offsets on the default sensor.
bool True after all offsets are reset to zero.
mpu6050.sleep(enabled: bool = None) -> bool
mpu6050.sleep(enabled: bool = None) -> boolLegacy: put the default sensor into or out of sleep mode.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
enabled |
bool |
positional or keyword | No | Optional sleep state. True sleeps the sensor, False wakes it. Defaults to True. |
bool True when the power-management register is updated.
mpu6050.wake() -> bool
mpu6050.wake() -> boolLegacy: wake the default sensor.
bool True when the wake command is accepted.
mpu6050.reset() -> bool
mpu6050.reset() -> boolLegacy: soft-reset the default sensor and restore its current configuration.
bool True when reset and configuration succeed.
mpu6050.id() -> int
mpu6050.id() -> intLegacy: read the WHO_AM_I register from the default sensor.
int Integer device identifier. A normal MPU6050 returns 0x68.
mpu6050.info() -> dict
mpu6050.info() -> dictLegacy: return default MPU6050 module state.
dict Dict with initialization state, sleep state, I2C settings, ranges, filter, sample rate, scale factors, and software offsets.