How to Integrate the MPU6050 With the STM32 Blue Pill
by mahmoodmustafashilleh in Circuits > Arduino
520 Views, 0 Favorites, 0 Comments
How to Integrate the MPU6050 With the STM32 Blue Pill
If you’re looking to add motion detection to your projects, the MPU6050 sensor is a great option. When combined with the STM32 Blue Pill, a powerful yet affordable microcontroller, you have a versatile solution for accelerometer and gyroscope-based applications. In this tutorial, we’ll guide you through the process of connecting the MPU6050 to the STM32 Blue Pill, setting up the Arduino environment, reading raw data from the sensor, and applying a simple gyroscope calibration.
Be sure to watch part 1 if you have not already. Where we show how to get setup with the board in Arduino! This is crucial to watch if you have not.
— — -
Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:
- Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
- Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
- Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.
ShillehTek Website (Exclusive Discounts):
https://shillehtek.com/collections/all
ShillehTekAmazon Store:
Supplies
ShillehTek Pre-Soldered STM32
ShillehTek MPU6050
ShillehTek Jumper Wires and Breadboard
Hardware Connections
The first step is to connect the MPU6050 to the STM32 Blue Pill. Here’s how to wire them together:
Power Supply:
- Connect the VCC pin of the MPU6050 to the 3.3V pin on the STM32 Blue Pill.
- Connect the GND pin of the MPU6050 to the GND pin on the STM32 Blue Pill.
I2C Communication:
- Connect the SCL pin of the MPU6050 to the PB6 pin on the STM32 Blue Pill (default SCL pin for I2C).
- Connect the SDA pin of the MPU6050 to the PB7 pin on the STM32 Blue Pill (default SDA pin for I2C).
Install the MPU6050 Library
Go to Sketch > Include Library > Manage Libraries and search for “MPU6050.” Install the library compatible with your setup.
Writing the Code
Let’s start with the basic version of the code, which initializes the MPU6050 and reads raw accelerometer and gyroscope data.
Code Without Calibration
Explanation:
- Initialization: The MPU6050 is initialized, and a connection test is performed to ensure proper setup.
- Reading Raw Values: The code reads raw values from both the accelerometer and gyroscope and prints them to the Serial Monitor.
This version of the code is straightforward and works well if you don’t need to correct for any offsets in the gyroscope readings. However, for more accurate data, especially in applications where precise motion tracking is crucial, it’s recommended to apply a simple calibration to the gyroscope.
Code with Gyro Calibration
Now, let’s modify the code to include a basic calibration for the gyroscope.
Explanation:
- Calibration Function: The calibrateGyro() function averages 1000 readings from the gyroscope while the sensor is stationary. These average values (offsets) are then subtracted from the subsequent readings to correct any bias.
- Applying Offsets: In the loop() function, after reading the gyroscope data, the offsets are subtracted from the raw values to correct them.
- Output: The calibrated gyroscope values, along with the raw accelerometer values, are printed to the Serial Monitor.
Running the Code
Upload either version of the code to your STM32 Blue Pill using the Arduino IDE. After the upload is complete:
- Open the Serial Monitor from the Arduino IDE (Tools > Serial Monitor).
- Set the baud rate to 115200.
- Observe the raw accelerometer and gyroscope values being printed. If using the calibrated version, the gyroscope readings should now be corrected for any static offset.
Conclusion
By following this guide, you’ve successfully integrated the MPU6050 sensor with the STM32 Blue Pill and implemented a simple calibration method for the gyroscope. Whether you’re working on a robotics project, a drone, or any other motion-based application, this setup provides accurate motion detection for a variety of applications.
This tutorial gives you a solid foundation to build upon, and you can further explore by experimenting with different settings or adding additional sensors. If you found this tutorial helpful, be sure to check out our video on the topic and subscribe for more tutorials!