The MPU6050 sensor has many functions over the single chip. It consists a MEMS accelerometer, a MEMS gyro, and temperature sensor. This module is very accurate while converting analog values to digital because it has a 16bit analog to digital converter hardware for each channel. This module is capable to capture x, y and z channel at the same time. It has an I2C interface to communicate with the host controller. This MPU6050 module is a compact chip having both accelerometer and gyro. This is a very useful device for many applications like drones, robots, motion sensors. It is also called Gyroscope or Triple axis accelerometer.

Today in this article we are going to Interface this MPU6050 with Raspberry Pi and showing the values over 16x2 LCD.

Required Components:

Raspberry Pi

MPU-6050

10K POT

Jumper wire

Breadboard

Power supply

MPU6050 Gyro Sensor:

MPU-6050 is an 8 pin 6 axis gyro and accelerometer in a single chip. This module works on I2C serial communication by default but it can be configured for SPI interface by configuring it register. For I2C this has SDA and SCL lines. Almost all the pins are multi-functioning but here we are proceeding only with I2C mode pins.

Pin Configuration:

Vcc:- this pin is used for powering the MPU6050 module with respect to ground

GND:- this is a ground pin

SDA:-SDA pin is used for data between controller and mpu6050 module

SCL:- SCL pin is used for clock input

XDA:- This is sensor I2C SDA Data line for configuring and reading from external sensors ((optional) not used in our case)

XCL:- This is sensor I2C SCL clock line for configuring and reading from external sensors ((optional) not used in our case)

In this project, we have first shown temperature value over LCD and after some time we show gyro values and then after some time we have accelerometer readings as shown in the images below:

Circuit Diagram and Explanation:

The circuit diagram, for interfacing MPU6050 with Raspberry Pi, is very simple here we have used an LCD and MPU6050. A 10k pot is used for controlling the brightness of the LCD. In connection with MPU6050, we have done 4 connections in which we have connected the 3.3v power supply and ground of MPU6050 to the 3.3v and ground of Raspberry Pi. SCL and SDA pins of MPU6050 is connected with Raspberry’s physical pin 3(GPIO2) and pin 5 (GPIO3). LCD’s RS, RW, and EN are directly connected to GPIO18, and 23 of raspberry pi. Data pin are directly connected to digital pin number GPIO24, GPIO25, GPIO8, and GPIO7. Learn more about interfacing LCD with Raspberry Pi here.

Configuring Raspberry Pi for MPU6050 Gyro sensor:

Before start programming, we need to enable i2c of Raspberry Pi by using given method:

Finally, we need to install the smbus library in Raspberry Pi by using given command:

sudo apt-get install python-smbus

Step 4: Install the library MPU6050

After this we need to install MPU6050 library by using given command

sudo pip install mpu6050

Now we can find example codes in the examples. User can test that code by directly uploading to the Raspberry Pi or customize it according to requirement. Here we have displayed MPU6050’s X, Y and Z axis values on 16x2 LCD. You can find the full Python Code at the end of the Tutorial.

Programming Explanation:

Complete Python Code is given at the end here we are explaining few important part of the code.

In Python Program, we have imported some required library like time, smbus, and GPIO.

import smbus
import time
import RPi.GPIO as gpio

After this, we need to take some register address to configure MPU6050 and for getting values from the same. We have also taken some variables for calibrating and initializing bus for I2C.

def calibrate() function is used to calibrate the MPU6050 and def display() function is used to display the values on LCD. Check these functions in the full code given below.

After this, we have begun the LCD, initialize and calibrate the MPU6050 and then in while loop we have called all the all three set of values from MPU- temperature, accelerometer and gyro and showed them over LCD.

MPU6050 gyro and accelerometer both are used to detect the position and orientation of any device. Gyro uses earth gravity to determine the x,y and z-axis positions and accelerometer detects based on the rate of the change of movement. We already used the accelerometer with Arduino in many of our projects like: