MojoScale Studio Docs
API Reference

mpu6050

MPU6050 accelerometer and gyroscope module.

Studio Docs Sensors & I/O

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.

API 34 available

mpu6050.MPU6050(sda: int, scl: int, address: int = None, frequency: int = None, port: int = None) -> mpu6050_sensor

Create 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.

Parameters
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.
Returns

mpu6050_sensor mpu6050_sensor object.

mpu6050.MPU6050().configure(accel_range=None, gyro_range=None, dlpf=None, sample_rate=None) -> bool

Configure accelerometer range, gyroscope range, digital low-pass filter, and output sample rate for this sensor.

Parameters
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

Read 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.

Parameters
Name Type Pass as Required Description
raw bool positional or keyword No Optional value. Defaults to False.

mpu6050.MPU6050().accel(raw=False) -> dict

Read acceleration only from this sensor. By default returns x, y, z, and unit. Pass raw=True for raw counts and range metadata.

Parameters
Name Type Pass as Required Description
raw bool positional or keyword No Optional value. Defaults to False.

mpu6050.MPU6050().gyro(raw=False) -> dict

Read angular velocity only from this sensor. By default returns x, y, z, and unit. Pass raw=True for raw counts and range metadata.

Parameters
Name Type Pass as Required Description
raw bool positional or keyword No Optional value. Defaults to False.

mpu6050.MPU6050().temperature() -> float

Read the on-chip temperature in degrees Celsius.

mpu6050.MPU6050().ready() -> bool

Return True when the data-ready status bit is set.

mpu6050.MPU6050().calibrate(samples=None, delay_ms=None) -> dict

Estimate software offsets while the board is still and level.

Parameters
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

Set software offsets manually for this sensor.

Parameters
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

Return this sensor's software calibration offsets.

mpu6050.MPU6050().clear_offsets() -> bool

Clear this sensor's software calibration offsets.

mpu6050.MPU6050().sleep(enabled=True) -> bool

Put this sensor into or out of sleep mode.

Parameters
Name Type Pass as Required Description
enabled bool positional or keyword No Optional value. Defaults to True.

mpu6050.MPU6050().wake() -> bool

Wake this sensor and select the normal gyro PLL clock.

mpu6050.MPU6050().reset() -> bool

Soft-reset this sensor and restore its current configuration.

mpu6050.MPU6050().id() -> int

Read the WHO_AM_I register.

mpu6050.MPU6050().info() -> dict

Return I2C settings, range settings, sleep state, and offsets for this sensor.

mpu6050.MPU6050().close() -> bool

Release this MPU6050 device handle.

mpu6050.MPU6050().Example

imu = 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

Legacy: initialize the module-level default MPU6050 sensor.

Prefer ``imu = mpu6050.MPU6050(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working.

Parameters
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.
Returns

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

Legacy: configure the default sensor.

Parameters
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.
Returns

bool True when the registers are updated.

mpu6050.read(raw: bool = None) -> dict

Legacy: read acceleration, gyroscope, and temperature from the default sensor.

Parameters
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.
Returns

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

Legacy: read acceleration only from the default sensor.

Parameters
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.
Returns

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

Legacy: read angular velocity only from the default sensor.

Parameters
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.
Returns

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

Legacy: read the default sensor's on-chip temperature.

Returns

float Temperature in degrees Celsius.

mpu6050.ready() -> bool

Legacy: check whether the default sensor has new data.

Returns

bool True when new sensor data is ready.

mpu6050.calibrate(samples: int = None, delay_ms: int = None) -> dict

Legacy: 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.

Parameters
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.
Returns

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

Legacy: set software offsets manually for the default sensor.

Parameters
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.
Returns

bool True after the offsets are stored in memory.

mpu6050.offsets() -> dict

Legacy: return the default sensor's software calibration offsets.

Returns

dict Dict with raw accelerometer and gyroscope offsets.

mpu6050.clear_offsets() -> bool

Legacy: clear software calibration offsets on the default sensor.

Returns

bool True after all offsets are reset to zero.

mpu6050.sleep(enabled: bool = None) -> bool

Legacy: put the default sensor into or out of sleep mode.

Parameters
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.
Returns

bool True when the power-management register is updated.

mpu6050.wake() -> bool

Legacy: wake the default sensor.

Returns

bool True when the wake command is accepted.

mpu6050.reset() -> bool

Legacy: soft-reset the default sensor and restore its current configuration.

Returns

bool True when reset and configuration succeed.

mpu6050.id() -> int

Legacy: read the WHO_AM_I register from the default sensor.

Returns

int Integer device identifier. A normal MPU6050 returns 0x68.

mpu6050.info() -> dict

Legacy: return default MPU6050 module state.

Returns

dict Dict with initialization state, sleep state, I2C settings, ranges, filter, sample rate, scale factors, and software offsets.