I have already covered How to get acceleration values in ADXL345. MPU6050 is also an accelerometer cum Gyroscope device and today in this tutorial we are going to interface it with the STM32 microcontroller.
I will use LCD 20×4 to display the respective values. What’s interesting here is that the LCD and GY521, both will be connected to the same I2C peripheral. SO we are only using 2 wires from microcontroller to interface both the devices and that’s what the true purpose of I2C is.
MPU6050 Have a lot of functions and its not possible to cover all of them in just one tutorial. So in part 1, we will only cover the following:-
- How to Initialize the device
- How to read RAW values
- How to convert acceleration values in ‘g’
- and Gyroscope values in dps (°/s)
How To work with MPU6050
Let’s start with the initialization of the MPU6050. In order to initialize the sensor, we need to perform the following actions:-
First we need to check if the sensor is responding by reading the
"WHO_AM_I (0x75)" Register. If the sensor responds with 0x68, this means it’s available and good to go.
I am using
HAL_I2C_Mem_Read function to directly read from the given memory register
In Second step we will wake the sensor up and in order to do that we will write to the
"PWR_MGMT_1 (0x6B)" Register. See below the register content
On writing (0x00) to the
PWR_MGMT_1 Register, sensor wakes up and the Clock sets up to 8 MHz
Third, Now we have to set the Data output Rate or Sample Rate. This can be done by writing into
"SMPLRT_DIV (0x19)" Register. This register specifies the divider from the gyroscope output rate used to generate the Sample Rate for the MPU6050.
As the formula says Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV). Where Gyroscope Output Rate is 8KHz, To get the sample rate of 1KHz, we need to use the SMPLRT_DIV as ‘7’.
Fourth Step would be to configure the Accelerometer and Gyroscope registers and to do so, we need to modify
"GYRO_CONFIG (0x1B)" and "ACCEL_CONFIG (0x1C)"Registers.
Writing (0x00) to both of these registers would set the Full scale range of ± 2g in
ACCEL_CONFIG Register and a Full scale range of ± 250 °/s in
GYRO_CONFIG Register along with Self-test disabled.
This completes the initialization of the MPU6050 and Now we will see How to Read the Data from the sensor and how to convert it in the respective formats. Let us start with the Acceleration values first.
Read Value from MPU6050
We can read 1 BYTE from each Register separately or we can just read 6 BYTES all together starting from
As shown above, The
ACCEL_XOUT_H (0x3B) Register stores the higher Byte for the acceleration data along X-Axis and Lower Byte is stored in
ACCEL_XOUT_L Register. So we need to combine these 2 BYTES into a 16 bit integer value. Below is the process to do that:-
ACCEL_X = (ACCEL_XOUT_H <<8 | ACCEL_XOUT_L)
we are shifting the higher 8 bits to the left and than ‘OR’ it with the lower 8 bits.
For Example, if ACCEL_XOUT_H = 11101110 and ACCEL_XOUT_L = 10101010, we will get the resultant 16 bit value as 1110111010101010
Similarly we can do the same for the
ACCEL_ZOUT Registers. These values will still be the RAW values and we still need to convert them into proper ‘g’ format.
You can see in the picture above that for the Full-Scale range of ± 2g, the sensitivity is 16384 LSB/g. So to get the ‘g‘ value, we need to divide the RAW from 16384. Look at the code below
Reading Gyro Data is similar to the Acceleration case. We will start reading 6 BYTES of data from the
GYRO_XOUT_H Register, Combine the 2 Bytes to get 16 bit integer RAW values. As we have selected the Full-Scale range of ± 250 °/s, for which the sensitivity is 131 LSB /°/s, we have to divide the RAW values by 131.0 to get the values in dps ( °/s ). Check below
To print the values on the LCD, use the function below
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
If the LCD don’t show anything, do the following.
Goto project->setting->c/c++ build->setting->tool setting->c linker and type
-u _printf_float inside the command.
- LPC2148 UART Send and Receive April 5, 2020
- FreeRTOS Tutorial #5 ->Using Queue April 2, 2020
- Low Power Modes in STM32 March 30, 2020
- FreeRTOS TUTORIAL #4 -> Using Counting Semaphore February 18, 2020
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020