IOT

How to measure Tilt Angle with MPU6050 & STM32 Microcontroller

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.

Hardware Required

  • STM32F103C8T6 Bluepill Development Board
  • MPU6050 6-axis Gyroscope/Accelerometer
  • 0.96″ SSD1306 I2C OLED Display
  • Connecting Wires
  • Breadboard

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.

Features:

  • 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

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 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.

3-Axis Gyroscope:

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.

3-Axis Gyroscope

3-Axis Accelerometer:

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.

3-Axis Accelerometer

APPLICATIONS

  • 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.

STM32F103C8 MPU6050

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.

Source Code/Program

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

#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 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.
STM32 OLED Accelerometer

Conclusion:

Hope all of you understand how to measure Tilt Angle with MPU6050 & STM32 Microcontroller. We, MATHA ELECTRONICS will come back with more informative blogs.

Leave a Reply

Your email address will not be published.