STM32F103: MPU6050 [I2C] (using Mbed.h)

Introduction: STM32F103: MPU6050 [I2C] (using Mbed.h)

In this tutorial, we will be interfacing MPU6050 Acclerometer/Gyrometer with our STM32F103 Nucleo Board. I will be using GY-521 Module. We will be using online mbed compiler and will create the program from scratch. Programming part is real easy because most of the work is already done in libraries.

To follow along with this tutorial you will require following:

Components needed:

  1. STM32F103 (RBT6) with a cable to connect to PC.
  2. MPU6050 ( I will be using GY-521).
  3. 4x Male-to-Female connecting wires
  4. Developer Account at mbed

This Tutorial is divided into following parts:

  1. Setting up Project.
  2. Adding code.
  3. Visualize the output

Test Run:

If you want to test this before starting ahead, just download attached binary file onto your Nucleo and open Serial Terminal to Visualize the output.

If you are NEW to mbed, please first go through this tutorial briefly. This will help you in getting started with mbed.

Step 1: Setting Up Hardware

Name of each Pin is mentioned in front of that Pin on GY-521. You can google pinout of your MPU6050 Board.

Make Connections as following:

  1. Nucleo-F103 3V3 -> GY-521 Vcc
  2. Nucleo-F103 GND -> GY-521 GND
  3. Nucleo-F103 D14/PB9/SDA -> GY-521 SDA
  4. Nucleo-F103 D15/PB8/SCL -> GY-521 SCL

Dont worry about remaning pins on your GY-521/MPU6050 Board.

Step 2: Creating Program and Adding Required Libraries

Open mbed Compiler. Sign In. Click on New in the top left corner. Make sure that NUCLEO-F103RB is selected in Platform. From template, select Empty Program. Enter a program name. and click Ok.

Now, right click on that program i.e. the program main folder in program workspace window and select new file. Enter file name main.cpp. Click OK.

Now, again Right Click on that program main folder and select New library-> From Import Wizard. The search for 'mbed'. After some time, a list will appear. drag the first library into your program (drop onto your program folder i.e. program name in Program Workspace Window)

Now, again Right Click on that program main folder and select New library-> From Import Wizard. This time search for 'MPU6050'. After some time, a list will appear. Drag the first library named MPU6050 (whose author is 'Erik Olieman') into your program (drop onto your program folder i.e. program name in Program Workspace Window)

You Program Workspace Window must be like on shown in Figure.


You may need to change adress of your MPU6050. Default Adress of Most of MPU6050's is 0x68 but this library is using 0x69. You need to Change that. In prder to change that, open MPU6050.h file in MP6050 folder.

On 19th line you will find (not writing the comment here:

#define MPU6050_ADDRESS 0x69

Change this 0x69 to 0x68 and click on Save. So Now, your header file is like shown in figure[2].

Step 3: Adding Code

Now, Open main.cpp file. Delete all existing code (if any) and paste in this code.

//include libraries in cpp file
#include "mbed.h"
#include "MPU6050.h"
//creating an object of serial class
//so that we can communicate with PC
//setting LED1 to give digital output
DigitalOut myled(LED1);
//creating onject of MPU6050 class
MPU6050 ark(PB_9,PB_8);
int main()
    while(1) {
        //reading Temprature
        float temp = ark.getTemp();
        pc.printf("temprature = %0.2f ^C\r\n",temp);
        //reading Grometer readings
        float gyro[3];
        //reading Acclerometer readings
        float acce[3];

        wait(1); //wait 1000ms

Code is explained in comments. Click on Compile and Download the binary file onto Nucleo Board. (or copy)

Step 4: Visualizing the Output

In order to visualize the output, you will need to install the a Software that can read from Serial Port. We will be using Putty, most widely used software. I have attached the .exe file. You can download it from here, or you can from elsewhere. Once downloaded, now run putty.exe. Select 'Serial'. Enter the COMport onto which your STM32 Nucleo is attached. click 'OPEN" or press Enter. Now, you should be able to see the temperature, Gyrometer and Accelerometer reading on your putty screen.

Be the First to Share


    • Audio Challenge 2020

      Audio Challenge 2020
    • Maps Challenge

      Maps Challenge
    • Backyard Contest

      Backyard Contest



    2 years ago

    Is there any means to get the distance traveled using the same method?