MPU6050 integrates a 3-axis accelerometer and a 3-axis gyroscope. It also has a built-in temperature sensor and DCM to perform complex tasks. MPU6050 is commonly used for building drones and other remote robots like self-balancing robots. In this project, we will learn how to use the MPU6050 to build an inclinometer or Spirit Leveller. As we know, an inclinometer is used to check whether a surface is perfectly flat, and they can act as spirit bubble surfaces or digital gauges. In this project, we will create a digital inclinometer that can be monitored using an Android application. The reason for using a remote display similar to a mobile phone is that we can monitor the values from the MPU6050 without having to look at the hardware, which is very convenient when the MPU6050 is placed in a drone or some other inaccessible position.
Required Materials
● Arduino Pro-mini (5V) development board
● MPU6050 gyroscope sensor
● HC-05 or HC-06 Bluetooth module
● FTDI conversion board
● Breadboard
● Connecting wires
● Mobile phone
Circuit Diagram
The complete circuit diagram for the Arduino inclinometer project is shown below. It has only three components and can be easily built on a breadboard.
MPU6050 communicates via I2C, so the SDA pin is connected to the A4 pin of the Arduino, which is the SDA pin, and the SCL pin is connected to the A5 pin of the Arduino. The HC-06 Bluetooth module operates in serial communication mode, so the Rx pin of the Bluetooth is connected to pin D11, and the Tx pin of the Bluetooth is connected to pin D10 of the Arduino. These pins D10 and D11 will be configured as serial pins through programming the Arduino. The HC-05 module and the MSP6050 module operate at +5V, so they are powered by the Vcc pin of the Arduino, as shown above.
I used some breadboard jumper wires and installed them on a small breadboard. Once the connections are complete, the circuit board looks as follows.
Powering Your Setup
You can power your circuit using an FTDI programming board like I did, or use a 9V battery or a 12V adapter and connect it to the Raw pin of the Arduino Pro mini. The Arduino Pro-mini has a built-in voltage regulator that converts the external voltage to +5V.
Programming Your Arduino
Once the hardware is ready, we can start programming our Arduino. As usual, the complete code for this project can be found at the bottom of this page. But to better understand this project, I have broken down the code into small segments and explained them below.
The first step is to connect the MPU6050 to the Arduino. For this project, we will use the library developed by Korneliusz, which can be downloaded from the link below.
MPU6050 Liberty – Korneliusz Jarzebski
Download the ZIP file and add it to your Arduino IDE. Then go to File-> Examples-> Arduino_MPU6050_Master -> MPU6050_gyro_pitch_roll_yaw. This will open the example program that uses the library we just downloaded. So click upload and wait for the program to upload to your Arduino Pro mini. Once done, open the serial monitor and set the baud rate to 115200, and check if you get the following output.
Initially, all three values will be zero, but as you move the breadboard, you will observe these values change. If they change, it means your connections are correct; otherwise, check your connections. Please take some time to notice how the three values Pitch, Roll, and Yaw change according to the way you tilt the sensor. If you get confused, press the reset button on the Arduino, and the values will initialize to zero again, then tilt the sensor in one direction and check which values are changing. The image below will help you understand better.
Among these three parameters, we are only interested in Roll and Pitch. The Roll value tells us the tilt on the X-axis, while the Pitch value tells us the tilt on the Y-axis. Now that we understand the basics, let’s start programming the Arduino to read these values and then send them to the Arduino via Bluetooth. As always, let’s start with including all the libraries required for this project.
-
#include <Wire.h> //Lib for IIC communication
-
#include <MPU6050.h> //Lib for MPU6050
-
#include <SoftwareSerial.h>// import the serial library
Copy Code
Then we initialize the software serial port for the Bluetooth module. By using the software serial library in Arduino, we can program these IO pins to be serial pins. Here, we use digital pins D10 and D11, where D10 is Rx and D11 is Tx.
-
SoftwareSerial BT(10, 11); // RX, TX
Copy Code
Next, we initialize the variables and objects needed for the program and go to the setup() function, where we specify the baud rate for the serial monitor and Bluetooth. For HC-05 and HC-06, the baud rate is 9600, so we must use the same baud rate. Then we check if the IIC bus of the Arduino is connected to the MPU6050, and if not, we print a warning message and stay there as long as the device is connected. After that, we calibrate the gyroscope and set its threshold using their respective functions as follows.
-
void setup()
-
{
-
Serial.begin(115200);
-
BT.begin(9600); //start the Bluetooth communication at 9600 baudrate
-
// Initialize MPU6050
-
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
-
{
-
Serial.println(“Could not find a valid MPU6050 sensor, check wiring!”);
-
delay(500);
-
}
-
mpu.calibrateGyro(); // Calibrate gyroscope during start
-
mpu.setThreshold(3); //Controls the sensitivity
-
}
Copy Code
The line “mpu.calibrateGyro();” calibrates the MPU6050 for its current position. Whenever the MPU6050 needs to be calibrated and all values set to zero, this line can be called multiple times in the program. The function “mpu.setThreshold(3);” controls how much the sensor’s values change when it moves; a value that is too small will increase noise, so caution should be taken when using it.
Inside void loop(), we repeatedly read the values from the gyroscope and temperature sensor, calculate the values for Pitch, Roll, and Yaw, and send them to the Bluetooth module. The following two lines will read the raw gyroscope values and the temperature value:
-
Vector norm = mpu.readNormalizeGyro();
-
temp = mpu.readTemperature();
Copy Code
Next, we calculate the Pitch, Roll, and Yaw by multiplying the time step and adding it to the previous values. The timeStep is simply the interval between consecutive readings.
-
pitch = pitch + norm.YAxis * timeStep;
-
roll = roll + norm.XAxis * timeStep;
-
yaw = yaw + norm.ZAxis * timeStep;
Copy Code
To better understand the time step, let’s look at the line below. This line is used to read the values from the MPU6050 with an interval of 10mS or 0.01 seconds. So we declare the value of timeStep as 0.01. If there’s more time, use the line below to save the program. (millis() – timer()) gives the time the program has been executed so far. We simply use 0.01 seconds to subtract from it, and the remaining time we just use the delay function to save our program.
-
delay((timeStep*1000) – (millis() – timer));
Copy Code
Once we finish reading and calculating the values, we can send them to our phone via Bluetooth. But there’s a problem. The Bluetooth module we are using can only send 1 byte (8 bits), which limits us to sending numbers from 0 to 255. Therefore, we must split our values and map them within this range. This is done by the following lines:
-
if (roll>-100 && roll<100)
-
x = map (roll, -100, 100, 0, 100);
-
if (pitch>-100 && pitch<100)
-
y = map (pitch, -100, 100, 100, 200);
-
if (temp>0 && temp<50)
-
t = 200 + int(temp);
Copy Code
As you can see, the Roll value is mapped to the variable x from 0 to 100, Pitch is mapped to the variable y from 100 to 200, and Temp is mapped to the variable t above 200. We can retrieve the data from the information we send. Finally, we use the following lines to write these values via Bluetooth:
-
BT.write(x);
-
BT.write(y);
-
BT.write(t);
Copy Code
If you have understood the complete program, scroll down to browse the program and upload it to the Arduino board.
Preparing the Android Application
The Android application for the Arduino inclinometer was developed using the Processing IDE. This is very similar to Arduino and can be used to create system applications, Android applications, web designs, etc.
However, I cannot explain the complete code for creating this application. So you have two options to solve this problem. You can download the APK file from the link below and directly install the Android application on your phone.
Note: The application by default only connects to Bluetooth devices named “HC-06”
Understanding Processing Code:
For those who choose to understand the Processing code later, I want to explain some important points. The code requires the Processing mode for Android to be running, which in turn requires the Android SDK files. The installation is a bit cumbersome, but it is worth it. Secondly, if you try to use the code provided below, you will need the following ZIP file, which contains the data files and code.
Processing ZIP file for Arduino Inclinometer
In the ZIP file, you will find a folder named data, which contains all the images and other resources to be loaded into the Android application. The following line determines which name the Bluetooth should automatically connect to:
-
bt.connectToDeviceByName(“HC-06”);
Copy Code
In the draw() function, these things will be executed repeatedly as we draw images, display text, and animate shapes based on the values from the Bluetooth module. You can check what happens inside each function by reading the program.
-
void draw() //The infinite loop
-
{
-
background(0);
-
imageMode(CENTER);
-
image(logo, width/2, height/1.04, width, height/12);
-
load_images();
-
textfun();
-
getval();
-
}
Copy Code
Finally, there is one more important thing to explain; remember that we divided the values for Pitch, Roll, and Temperature from 0 to 255. Therefore, we will revert them back to normal values.
-
if (info<100 && info>0)
-
x = map(info, 0, 100, -(width/1.5)/3, +(width/1.5)/3);//x = info;
-
else if (info<200 && info>100)
-
y = map(info, 100, 200, -(width/4.5)/0.8, +(width/4.5)/0.8);//y = info;
-
else if (info>200)
-
temp = info -200;
-
println(temp,x,y);
Copy Code
There are many better ways from the Bluetooth module to the phone, but since this is just a hobby project, we have skipped them. If you are interested, you can dig deeper.
How the Arduino Inclinometer Works:
After preparing the hardware and applications, it’s time to enjoy the fun we have built. Upload the Arduino code to the board, and you can also uncomment the lines on Serial.println and use the serial monitor to check if the hardware is working as expected. Anyway, this is completely optional.
After uploading the code, launch the Android application on your phone. The application should automatically connect to your HC-06 module, and it will display “Connect to: HC-06” at the top of the application as shown below.
Initially, all values will be zero except for the temperature value. This is because the Arduino has calibrated the MPU-6050 as a reference, and now you can tilt the hardware and check if the values in the mobile application change with the animation. The complete working of the application can be found in the video below. So now you can place the breadboard anywhere and check if the surface is perfectly flat.
I hope you understand this project and learn something useful from it. If you have any questions, please reply below this article.
For more great tutorials on Arduino development boards, click on “Read Original“.