4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)

Video: https://youtu.be/yhz3bRQLvBY

#Robotics #Drones #Arduino #Hardware #Teensy_4

Full code and manual on GitHub
Quadcopter frame PCB on OSHW lab

🔙 Previous Part | Next Part 🔜

↩️ Go Back

Table of Contents:


A) Introduction to Rotation Rates: roll, pitch, and yaw.

Welcome to the fourth video session in our series where you learn how to build and program your own Teensy-controlled quadcopter.

In the previous video, you learned how to measure the remaining flight time of your quadcopter.

In this video, I will explain a key component, the [[MPU-6050 gyroscope and accelerometer]], which is necessary to keep the quadcopter level.

This blue component is situated at the front of your quadcopter,

and is responsible for measuring the rotation rates in three directions: roll, pitch, and yaw.

A roll rotation means that you rotate clockwise around the X-axis of the gyroscope.

A pitch rotation means that you rotate clockwise around the Y-axis of the gyroscope.

Finally, in a yaw rotation, you rotate counterclockwise around the Z-axis of the MPU-6050.

In all cases, you can observe that the axis around which you rotate is the only axis that keeps pointing in the same direction during the rotation movements.

Notice that the X and Y axes and their respective rotation directions are also physically marked on the MPU-6050 sensor itself.

Once again, the sensor does not come with the headers soldered on, meaning you will have to do some soldering yourself.

Now, let's reflect on the physical value measured by the gyroscope.


Read more here
And here and here

A gyroscope measures the rotation rate, which can be expressed in degrees per second.

When you want the quadcopter to stay at its current orientation, the rotation rate will need to be equal to 0 degrees per second.

This will be very important for your flight controller.


B) How to use the MPU-6050 Gyroscope

B.1) I2C Communication Protocol

The communication protocol that your microcontroller will use to read the information from the MPU-6050 is [[I2C]].

This protocol requires two wires:

Pins 16 to 19 of your Teensy can accommodate these lines.

The schematic is shown in the figure,

Power the gyroscope by connecting the 5V DC output to the VCC pin and ground to ground. Connect the SCL and SDA pins to the Teensy pins 19 and 18, respectively.

Let’s start programming.


B.2) Arduino Code to Read Rotation Rates (roll, pitch, and yaw) with the MPU-6050 Gyroscope

The code for the [[I2C protocol]] is rather complex, so we will use a predefined library called Wire.h.
This library is normally installed automatically when you installed Teensyduino.

Define the roll, pitch, and yaw rates in degrees per second as global variables.
You will write the output of the measurements from the sensor to these variables.

Once again, use a function to get the data from the gyro.

With the I2C protocol, each device has its unique address.

For the MPU-6050, this address can be found in the register documentation and has a default value of 0x68.

Some registers can be accessed to select predefined options.

B.2.1) Using a Low-Pass Filter

One of these options is a [[low-pass filter]].

Read more here

The register has the hexadecimal address 0x1A.

Choose a [[low-pass filter]] with a [[cutoff frequency]] of 10 Hz,

which corresponds to the DLPF value of 5.

The [[low-pass filter]] is essential for the gyroscope on a quadcopter because motor vibrations can cause the rotation rates to fluctuate wildly. The figure shows the measured rotation rates with and without a low-pass filter for a stationary quadcopter at increasing motor speeds.


B.2.2) Sensor Sensitivity Scale Factor

In addition to the [[low-pass filter]], you also need to set the [[sensitivity scale factor]] of the sensor.


Read more here

The image gives an example to illustrate this:

  1. Diagram

The circle diagram represents the gyro sensor's operation, showing a 250°/s rotation, which is the maximum angular velocity in this case. For each degree of rotation, the sensor measures 131 units, as calculated above.

  1. Angular Velocity Limits and Sensitivity Table

The table shows different angular velocity limits for the sensor and the corresponding sensitivities for each:

As you increase the angular velocity limit, the sensitivity (units per degree) decreases. This is because the same 32,750 measurement units are spread over a larger range of degrees per second, resulting in fewer units for each degree.

