In this blog, we’ll learn how to measure tilt angle with an MPU6050 and an STM32F103C8 microcontroller. This is achieved by connecting the MPU6050 6 axis Gyro/Accelerometer Sensor to the STM32. The Accelerometer provides acceleration forces in the directions of X, Y, and Z. To establish the sensor’s 3D Orientation, we must transform the forces into X, Y, and Z 3D angles. The tilt angle is measured and delivered to the Blynk Application via the Blynk cloud. As a result, the tilting position can be tracked via IoT.
The rotational velocity, or rate of change of angular position over time, is measured by the gyroscope along the X, Y, and Z axes. It measures using MEMS technology and the Coriolis Effect. Because the gyroscope’s outputs are in degrees per second, all we have to do is integrate the angular velocity to get the angular location.
We’ll measure tilt angle by connecting an MPU6050 Gyro/Accelerometer Sensor to an STM32 Microcontroller (STM32F103C8). On the OLED Display, the measured till angle is presented.
- STM32F103C8T6 Bluepill Development Board
- MPU6050 6-axis Gyroscope/Accelerometer
- 0.96″ SSD1306 I2C OLED Display
- Connecting Wires
MPU6050 Gyro/Accelerometer Sensor
The MPU-6050 is a sensor based on Micro Electrical Mechanical System (MEMS) technology. In this sensor module, the 3-axis accelerometer and the 3-axis gyroscope are embedded in a single chip using the I2C protocol via the serial clock (SCL) and data (SDA). MPU-6050 measures the acceleration, velocity, orientation, displacement, and many other motion-related parameters of a system or an object. An onboard Digital Motion Processor in this module is used to process the 6 axis fusion algorithm. Thus providing a complete 9-axis motion fusion output by accessing the external magnetometer.
The MPU6050 sensor has 8pins, the pins are following VCC, GND, SCL, SDA, XDA, SCL, AD0, and INT. This sensor chip operates at a voltage of 3.3V but a voltage regulator on the GY-521 board allows you to give it up to 5V. The devices were designed in a 4 mm x 4 mm x 0.9 mm QFN package. The MPU-6050 features a user-programmable gyroscope that allows a full-scale range of ±250, ±500, ±1000, and ±2000°/sec (DPS). Also provides a user-programmable accelerometer full-scale range of +/-2G to +/- 16G. I2C communications at up to 400kHz are supported with a VLOGIC pin that defines its interface voltage levels.
The MPU-6050 is known as the world’s first Motion Tracking device designed specifically for the low power, low cost, and high-performance requirements of smartphones, tablets, and wearable sensors. The module also consists of two auxiliary pins to interface external IIC modules like a magnetometer. Since this IIC address is configurable, more than one MPU6050 sensor can be interfaced to a Microcontroller using the AD0 pin.
- MEMS 3-axis accelerometer and 3-axis gyroscope values combined
- Power Supply: 3-5V
- Communication: I2C protocol
- Built-in 16-bit ADC provides high accuracy
- Sensing range: +/-2G to +/-16G
- Built-in DMP provides high computational power
- Can be used to interface with other IIC devices like a magnetometer
- Configurable IIC Address
- In-built temperature sensor
- Ideal for a noisy power environment
- Operating temperature: -40 degrees to 85 degrees Celsius
- Dimension: 28*14mm
The MPU-6050 module has 8 pins:
- INT: Interrupt digital output pin.
- AD0: I2C Slave Address LSB pin– This is the 0th bit of the device’s 7-bit slave address. When VCC is connected, it is read as logic one, and the slave address changes.
- XCL: Auxiliary Serial Clock pin-This pin is used to connect other I2C interface enabled sensors SCL pin to MPU-6050.
- XDA: Auxiliary Serial Data pin-This pin is used to connect other I2C interface-enabled sensors to the MPU-6050’s SDA pin.
- SCL: Serial Clock pin– Connect this pin to the microcontroller SCL pin.
- SDA: Serial Data pin- Connect this pin to the microcontroller SDA pin.
- GND: Ground pin- Connect this pin to the ground connection.
- VCC: Power supply pin- Connect this pin to a +5V DC supply.
The MPU6050 is a three-axis gyroscope that uses MEMS (Micro Electro Mechanical System) technology. As indicated in the diagram below, it is used to detect rotational velocity along the X, Y, and Z axes.
The MPU6050 is a three-axis accelerometer that uses Micro-Electro-Mechanical (MEM) technology. As indicated in the diagram below, it is used to detect the angle of tilt or inclination along the X, Y, and Z axes.
- Used for IMU measurement
- Drones / Quadcopters
- Self-balancing robots
- Robotic arm controls
- Humanoid robots
- Tilt sensor
- Orientation / Rotation Detector
Circuit: Measure Tilt Angle with MPU6050 & STM32
The circuit diagram for connecting the MPU6050 Gyro/Accelerometer to the STM32 Microcontroller is shown below (STM32F103C8). On a breadboard, the circuit can be built.
The MPU6050 and the OLED display are both I2C modules. So we only need two wires to connect them to the STM32. Connect the Serial Data (SDA) and Serial Clock (SCL) pins to the STM32F103C8 B7 and B6 pins, respectively. Both modules should be powered at 3.3 volts.
Below is the source code/program for connecting MPU6050 with STM32. To compile the code, we’ll need a few libraries. So, go to the website below and download the libraries, then add them to the Arduino Library.
1. Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
2. Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int MPU_addr=0x68;
Serial.println(F(“SSD1306 allocation failed”));
for(;;); // Don’t proceed, loop forever
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
So, once the code has been uploaded, you can examine the outputs by clicking on the serial monitor. To detect the angular position of the X, Y, and Z axes, tilt the MPU6050 Gyro/Accelerometer.
- On an OLED Display, the same tilting angles can be seen.
Hope all of you understand how to measure Tilt Angle with MPU6050 & STM32 Microcontroller. We, MATHA ELECTRONICS will come back with more informative blogs.