Como usar o sensor de rastreamento de movimento MEMS MPU6050 de 6 eixos (giroscópio + acelerômetro) com Arduino

How to use MPU6050 6-Axis MEMS Motion Tracking Sensor (Gyroscope + Accelerometer) with Arduino

Motion tracking sensors are used in applications such as robotics, gesture recognition, vehicle stabilization, position control in drones/quadcopters, pointing devices, game controllers, and fitness tracking devices. One of the popular motion tracking sensors is the MPU6050. It is a six-axis MEMS motion tracking sensor that includes a MEMS accelerometer and a MEMS gyroscope. The sensor also has an integrated temperature sensor where the temperature measurement can be used to calibrate the accelerometer and gyroscope. Additionally, the MPU6050 can be connected to an external magnetometer using an integrated I2C channel that extends the sensor to 9 DOF (Degree of Freedom). With an external magnetometer connected to the MPU6050, a controller or computer can accurately track the exact movement.

MPU6050 Motion Detection Sensor

MPU6050 Motion Detection Sensor Example

The MPU6050 breakout board has a 3.3V voltage regulator that allows easy interfacing with 5V and 3.3V computers. Therefore, we can easily connect the MPU6050 with Arduino, Raspberry Pi, ESP32 and various other microcontrollers and SBC platforms . In this project, we connect the MPU6050 motion tracker with Arduino and display its accelerometer, gyroscope and temperature readings on an SSD1306 OLED display.

Required components

  1. Arduino UNO x1
  2. MPU6050 sensor x1
  3. SSD1306 x1 OLED Module
  4. Connecting wires/jumper wires

Required software

  1. Arduino IDE

How the MEMS accelerometer works
Accelerometers are used for detecting linear acceleration. These sensors can detect linear acceleration in one, two, or three dimensions. An accelerometer has a test mass suspended by springs within a reference frame. The test mass in a one-dimensional accelerometer has one degree of freedom. Similarly, the test mass in a two-dimensional and three-dimensional accelerometer is 2 DOF and 3 DOF within the frame, respectively. The test mass acts as a suspended ball free to move within a cubic structure. When the sensor moves along a given axis, the suspended test mass experiences a force in the opposite direction.

Accelerometer Operation

Accelerometer working principle

If the acceleration detected by the test mass is equivalent to the gravitational acceleration, that is, 9.8 m/s, it is said to be 1g. If a three-dimensional accelerometer detects a force of 1g in the Z direction, it is detecting gravitational acceleration.

The MEMS accelerometer consists of a micromachined structure built on a silicon wafer. A test mass is suspended by polysilicon springs where it is free to deflect in one, two or three dimensions when subjected to an external force. The test mass deflects between fixed plates that act as electrodes of a capacitor. In deflection along an axis, the distance between the test mass and the fixed plates is changed in proportion to the acceleration along that axis. This produces a proportional change in the capacitance of the fixed plates. The difference in capacitance is detected by a high-precision sensor that outputs a proportional analog voltage.

MEMS Accelerometer Structure

MEMS Accelerometer Structure

How the MEMS gyroscope works
A gyroscope is used to detect angular rotation. It uses the phenomenon called the Coriolis Effect. The Coriolis force is an inertial force experienced by an object moving in a rotating system perpendicular to the direction of motion and the axis of rotation.

Coriolis effect on gyroscope

Coriolis effect example

A gyroscope consists of two test masses that are in oscillating motion in opposite directions. When an angular force is applied to them due to the rotation of the inertial structure, Coriolis forces act on the two masses in opposite directions. This causes the masses to deflect in opposite directions along the Coriolis Force axis, causing a change in the capacitance between them.

MEMS gyroscope working principle

MEMS gyroscope working principle

The MEMS gyroscope consists of a test mass divided into four parts. A fixed mass suspends each part in the center. The masses continually oscillate in and out along the horizontal plane.

MEMS gyroscope structure

MEMS gyroscope structure

When the gyroscope rotates, the test masses passing through a horizontal oscillating force experience a vertical force due to the Coriolis Effect. There are three modes based on the axis of rotation.

When the axis of rotation is the X axis, plates M1 and M3 move up and down, deviating from the horizontal plane. This is called Roll mode.

Roll mode in the MEMS gyroscope

Roll mode in the MEMS gyroscope

When the axis of rotation is the Y axis, plates M2 and M4 move up and down, deviating from the horizontal plane. This is called Pitch Mode.

Pitch mode in MEMS gyroscope

Pitch mode in MEMS gyroscope

When the axis of rotation is the Y axis, plates M1 and M3 move in the opposite direction in the horizontal plane, and at the same time plates M2 and M4 also move in the opposite direction in the horizontal plane. This is called Yaw Mode.

Yaw mode in MEMS gyroscope

Yaw mode in MEMS gyroscope

When deflection in the driving masses causes a change in capacitance detected by a high precision sensor and is converted into a proportional voltage signal.

