• Vns Akhil tech badi channel

Interfacing an MPU6050 IMU Sensor with Raspberry Pi

Updated: Apr 17

The MPU6050 is an IMU(Inertial Measurement Unit) sensor that comes with an Accelerometer, Gyroscope sensor, and a temperature sensor too. It consists of a MEMS accelerometer, a temperature sensor and a MEMS gyro. When converting analogue values to digital, this device is very precise because it has a 16bit analogue to digital converter hardware for each channel. This module is capable of capturing channels x, y and z simultaneously. For interact with the host controller, it has an I2C interface. For many applications, such as drones, robots and motion sensors, this is a very useful unit. It is sometimes referred to as the Triple Axis Accelerometer or Gyroscope.

List of components required:

1. Raspberry Pi

2. MPU-6050

3. Jumping wires

4. Breadboard

Raspberry Pi: The Raspberry Pi is a series of small single-board computers which was developed by the Raspberry Pi Foundation in the United Kingdom. Early on, the Raspberry Pi project focused on encouraging basic computer science teaching in schools and in developing economies. The original design later became much more popular than expected, selling for uses such as automobiles outside its target group.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi
Raspberry Pi

MPU-6050: The MPU 6050 is a 6 Degrees of Freedom or a six-axis IMU sensor, which means that it produces six values. Three values for the accelerometer and three for the gyroscope. The MPU 6050 is a MEMS (Micro Electro Mechanical Systems) based sensor.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi

Jumping wires: Jumper wires are essentially wires with connector pins at either end, allowing them to be used to connect two points to each other without soldering. Jumper wires are usually used for breadboards and other prototyping instruments to make it easy to adjusts circuits.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi
Jumping wires

Breadboard: The breadboard is a rectangular acrylic board with a tonne of tiny holes inside it. Such holes allow you to quickly insert electronic components into the model of an electronic circuit, such as this one with a battery, a switch, a resistor, and an LED.

Circuit Diagram:

Interfacing an MPU6050 IMU Sensor with Raspberry Pi
Circuit diagram

In #MPU6050 we have 8 eight pins on them in that 8 pins we VCC, SCL, ground and SDA evidence. Now we will setup i2c communication format with the raspberry pi board here VCC is connected to the 5-volt pin, ground is #connected with ground, SCL with SCL pin and SDA pin with SDA pin on the raspberry pi. This sensor can we used to measure velocity, rotational velocity and many other parameters.


import smbus			#import SMBus module of I2C
from time import sleep          #import

#some MPU6050 Registers and their Address
PWR_MGMT_1   = 0x6B
SMPLRT_DIV   = 0x19
CONFIG       = 0x1A
INT_ENABLE   = 0x38
GYRO_XOUT_H  = 0x43
GYRO_YOUT_H  = 0x45
GYRO_ZOUT_H  = 0x47

def MPU_Init():
	#write to sample rate register
	bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
	#Write to power management register
	bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
	#Write to Configuration register
	bus.write_byte_data(Device_Address, CONFIG, 0)
	#Write to Gyro configuration register
	bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
	#Write to interrupt enable register
	bus.write_byte_data(Device_Address, INT_ENABLE, 1)

def read_raw_data(addr):
	#Accelero and Gyro value are 16-bit
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)
        #concatenate higher and lower value
        value = ((high << 8) | low)
        #to get signed value from mpu6050
        if(value > 32768):
                value = value - 65536
        return value

bus = smbus.SMBus(1) 	# or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68   # MPU6050 device address


print (" Reading Data of Gyroscope and Accelerometer")

while True:
	#Read Accelerometer raw value
	acc_x = read_raw_data(ACCEL_XOUT_H)
	acc_y = read_raw_data(ACCEL_YOUT_H)
	acc_z = read_raw_data(ACCEL_ZOUT_H)
	#Read Gyroscope raw value
	gyro_x = read_raw_data(GYRO_XOUT_H)
	gyro_y = read_raw_data(GYRO_YOUT_H)
	gyro_z = read_raw_data(GYRO_ZOUT_H)
	#Full scale range +/- 250 degree/C as per sensitivity scale factor
	Ax = acc_x/16384.0
	Ay = acc_y/16384.0
	Az = acc_z/16384.0
	Gx = gyro_x/131.0
	Gy = gyro_y/131.0
	Gz = gyro_z/131.0

	print ("Gx=%.2f" %Gx,"Gy=%.2f" %Gy,"Gz=%.2f" %Gz,"Ax=%.2f g" %Ax,"Ay=%.2f g" %Ay,"Az=%.2f g" %Az) 	

Download code from here

Main code file


In-circuit #diagram we have already gone through connections with the help of bread we connect MPU 6050 sensor with #raspberry pi board through jumping wires.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi

The above picture is after switching on power to raspberry pi. If we observe #LED lights are switched this shows that power supplied throughout the #circuit.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi
i2c communication

The above picture is about i2c communication in raspberry pi.

Interfacing an MPU6050 IMU Sensor with Raspberry Pi

After running the program it reads data of accelerometer and gyroscope.

Video explanation of Interfacing MPU6050 sensor with raspberry pi for better understanding

See Also:

  1. Interfacing DC motor with Arduino

  2. Interfacing servo motor with Arduino

  3. Interfacing sound sensor with Arduino