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.
In the image above the pin connections are as follows:
MPU6050-Raspberry Pi (board pins)
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.