Skip to main content

MPU6050 Gyroscope

It can see you


MPU6050 sensor module is complete 6-axis Motion Tracking Device. It combines 3-axis Gyroscope, 3-axis Accelerometer and Digital Motion Processor all in small package. Also, it has additional feature of on-chip Temperature sensor. It has I2C bus interface to communicate with the microcontrollers. It has Auxiliary I2C bus to communicate with other sensor devices like 3-axis Magnetometer, Pressure sensor etc. If 3-axis Magnetometer is connected to auxiliary I2C bus, then MPU6050 can provide complete 9-axis Motion Fusion output.More about the sensor and its functions can be found out at MPU6050 Datasheet

3-Axis Gyroscope#

The MPU6050 consist of 3-axis Gyroscope with Micro Electro Mechanical System(MEMS) technology. It is used to detect rotational velocity along the X, Y, Z axes. The full-scale range of output are +/- 250, +/- 500, +/- 1000, +/- 2000. It measures the angular velocity along each axis in degree per second unit.

3-Axis Accelerometer#

The MPU6050 consist 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect angle of tilt or inclination along the X, Y and Z axes. The full-scale range of acceleration are +/- 2g, +/- 4g, +/- 8g, +/- 16g. It measured in g (gravity force) unit.

Calculations#

Gyroscope and accelerometer sensor data of MPU6050 module consists of 16-bit raw data in 2’s complement form.

Now suppose we have selected,

  • Accelerometer full scale range of +/- 2g with Sensitivity Scale Factor of 16,384 LSB(Count)/g.

  • Gyroscope full scale range of +/- 250 °/s with Sensitivity Scale Factor of 131 LSB (Count)/°/s.

Accelerometer values in g Acceleration along the X axis = (Accelerometer X axis raw data/16384) g.

Acceleration along the Y axis = (Accelerometer Y axis raw data/16384) g.

Acceleration along the Z axis = (Accelerometer Z axis raw data/16384) g.

Gyroscope values in °/s Angular velocity along the X axis = (Gyroscope X axis raw data/131) °/s.

Angular velocity along the Y axis = (Gyroscope Y axis raw data/131) °/s.

Angular velocity along the Z axis = (Gyroscope Z axis raw data/131) °/s.


Enums definition#

pub enum MPUClockSource {
MPU6050ClockInternal8MHZ,
MPU6050ClockPllGyrox,
MPU6050ClockPllGyroy,
MPU6050ClockPllGyroz,
MPU6050ClockExternal32MHZ,
MPU6050ClockExternal19MHZ,
MPU6050ClockKeepReset,
}
pub enum MPUdpsT {
MPU6050Scale2000DPS,
MPU6050Scale1000DPS,
MPU6050Scale500DPS,
MPU6050Scale250DPS,
}
pub enum MPURangeT {
MPU6050Range2G,
MPU6050Range4G,
MPU6050Range8G,
MPU6050Range16G,
}
pub enum MPUOnDelayT {
MPU6050Delay3MS,
MPU6050Delay2MS,
MPU6050Delay1MS,
MPU6050NoDelay,
}
pub enum MPUdhpfT {
MPU6050dhpfReset,
MPU6050dhpf5HZ,
MPU6050dhpf2_5HZ,
MPU6050dhpf1_25HZ,
MPU6050dhpf0_63HZ,
MPU6050dhpfHold,
}
pub enum MPUdlpfT {
MPU6050dlpf6,
MPU6050dlpf5,
MPU6050dlpf4,
MPU6050dlpf3,
MPU6050dlpf2,
MPU6050dlpf1,
MPU6050dlpf0,
}

Struct definition#

pub struct MPU6050 {/*fields omitted*/}
use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::MPU6050;
// Here sensor is a mutable struct of type MPU6050.

Trait implementation#

Impl new for MPU6050#

pub fn new(&mut self) -> &'static mut Self

Returns pointer to the struct.

Usage:#

use rustduino::sensors::mpu6050;
let mut sensor = MPU6050::new()
// Here sensor is the pointer to the struct of the type MPU6050.
// This sensor pointer is used further for different functions below.