MPU6050 Motion Tracking Sensor
MPU6050 is a low-cost 6 DoF motion tracking sensor with a 3-axis on-chip accelerometer and a 3-axis on-chip gyroscope. It also includes a temperature sensor that measures the operating temperature of the silicon die in the range of -40˚C to 85˚C. It should be noted that the temperature sensor built into the MPU6050 does not read the ambient temperature; it reads the operating temperature of the IC. Temperature measurements are used for accelerometer and gyroscope calibration. Temperature measurement can also be used to detect temperature changes. MPU6050 has the following pin diagram

MPU6050 sensor pin configuration

MPU6050 accelerometer pinout plus gyroscope

MPU6050 finds its importance due to its on-chip Digital Motion Processor (DMP). The sensor's DMP does all the complex calculations, sending direct acceleration and rotation values ​​to any external controller/computer via an I2C interface. The sensor has two 16-bit analog-to-digital converters on the chip. A 16-bit ADC simultaneously converts three-axis linear acceleration values ​​detected by the on-chip accelerometer, which can be adjusted to four programmable scales – +/-2g, +/-4g, +/-8g and +/- 16g. Another 16-bit ADC simultaneously converts angular acceleration values ​​into three rotation axes (roll mode, pitch mode, and yaw mode) detected by the on-chip gyroscope. The gyroscope can be adjusted to four programmable ranges of +/-250˚/s, +/-500˚/s, +/-1000˚/s and +/-2000˚/s.

The MPU6050 communicates with a controller/computer on the I2C interface. It has two I2C addresses – 0x68 and 0x69 to avoid any conflict with other I2C devices sharing the same bus. The I2C address of the MPU6050 can be controlled from the ADO pin. If the ADO pin is not connected, the module will have a default I2C address of 0x68. If the ADO pin is connected to 3.3V, the module will have an I2C address of 0x69.

It is possible to connect an external magnetometer to the MPU6050. The breakout board has a separate I2C bus on XDA and XCL pins. The interrupt board also has an interrupt pin to trigger a hardware interrupt upon motion, shock, or drop detection.

MPU6050 interface with Arduino
It's simple to interface the MPU6050 with Arduino. The MPU6050 breakout board has an LD3985 3.3V regulator. Thus, we can connect the module directly to any 5V Arduino. The sensor can be powered by the Arduino power supply, and the module's I2C pins must be connected to the Arduino's I2C port. The I2C ports on Arduino UNO are shown in the image below.

I2C ports on Arduino UNO

I2C ports on Arduino UNO

The interrupt pin of the MPU6050 can be connected to any GPIO on the Arduino.

Circuit Connections
This project has an MPU6050 interface with Arduino UNO and displays temperature, accelerometer and gyroscope readings on an SSD1306 OLED display. For the MPU6050 interface, the VCC and GND pins of the MPU6050 are connected to the 5V output and ground pins of the Arduino; the module's SDA and SCL pins are connected to the Arduino's I2C port, and the module's interrupt pin is connected to the Arduino's D2 pin.

The SSD1306 interfaces with the Arduino using the Arduino's physical SPI port. To interface the SSD1306 OLED via physical SPI port, connect the D0/SCK and D1/MOSI pins of the SSD1306 OLED to the D13 and D11 pins of the Arduino, respectively. Connect the DC, RESET and CS pins of the SSD1306 to the D9, D10 and D8 pins of the Arduino respectively.

MPU6050 interface with Arduino

MPU6050 interface circuit diagram with Arduino

Arduino Libraries for MPU6050
There are several Arduino libraries available to work with MPU6050. Adafruit's Adafruit MPU6050 library is quite popular. We use Arduino-MPU6050 library provided by jarzebski from Github in this project. The library is licensed under GPL 3.0.

Arduino Sketch

Programming guide
The sketch starts with importing the Wire and Arduino-MPU6050 libraries for I2C communication with the MPU6050 sensor. The libraries for working with SSD1306 OLED are imported next. A constant 'period' is defined to store the time interval for reading the values ​​of the built-in temperature sensor, accelerometer and gyroscope one by one. Constants for screen resolution and OLED display pin assignment are declared. Objects of class Adafruit_SSD1306 and MPU6050 are instantiated. Next, a bitmap and an array are defined to store the website logo.

In the setup function, the baud rate for serial communication with the Serial Monitor is set to 9600 bps. The MPU6050 sensor is tested using the mpu.begin method and is calibrated by calling the mpu.calibrateGyro method. The sensor scale is set by calling the mpu.setThreshold method. The OLED screen starts up and the website logo flashes on it.

In the loop function, readings from the built-in temperature sensor, accelerometer and gyroscope are sampled with a 1 second delay and displayed on the OLED screen. The temperature is obtained by calling the mpu.readTemperature method. Gyroscope readings are obtained by calling the mpu.readRawGyro and mpu.readNormalizeGyro methods. Normalized values ​​for roll, pitch, and yaw modes are retrieved by calling the normGyro.XAxis, normGyro.YAxis, and normGyro.ZAxis properties, respectively. Acceleration values ​​are obtained by calling the mpu.readRawAccel and mpu.readNormalizeAccel methods. The normalized acceleration values ​​on the X, Y, and Z axes are retrieved by calling the normAccel.XAxis, normAccel.YAxis, and normAccel.ZAxis properties, respectively.

Results

Related Content

Back to blog

Leave a comment

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