How to Connect MPU9250 and Raspberry Pi (Part 1)

Discover how to capture magnetometer, accelerometer, and gyroscope data from the MPU9250 9-axis sensor using the Raspberry Pi. Although I'm utilizing the Raspberry Pi 4B for this guide, you can apply these instructions to any Raspberry Pi model that supports Python. By the end of this tutorial, you'll be proficient in sampling data and establishing a physical connection between the MPU9250 and your Raspberry Pi.
Before reading the remainder, be sure to subscribe and support the channel if you have not!
Subscribe:
Support:
Hire me at UpWork to build your IoT projects:

Part 1: Physical Setup

Connect your Jumper Wires to your Raspberry Pi as shown in the diagram above.
You can purchase Jumper Wires here. This is a simple I2C connection with power, you only need four wires to do this. I recommend a breadboard as well for these sorts of projects.

Part 2: Library and System Setup

Before connecting the MPU9250, ensure that I2C is enabled on your Raspberry Pi:
  • Open the Raspberry Pi Configuration Tool from the Preferences menu in the desktop environment, or run sudo raspi-config in the terminal.
  • Navigate to Interface Options.
  • Select I2C and enable it.
  • Reboot your Raspberry Pi.
You'll need some libraries to communicate with the MPU9250. We'll use Python for our example:
  • Update your package list: sudo apt-get update
  • Install Python's package manager if you haven't: sudo apt-get install python3-pip
  • Install the necessary Python libraries for I2C: sudo pip3 install smbus2
  • Install a library specifically for the MPU9250, such as mpu9250-jmdev: sudo pip3 install mpu9250-jmdev

Part 3: Python Code

With our system and hardware now fully prepared, we're ready to dive into the coding aspect of our project. The Python script below demonstrates a straightforward approach to continuously reading and displaying data from all nine degrees of freedom provided by the MPU9250 sensor. This basic example serves as a foundation for more complex applications.
https://github.com/shillehbean/youtube-p2/blob/main/mpu9250_basic.py
In this snippet, we create an instance of the MPU9250 class using specific parameters that define the sensor's configuration. Noteworthy among these are gfs, afs, and mfs, which determine the full-scale ranges for the gyroscope, accelerometer, and magnetometer, respectively. These parameters are crucial as they allow you to tailor the sensitivity and range of the measurements to fit your project's requirements. Adjusting them can significantly impact the precision and nature of the data you collect, making your sensor more adaptable to different scenarios.
For users with a genuine MPU9250 sensor, this script should execute seamlessly. The ease with which we can instantiate and configure the MPU9250 object with this library underscores its versatility. Whether you're looking to capture subtle movements or detect more significant orientations and magnetic fields, adjusting the configuration parameters enables fine-tuning of the device's responsiveness to physical phenomena.
Once you run the provided script, your console will begin displaying a stream of data captured from the MPU9250 sensor, including readings from the accelerometer, gyroscope, and magnetometer. It's important to understand the units and scale of these values to interpret them correctly:
  • Accelerometer Values: The acceleration data is presented in units of 'G', where 1 G represents the acceleration due to gravity at the Earth's surface (approximately 9.81 m/s^2). This means if the sensor is lying flat on a stable surface, you can expect the Z-axis to show a value close to ±1 G (depending on its orientation), while the X and Y axes should be near 0 G, indicating no acceleration.
  • Gyroscope Values: The gyroscope readings are given in degrees per second (°/s). These values represent the rate of rotation around each of the sensor's three axes. When the sensor is stationary, the gyroscope values should be close to 0, indicating no rotation. As the sensor rotates around any of its axes, the gyroscope detects this movement and outputs the rate of rotation.
  • Magnetometer Values: The magnetometer readings are provided in microteslas (µT) and represent the strength and direction of the magnetic field around the sensor. Unlike the accelerometer and gyroscope, the magnetometer measures environmental magnetic fields, which include the Earth's magnetic field. This makes it invaluable for applications like compass navigation or detecting magnetic anomalies.
Finally one thing you will notice is that the resting values for the device may seem off, this is expected and we will discuss a simple calibration process in the next tutorial to correct for this flaw, as all accelerometers come with an inherent inaccuracy that needs to be corrected for. Stay tuned for the next video and let me know if you have any questions!

Conclusion

We showed you how to easily connect to the MPU9250 using a convenient library in Python and how to interprate values with a simple script. In the next videos we will build upon this and calibrate the accelerometer, gyro, and magnetometer to get more accurate values.
Do not forget to subscribe to Shilleh on YouTube for more!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

Raspberry Pi Pico/Pico W Free Simulator

Raspberry Pi Pico/Pico W Free Simulator

Discover how to simulate Raspberry Pi Pico projects using Wokwi, a free online simulator for Arduino and MicroPython....

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interface with the MPU6050 using the Raspberry Pi Pico W in C++.

How to Write your First C++ Program on the Raspberry Pi Pico W

How to Write your First C++ Program on the Raspberry Pi Pico W

Write your first C++ Program on the Pico W in a few simple steps.

How to Use ThingSpeak with the Raspberry Pi Pico W

How to Use ThingSpeak with the Raspberry Pi Pico W

Learn how to create a real-time environmental monitoring system with the Raspberry Pi Pico W and ThingSpeak!

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.