Impl readregister for MPU6050#

fn readregister(&mut self, reg: u8) -> u8

Returns the 8 bit data stored in the register with address reg using the I2C module.

Impl writeregister for MPU6050#

fn writeregister(&mut self, reg: u8, value: u8)

Writes the 8 bit value to register with address reg using the I2C module.

Impl writeregister_bit for MPU6050#

fn writeregister_bit(&mut self, reg: u8, pos: u8, state: bool)

Writes the state to bit pos of the register with address reg using the I2C module.

Impl set_dlpf_mode for MPU6050#

pub fn set_dlpf_mode(&mut self, dlpf: MPUdlpfT)

This configures the Digital Low Pass Filter (DLPF) setting for both the gyroscopes and accelerometers. The accelerometer and gyroscope are filtered according to the value of the enum MPUdlpfT as shown in the table below.

MPUdlpfT::Accelerometer
(Fs = 1kHz)
Gyroscope
Bandwidth
(Hz)
Delay
(ms)
Bandwidth
(Hz)
Delay
(ms)
Fs
(kHz)
MPU6050dlpf026002560.988
MPU6050dlpf11842.01881.91
MPU6050dlpf2943.0982.81
MPU6050dlpf3444.9424.81
MPU6050dlpf4218.5208.31
MPU6050dlpf51013.81013.41
MPU6050dlpf6519.0518.61

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_dlpf_mode(MPUdlpf::MPU6050dlpf4);

Impl set_dhpf_mode for MPU6050#

pub fn set_dhpf_mode(&mut self, dhpf: MPUdhpfT)

This configures the Digital High Pass Filter (DHPF). The DHPF is a filter module in the path leading to motion detectors (Free Fall, Motion threshold, and Zero Motion). The high pass filter output is not available to the data registers (see Figure in Section 8 of the MPU-6000/ MPU-6050 Product Specification document). The high pass filter has three modes:

  • Reset: The filter output settles to zero within one sample. This effectively disables the high pass filter. This mode may be toggled to quickly settle the filter.
  • On: The high pass filter will pass signals above the cut off frequency.
  • Hold: When triggered, the filter holds the present sample. The filter output will be the difference between the input sample and the held sample.
MPUdhpfT::Filter ModeCutoff Frequency
(Hz)
MPU6050dhpfResetResetNone
MPU6050dhpf5HZOn5Hz
MPU6050dhpf2_5HZOn2.5Hz
MPU6050dhpf1_25HZOn1.25Hz
MPU6050dhpf0_63HZOn0.63Hz
MPU6050dhpfHoldHoldNone

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_dhpf_mode(MPUdhpfT::MPU6050dhpfReset);

Impl set_scale for MPU6050#

pub fn set_scale(&mut self, scale: MPUdpsT)

Sets the full scale range of the gyroscope.

MPUdpsT::Full Scale Range
(deg/s)
MPU6050dps250+/-250
MPU6050dps500+/-500
MPU6050dps1000+/-1000
MPU6050dps2000+/-2000

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_scale(MPUdpsT::MPU6050dps500);

Impl get_scale for MPU6050#

pub fn get_scale(&mut self, scale: MPUdpsT) -> MPUdpsT

Returns the current full scale range of the gyroscope.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let scale = sensor.get_scale();

Impl set_range for MPU6050#

pub fn set_range(&mut self, range: MPURangeT)

Sets the range of the accelerometer.

{" "}

MPURangeT::Full Scale Range
(g)
MPU6050Range2G+/-2
MPU6050Range4G+/-4
MPU6050Range8G+/-8
MPU6050Range16G+/-16

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_range(MPURangeT::MPU6050Range8G);

Impl get_range for MPU6050#

pub fn get_range(&mut self) -> MPURangeT

Gets the current range of the accelerometer.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let range = sensor.get_range();

Impl set_clock_source for MPU6050#

pub fn set_clock_source(&mut self, source: MPUClockSourceT)

