We will interface MPU6050 Gyro/Accelerometer Sensor with STM32 Microcontroller (STM32F103C8) & measure tilt angle. The measured till angle is displayed on OLED Display.
Overview
In this post we will learn how to Measure Tilt Angle using MPU6050 & STM32F103C8 Microcontroller. This can be done by simply interfacing MPU6050 6 axis Gyro/Accelerometer Sensor with STM32. The Accelerometer sends X, Y, and Z acceleration forces. We need to convert the forces into X, Y, Z 3D angle to determine the 3D Orientation of the sensor. The measured tilt angle is sent to the Blynk Application using the Blynk cloud. Hence the tilting position can be monitored over IoT.
The gyroscope measures rotational velocity or rate of change of the angular position over time, along the X, Y and Z-axis. It uses MEMS technology and the Coriolis Effect for measuring. The outputs of the gyroscope are in degrees per second, so in order to get the angular position, we just need to integrate the angular velocity.
You can refer to the previous post where we measured tilt angle using MPU6050 Gyro/Accelerometer & Arduino:
Measure Tilt Angle Using MPU6050. Apart from this the MPU6050 can also be used to measure and control speed of DC Motor.
Bill of Materials
Following are the components required for making this project. All the components can be easily purchased from Amazon. The component purchased link is added.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | STM32F103C Microcontroller | 1 | Amazon | AliExpress |
| 2 | MPU6050 | 1 | Amazon | AliExpress |
| 3 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
| 4 | Connecting Wires | 10 | Amazon | AliExpress |
| 5 | Breadboard | 1 | Amazon | AliExpress |
MPU6050 Gyro/Accelerometer Sensor
Introduction:
The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time. The sensor uses the I2C-bus to interface with the Arduino.
The MPU-6050 is not expensive, especially given the fact that it combines both an accelerometer and a gyro.
MPU6050 Pinout:
The MPU-6050 module has 8 pins:
INT: Interrupt digital output pin.
AD0: I2C Slave Address LSB pin. This is the 0th bit in the 7-bit slave address of device. If connected to VCC then it is read as logic one and 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 SDA pin to MPU-6050.
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.
3-Axis Gyroscope:
The MPU6050 consist of 3-axis Gyroscope with Micro Electro Mechanical System(MEMS) technology. It is used to detect rotational velocity along the X, Y, Z axes as shown in the below figure.
3-Axis Accelerometer:
The MPU6050 consist 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect angle of tilt or inclination along the X, Y, and Z axes as shown the below figure.
Circuit: Measure Tilt Angle with MPU6050 & STM32
Here is the circuit diagram for interfacing MPU6050 Gyro/Accelerometer with STM32 Microcontroller (STM32F103C8). The circuit can be assembled on breadboard.
Both the MPU6050 & OLED display are I2C Module. So we need only 2 wires for interfacing them with STM32. So, connect the Serial Data (SDA) pins to STM32F103C8 B7 pin & Serial Clock (SCL) to STM32F103C8 B6 pin. Supply 3.3V power to both modules.
Source Code/Program
The source code/program for interfacing MPU6050 with STM32 is given below. We will need few libraries to compile the code. So download the libraries from the link below and add it to Arduino Library.
- Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
- Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include<Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #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; int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; int minVal=265; int maxVal=402; double x; double y; double z; void setup(){ Wire.begin(); Wire.beginTransmission(MPU_addr); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); Serial.begin(9600); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } display.display(); delay(2); display.clearDisplay(); display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(0,5); display.print("MPU Angles"); display.display(); delay(3000); } void loop() { Wire.beginTransmission(MPU_addr); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(MPU_addr,14,true); AcX=Wire.read()<<8|Wire.read(); AcY=Wire.read()<<8|Wire.read(); AcZ=Wire.read()<<8|Wire.read(); 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); Serial.print("AngleX= "); Serial.println(x); Serial.print("AngleY= "); Serial.println(y); Serial.print("AngleZ= "); Serial.println(z); Serial.println("-----------------------------------------"); display.clearDisplay(); display.setTextSize(2); display.setCursor(0,0); display.print("X: "); display.println(x); display.setTextSize(2); display.setCursor(0,20); display.print("Y: "); display.println(y); display.setTextSize(2); display.setCursor(0,40); display.print("Z: "); display.println(z); display.display(); delay(1000); } |
Output/Results
So once the code is uploaded, you can click on serial monitor to check the outputs. You need to tilt the MPU6050 Gyro/Accelerometer to detect the angular position of X, Y & Z axis.
The same tilting angles can be observed on OLED Display as well.














1 Comment
i get this error while trying to compile this sketch
Arduino: 1.8.13 (Windows 10), Board: “Generic STM32F103C series, STM32F103C8 (20k RAM. 64k Flash), STM32duino bootloader, 72Mhz (Normal), Smallest (default)”
C:\Users\reisi\Documents\Arduino\libraries\Adafruit_BusIO-master\Adafruit_SPIDevice.cpp: In member function ‘void Adafruit_SPIDevice::transfer(uint8_t*, size_t)’:
C:\Users\reisi\Documents\Arduino\libraries\Adafruit_BusIO-master\Adafruit_SPIDevice.cpp:115:31: error: no matching function for call to ‘SPIClass::transfer(uint8_t*&, size_t&)’
C:\Users\reisi\Documents\Arduino\libraries\Adafruit_BusIO-master\Adafruit_SPIDevice.cpp:115:31: note: candidate is:
In file included from C:\Users\reisi\Documents\Arduino\libraries\Adafruit_BusIO-master/Adafruit_SPIDevice.h:1:0,
C:\Users\reisi\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2020.6.20\libraries\SPI\src/SPI.h:286:11: note: uint8 SPIClass::transfer(uint8) const
C:\Users\reisi\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2020.6.20\libraries\SPI\src/SPI.h:286:11: note: candidate expects 1 argument, 2 provided
Multiple libraries were found for “Wire.h”
Used: C:\Users\reisi\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2020.6.20\libraries\Wire
Not used: C:\Users\reisi\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2020.6.20\libraries\WireSlave
exit status 1
Error compiling for board Generic STM32F103C series.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.