
Interfacing MPU6050 accelerometer with Raspberry Pi
In this article, we will look at theΒ MPU6050 accelerometerΒ and interface it with aΒ Raspberry Pi. The MPU6050 sensor module is a complete 6-axis Motion Tracking Device. It combines a 3-axis Gyroscope, a 3-axis Accelerometer and a Digital Motion Processor all in a small package. It has an I2C bus interface to communicate with the host controller (R-Pi).
The MPU6050 has 8 pins as mentioned below:
- VCC: +5V DC power supply pin
- GND: Ground pin
- SCL: Serial Clock pin
- SDA: Serial Data pin
- XDA: Auxiliary Serial Data pin
- XCL: Auxiliary Serial Clock pin
- AD0: Slave Address select pin
- INT: Interrupt pin
The AD0 pin is used to change the I2C address of the MPU6050. Connecting this pin to +5v sets the address of the sensor to 0x69 and connecting it to ground sets it to 0x68. This is helpful if you have two MPU6050βs or another sensor with the same I2C address on the I2C bus.
Β
The connection diagram is as shown below. This will work for bothΒ Raspberry Pi 3 BΒ andΒ Raspberry Pi 3B+Β variants.
In the image above the pin connections are as follows:
MPU6050-Raspberry Pi (board pins)
VCC-4
GND-34
SDA-3
SCL-5
Β
Refer to the python code for reading accelerometer readings from the MPU6050 and displaying on the terminal. Link for the code:Β https://github.com/Robocraze/MPU6050
Β
Upload the above code to a raspberry pi and run it using python3. Running it will print the accelerometer and gyroscope value along the three axes once every 500 ms on the terminal.
Β
Β
In the code above we have created a class for the mpu6050 which abstracts all the functions that will be required to get readings from and set parameters of the mpu6050. You can use that class as a tool to develop various applications using the mpu6050.
This application can be extended much further to detect the orientation of robots, their location (using a technique called inertial navigation), to build self-balancing bots, in quadcopters for roll, pitch, yaw angle calculations and many more.
Β
The MPU6050 accelerometer can be used with other microcontrollers as well. Libraries for a few microcontroller platforms are given below.
Arduino:Β https://www.arduino.cc/reference/en/libraries/mpu6050/
ESP32/8266:Β https://www.arduinolibraries.info/libraries/mpu6050
Tutorial
Β