Set clock source setting.
An internal 8MHz oscillator, gyroscope based clock, or external sources can be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator or an external source is chosen as the clock source, the MPU-60X0 can operate in low power modes with the gyroscopes disabled.
Upon power up, the MPU-60X0 clock source defaults to the internal oscillator.However, it is highly recommended that the device be configured to use one of the gyroscopes (or an external clock source) as the clock reference for improved stability. The clock source can be selected according to the following table:

MPUClockSourceT::Clock Source
MPU6050ClockInternal8MHZInternal oscillator
MPU6050ClockPllGyroxGyroscope X axis reference
MPU6050ClockPllGyroyGyroscope Y axis reference
MPU6050ClockPllGyrozGyroscope Z axis reference
MPU6050ClockExternal32MHZPll with external 32.768kHz reference
MPU6050ClockExternal19MHZPll with external 19.2MHz reference
MPU6050ClockKeepResetKeeps the timing generator in reset

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_clock_source(MPUClockSourceT::MPU6050ClockSourcePLLGyroZ);

Impl get_clock_source for MPU6050#

pub fn get_clock_source(&mut self) -> MPUClockSourceT

Gets clock source setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let source = sensor.get_clock_source();

Impl set_int_free_fall_enabled for MPU6050#

pub fn set_int_free_fall_enabled(&mut self, state: bool)

Sets int free fall enabled setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_int_free_fall_enabled(true);

Impl get_int_free_fall_enabled for MPU6050#

pub fn get_int_free_fall_enabled(&mut self) -> bool

Gets int free fall enabled setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state = sensor.get_int_free_fall_enabled();

Impl set_accel_power_on_delay for MPU6050#

pub fn set_accel_power_on_delay(&mut self, delay: MPUOnDelayT)

Sets accelerometer power on delay setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
set_accel_power_on_delay(MPUOnDelayT::MPU6050OnDelay1ms);

Impl get_accel_power_on_delay for MPU6050#

pub fn get_accel_power_on_delay(&mut self) -> MPUOnDelayT

Gets accelerometer power on delay setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_accel_power_on_delay();

Impl set_motion_detection_threshold for MPU6050#

pub fn set_motion_detection_threshold(&mut self, threshold: u8)

Sets motion detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_motion_detection_threshold(10);

Impl get_motion_detection_threshold for MPU6050#

pub fn get_motion_detection_threshold(&mut self) -> u8

Gets motion detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_motion_detection_threshold();

Impl set_motion_detection_duration for MPU6050#

pub fn set_motion_detection_duration(&mut self, duration: u8)

Sets motion detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
set_motion_detection_duration(10);

Impl get_motion_detection_duration for MPU6050#

pub fn get_motion_detection_duration(&mut self) -> u8

Gets motion detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_motion_detection_duration();

Impl set_zero_motion_detection_threshold for MPU6050#

pub fn set_zero_motion_detection_threshold(&mut self, threshold: u8)

Sets zero motion detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_zero_motion_detection_threshold(10);

Impl get_zero_motion_detection_threshold for MPU6050#

pub fn get_zero_motion_detection_threshold(&mut self) -> u8

Gets zero motion detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_zero_motion_detection_threshold();

Impl set_zero_motion_detection_duration for MPU6050#

pub fn set_zero_motion_detection_duration(&mut self, duration: u8)

Sets zero motion detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_zero_motion_detection_duration(10);

Impl get_zero_motion_detection_duration for MPU6050#

pub fn get_zero_motion_detection_duration(&mut self) -> u8

Gets zero motion detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_zero_motion_detection_duration();

Impl set_free_fall_detection_threshold for MPU6050#

pub fn set_free_fall_detection_threshold(&mut self, threshold: u8)

Sets free fall detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_free_fall_detection_threshold(10);

Impl get_free_fall_detection_threshold for MPU6050#

pub fn get_free_fall_detection_threshold(&mut self) -> u8

Gets free fall detection threshold setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_free_fall_detection_threshold();

Impl set_free_fall_detection_duration for MPU6050#