Summary:
The image explains how the gyro sensor converts rotational movement into raw measurement units. The sensitivity (measurement units per degree) depends on the angular velocity limit of the sensor. The higher the angular velocity limit, the fewer measurement units are allocated to each degree of rotation, meaning lower sensitivity.
Read more here

The register address where you can adjust the scale factor is 0x1B.

The measurements of the MPU-6050 are recorded in [[LSB (Least Significant Bits)]].
Choose a sensitivity setting of 65.5 LSB per degree per second,

which corresponds to the FS_SEL setting of 1.

Note: About FS_SEL = 1 (± 500°/s)

  • Sensitivity is 65.5 LSB/°/s.
  • For every degree per second of rotation, the gyroscope will output 65.5 digital bits (LSB).
  • This is a wider range, so it can measure higher rotational velocities, but it is less sensitive to small changes compared to the ±250°/s setting.

The other settings in the register can be set to 0, giving a binary representation as shown,

Converting this to a hexadecimal value gives an address equal to 0x08.


B.2.3) Gyroscope Measurement Registers

Now you are ready to import the measurement values from the gyro. These are located in the registers that hold the gyroscope measurements, which have the hexadecimal addresses 0x43 to 0x48.

Start by writing to address 0x43 to indicate the first register you will use.

Now, request six bytes from the MPU-6050 so that you can pull the information from the six registers (0x43-0x48) from the sensor.

The gyro measurements are the result of an unsigned 16-bit measurement.

Declare gyroX as an unsigned 16-bit integer.

Because the measurement of the rotation rate around the X-axis is spread over two registers, each with eight bits,

you will have to merge this information by calling Wire.read() twice.

How it works?

GYRO_XOUT_H:   00010010                  (this is `0x12` in hexadecimal)
After << 8:    00010010  00000000        (this becomes `0x1200`)

GYRO_XOUT_L:   00110100                  (this is `0x34` in hexadecimal)
equivalent to: 00000000  00110100

The final result is `0x1234` (or `00010010 00110100` in binary), representing the full 16-bit value.

Repeat the same code for the other rotation rates.


B.2.4) Convert Measurements to Degrees/Second

The measurements are expressed in LSB, but you want this information in degrees per second. Since you set the LSB sensitivity scale factor of the MPU-6050 to 65.5,

simply divide the values in LSB by 65.5.


B.2.5) Finish I2C configuration (Clock Speed and Power Management)

Set the clock speed of the I2C protocol to 400 kHz

this value comes from the product specifications.

Use a delay of 250 milliseconds to give the MPU-6050 time to start.

To activate the MPU-6050, write to the power management register, which has the hexadecimal address 0x6B.

All bits in this register must be set to 0 for the device to start and continue in power mode.

Terminate the connection with the gyroscope and end the setup section.


B.2.6) Print Rotation Rates to Serial Monitor and Test

In the loop part of the code, call your function and print the roll, pitch, and yaw rates on the Serial Monitor.

Wait 50 milliseconds after each loop to read the values on the Serial Monitor, and close the loop function.

Upload the code to your microcontroller,

and open the Serial Monitor. Now, move the gyroscope in the roll, pitch, and yaw directions and watch how the rotation rates change on the screen.

Notice that not all values are equal to zero for the axes that are not moving. This issue will be the subject of the next video.


🔙 Previous Part | Next Part 🔜

↩️ Go Back


Z) 🗃️ Glossary

File Definition
Uncreated files Origin Note
cutoff frequency 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
I2C 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
I2C protocol 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
Inertial Measurement Units (IMU) 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
low-pass filter 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
low-pass filter 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
low-pass filter 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
low-pass filter 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
LSB (Least Significant Bits) 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
MPU-6050 gyroscope and accelerometer 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
Sensitivity of a Gyro Sensor 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)
sensitivity scale factor 4 - TUTORIAL - How to use the MPU6050 with Arduino and Teensy (Gyroscope Reading)