MPU6050 Gyro Sensor Interfacing With Raspberry Pi

· 3 min read
MPU6050 Gyro Sensor Interfacing With Raspberry Pi

The MPU6050 sensor has many functions over the only chip. It consists a MEMS accelerometer, a MEMS gyro, and temperature sensor. This module is very correct while converting analog values to digital as a result of it has a 16bit analog to digital converter hardware for each channel. This module is capable to seize x, y and z channel at the identical time. It has an I2C interface to speak with the host controller. This MPU6050 module is a compact chip having both accelerometer and gyro. This is a very helpful device for many purposes like drones, robots, movement sensors. Additionally it is referred to as Gyroscope or Triple axis accelerometer.

Immediately in this text we're going to Interface this MPU6050 with Raspberry Pi and exhibiting the values over 16x2 LCD.


1. Raspberry Pi 2. MPU-6050 3. 10K POT 4.  MEMS gyroscope . Breadboard 6. Energy supply
MPU-6050 is an 8 pin 6 axis gyro and accelerometer in a single chip. This module works on I2C serial communication by default however 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 however right here we are proceeding only with I2C mode pins.


GND:- this can be a ground pin
We've beforehand interfaced MPU6050 with Arduino.

In this article, we're showing temperature, gyro and accelerometer readings over LCD using MPU6050 with Raspberry Pi. If you are new to Raspberry Pi then go through our Raspberry Pi tutorials section and learn getting began with Raspberry Pi.

On this challenge, we have first shown temperature worth over LCD and after some time we present gyro values and then after some time we have accelerometer readings as shown in the pictures under:

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


Configuring Raspberry Pi for MPU6050 Gyro sensor:
Earlier than start programming, we need to allow i2c of Raspberry Pi through the use of given method:


Step 1: Allow I2C communication
Earlier than putting in Adafruit SSD1306 library we need to allow I2C communication in Raspberry Pi.


To do this sort in Raspberry Pi console:
After which a blue screen will appear. Now select interface possibility


After this, we need to wish to select I2C
After this, we want to pick yes and press enter and then okay


After this, we need to reboot raspberry pi by issuing under command:
Step 2: Set up python-pip and GPIO Library


After this, we want to put in raspberry pi GPIO library
Step 3: Install the smbus library


Finally, we'd like to install the smbus library in Raspberry Pi through the use of given command:
Step 4: Install the library MPU6050


After this we need to install MPU6050 library by utilizing given command
Now we are able to discover example codes in the examples. User can take a look at that code by straight uploading to the Raspberry Pi or customise it in response to requirement. Right here we've got displayed MPU6050’s X, Y and Z axis values on 16x2 LCD. You can find the full Python Code at the top of the Tutorial.


Full Python Code is given at the end right here we are explaining few important a part of the code.
In Python Program, we've imported some required library like time, smbus, and GPIO.

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

Then now we have written some capabilities for Driving 16x2LCD like def begin(), def cmd(ch), def write(ch), def Print(str), def clear() and so forth. You can further verify Interfacing of LCD with Raspberry Pi.


After this, we have to initialize the MPU6050 Module
After this, we want to write some features to learn values from MPU6050 and display them to LCD. Given function is used to learn information from MPU6050


Given function is used to learn accelerometer and gyro meter information
After this, we've got written a temperature reading operate

def calibrate() operate is used to calibrate the MPU6050 and def display() operate is used to show the values on LCD. Check these capabilities in the total code given beneath.

After this, now we have begun the LCD, initialize and calibrate the MPU6050 and then in while loop we've got referred to as all of the all three set of values from MPU- temperature, accelerometer and gyro and confirmed them over LCD.