pub fn set_free_fall_detection_duration(&mut self, duration: u8)

Sets free fall detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_free_fall_detection_duration(10);

Impl get_free_fall_detection_duration for MPU6050#

pub fn get_free_fall_detection_duration(&mut self) -> u8

Gets free fall detection duration setting.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let output = sensor.get_free_fall_detection_duration();

Impl set_sleep_enabled for MPU6050#

pub fn set_sleep_enabled(&mut self, state: bool)

Enables sleep mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_sleep_enabled(true);

Impl get_sleep_enabled for MPU6050#

pub fn get_sleep_enabled(&mut self) -> bool

Gets the state of sleep mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_sleep_enabled();

Impl get_int_zero_motion_enabled for MPU6050#

pub fn get_int_zero_motion_enabled(&mut self) -> bool

Gets the state of zero motion detection interrupt.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_int_zero_motion_enabled();

Impl set_int_zero_motion_enabled for MPU6050#

pub fn set_int_zero_motion_enabled(&mut self, state: bool)

Sets the state of zero motion detection interrupt.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_int_zero_motion_enabled(true);

Impl get_int_motion_enabled for MPU6050#

pub fn get_int_motion_enabled(&mut self) -> bool

Gets the state of motion detection interrupt.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_int_motion_enabled();

Impl set_int_motion_enabled for MPU6050#

pub fn set_int_motion_enabled(&mut self, state: bool)

Sets the state of motion detection interrupt.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_int_motion_enabled(true);

Impl set_i2c_master_mode_enabled for MPU6050#

pub fn set_i2c_master_mode_enabled(&mut self, state: bool)

Enables I2C master mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_i2c_master_mode_enabled(true);

Impl get_i2c_master_mode_enabled for MPU6050#

pub fn get_i2c_master_mode_enabled(&mut self) -> bool

Gets the status of I2C master mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_i2c_master_mode_enabled();

Impl set_i2c_byepass_enabled for MPU6050#

pub fn set_i2c_byepass_enabled(&mut self, state: bool)

Enables I2C bypass mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.set_i2c_byepass_enabled(true);

Impl get_i2c_byepass_enabled for MPU6050#

pub fn get_i2c_byepass_enabled(&mut self) -> bool

Gets the status of I2C bypass mode.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_i2c_byepass_enabled();

Impl get_int_status for MPU6050#

pub fn get_int_status(&mut self) -> u8

Gets the interrupt status.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let state =sensor.get_int_status();

Impl read_accel for MPU6050#

pub fn read_accel(&mut self) -> FixedSliceVec<f32>
  • Reads the three, two-byte accelerometer values from the sensor.
  • Returns the two-byte raw accelerometer values as a 32-bit float.
  • The vec accel_output stores the raw values of the accelerometer where accel_output[0] is the x-axis, accel_output[1] is the y-axis and accel_output[2] is the z-axis output respectively. These raw values are then converted to g's per second according to the scale given as input in begin() function.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let accel_output = sensor.read_accel();

Impl read_gyro for MPU6050#

pub fn read_gyro(&mut self) -> FixedSliceVec<f32>
  • Reads the three, two-byte gyroscope values from the sensor.
  • Returns the two-byte raw gyroscope values as a 32-bit float.
  • The vec gyro_output stores the raw values of the gyroscope where gyro_output[0] is the x-axis, gyro_output[1] is the y-axis and gyro_output[2] is the z-axis output respectively. These raw values are then converted to degrees per second according to the scale given as input in begin() function.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
let gyro_output = sensor.read_gyro();

Impl begin for MPU6050#

pub fn begin(&mut self, scale: MPUdpsT, range: MPURangeT) -> bool

Starts the sensor by setting the device to active mode ,setting the accelerometer range and gyroscope scale.

Usage#

use rustduino::sensors::mpu6050;
let mut sensor = mpu6050::new();
sensor.begin(MPUdpsT::MPU6050dps500,MPUrangeT::MPU6050Range8G);
Last updated on by Devansh