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 and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Back to blog

Leave a comment

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