In this instructable, I am gonna share with you people a way to fetch angle from MPU6050 with raspberry pi using python. I am writing this article because I could not find any in internet that guide us to use MPU6050 to find angle with raspberry pi using python. We use kalman-filter a fusion algorithm that was used in Apollo missions (not kidding). Thanks to TJK for explaining such a complex algorithm so that dummies with little and decent knowledge in math (like me) can understand the working of Kalman filter. If you are interested go through his blog TJK's blog explaining kalman-filter
He has implemented his algorithm in C++ . I cannot find the python implementation of this algorithm anywhere in internet. So I thought I would make a python implementation of his algorithm so that python users can use it to find angle with raspberry pi.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Appratus Required :)
- Raspberry pi and its dependencies ( Means to display, power source and you know what else is required)
- MPU6050 (Obviously)
- Jumpers - (female to female - depends on your MPU6050 module)
Step 2: Let's Get Em' Connected
In case you are not aware, MPU6050 uses a communication protocol called I2C (pronounced I squared C). It is a very powerful one - all it takes is two wires for SDA and SCL and the maximum number of devices that can be used in the same is limited by the hardware limitations (You should atleast be able to connect upto 128 devices). In our case, raspberry pi acts as the master and the MPU6050 acts as the slave.
If you are interested in learning the working of I2C, Here it is.
Alright. Let's get to work.
Let's get em' connected:
Connections are pretty simple.
MPU6050 ---------- Raspberry Pi
VCC ---------- 5V (pins 2 or 4)
GND ----------- GND(pin 6)
SDA ----------- SDA(pin 5)
SCL ----------- SCL(pin 3)
If you don't know the pin configuration of the raspberry pi, you can google it. You can find the pin configuration of raspberry pi 3 here.
You can also take a look at the connection diagram and help yourself.(In the diagram MPU6050's GND is connected to the 34th pin of raspberry pi. That is ground pin too. So don't get confused. you can connect it either way. )
Step 3: Let's Get Em' Working
Download the code:
You can download or copy the code from this git-hub link. I would prefer download over copy because python throws “inconsistent use of tabs and spaces in indentation” sometimes if you copy and paste the code. Find out why here.
Run the program:
Once you have copied the code, open AngleOMeter.py and run it. Make sure you copy both the files AngleOMeter.py and Kalman.py and they are in the same folder (directory). You are all set to go. Tilt the MPU6050 module and the angle in the screen should change.