Maker Portal

View Original

Accelerometer, Gyroscope, and Magnetometer Analysis with Raspberry Pi Part I: Basic Readings

“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”

See this content in the original post

A Raspberry Pi will be used to read the MPU9250 3-axis acceleration, 3-axis angular rotation speed, and 3-axis magnetic flux (MPU9250 product page can be found here). The output and limitations of the MPU9250 will be explored, which will help define the limitations of applications for each sensor. This is only the first entry into the MPU9250 IMU series, where in the breadth of the articles we will apply advanced techniques in Python to analyze each of the 9-axes of the IMU and develop real-world applications for the sensor, which may be useful to engineers interested in vibration analysis, navigation, vehicle control, and many other areas.


See this content in the original post

This tutorial uses two primary components: An MPU9250 9-DoF IMU and a Raspberry Pi computer. Any Rapsberry Pi will do as long as it has I2C communication and is capable of running Python 3.x. I have listed the parts and where I purchased them below, along with some other components that may make following along with the tutorial more seamless:

  1. Raspberry Pi 4 Computer - $38.40 - $62.00 [1GB on Amazon, 2GB Amazon, 4GB Amazon], $55.00 [2GB from Our Store]

  2. MPU9250 IMU - $13.00 [Our Store]

  3. Mini Breadboard - $3.00 [Our Store]

  4. Jumper Wires - $0.90 (6 pcs) [Our Store]

  5. Raspberry Pi 4 Kit (HDMI cables, USB cables, etc.) - $97.99 [Amazon]

See this product in the original post

The wiring digram for the MPU9250 and Raspberry Pi is given below (and in the neighboring table). In actuality, I will be using the Rapsberry Pi 4 (despite the diagram stating it is a RPi 3), however the pinout and protocols are all the same. Take note of the second I2C (EDA/ECL) wiring - this is important because it wires both the MPU6050 (accelerometer/gyroscope) and AK8963 (magnetometer) to the RPi’s I2C port. This also means that we will be communicating with two I2C devices (more on this later).

See this content in the original post
See this content in the original post

See this content in the original post

The MPU9250 will communicate with the Raspberry Pi using the I2C protocol. In order to read and write data via I2C, we must first enable the I2C ports on the RPi. The way we do this is either using the command line or by navigating to the Preferences → Raspberry Pi Configuration. Adafruit has a great tutorial outlining this process, but an abridged version will be given below using screenshots of the RPi configuration window.

Once both MPU9250 devices show up on the I2C detector in the RPi command window, we are ready to read the MPU6050 (0x68 device address) and AK8963 (0x0C device address). The reason why we need both addresses is that we have wired them to the same I2C port, so we now use their addresses to control them in a program. In this tutorial, Python will be used. The device addresses can be found in their respective datasheets or by testing them individually by wiring them one-by-one. We know that the MPU6050 (accel/gyro) is 0x68 from its datasheet, and we know the AK8963 (magnetometer) is 0x0C from its datasheet, so the wiring test is not necessary.

If you only see one device address, recheck the wiring; and if no devices are showing up also check the wiring and ensure there is power to the MPU9250 and also that the I2C has been enabled on the RPi! Going forward, it is assumed that the MPU9250 has been wired to the RPi and that the device addresses are identical to the ones given above. Of course, we can easily change the device addresses in the Python code, so if your device for some reason has different addresses, the user will need to change that in the codes that follow.

Lastly, we will need to increase the speed of the I2C baud rate in order to get the fastest response from the MPU9250, which we can do by entering the following into the command line:

All we are doing here is setting the baud rate to 1 Mbps. This should give us a sample rate of about 400Hz - 500Hz (after conversion to real-world units). We can achieve a much higher sample rate for the gyroscope and a slightly higher sample rate for the accelerometer, but that will not be explored in this series.

See this content in the original post

See this content in the original post

In Python, the I2C port can be accessed using a particular library called ‘smbus.’ The smbus is intialized using the following simple routine:

See this content in the original post

Using the ‘bus’ specified in the code above, we will use the MPU9250 Register Map document to gives us information about how to communicate with the accelerometer, gyroscope, and magnetometer (MPU6050 and AK8963). The I2C bus methods used in Python are outside of the scope of this tutorial, so they will not be described in great detail; therefore, the code used to communicate back and forth to the MPU6050 and AK8963 are given below without much description. The full details and capabilities of each sensor are given in the datasheet to the MPU9250, where many questions regarding the registers and corresponding values can be explored, if desired.

See this content in the original post

The code block given above handles the startup for each I2C sensor (MPU6050 and AK8963) and also the conversion from bits to real-world values (gravitation, degrees per second, and Teslas). The code block should be saved in the local folder under the name ‘mpu6050_i2c.py’ - this library will be imported in the example below. All we do is call the conversion script for each sensor and we have the outputs from each of the nine variables. Simple!

The example usage code is given below, along with the sample readouts printed to the Python console:

See this content in the original post

The printout above can be used to verify that the sensor and code are working correctly. The following should be noted:

  • In the z-direction we have a value near 1, this means that gravity is acting in the vertical direction and positive is downward

  • The gyro is reading values close to 0, and in this case we haven’t moved the device so they should be close to 0

  • The magnetometer is showing values between -10μT-40μT in the x,y directions, which is roughly the approximation of the earth’s magnetic field strength in New York City (where the measurements were taken).

With these values verified, we can state that the MPU9250 sensors are working and we can begin our investigations and some simple calculations!


See this content in the original post

Now that we can verify that each sensor is returning meaningful values, we can go on to investigate the sensor in practice. The raw printouts shown in the previous section can be plotted as a function of time:

The code to replicate the plot above is given below:

See this content in the original post

We can also start to explore the sensors by rotating the device. For example, if we flip the sensor on its side such that the x-direction is pointed upwards, we can visualize how each of the 9 degrees of freedom responds:

A 3D, real-time GIF visualization is shown below, where each axis can be tracked. It’s a great visualization tool to understand just how each axis behaves under specific rotations:

A few observations can be made on the plot behavior above:

  1. The rotation about the y-axis results in the gravitational acceleration in the x-direction

  2. The rotation shows a negative angular velocity in the y-direction

  3. The magnetic field shifts from the x and y directions to the y and z-directions

In the upcoming tutorials, I will explore the accelerometer, gyroscope, and magnetometer individually, as well as the fusion of all three to create meaningful relationships to real world engineering.


See this content in the original post

This tutorial introduced the MPU9250 accelerometer, gyroscope, and magnetometer and how to use the Raspberry Pi computer to communicate with the device. Using the I2C protocol, we were able to read 9 different variables, one for each of the three cartesian axes for each of the three sensors. Then, each of the 9 variables was visualized and plotted for a given movement. In the specific example used above, I demonstrated the behavior of each sensor under a given axis rotation, where we learned how each sensor responded. The magnitudes of each sensor are important and provide information about real-world applications, and in the next few tutorials, the accelerometer, gyroscope, and magnetometer will individually explored to great lengths in order to provide a full working sensor fusion system that is able to reproduce physical movements and translations in 3-dimensional space.

See this content in the original post

See More in Raspberry Pi and Sensors:

See this content in the original post