MPU6050 Arduino project

In one of our previous Arduino project, we used a PIR motion sensor to detect movement. PIR sensors use infrared that is emitted by objects to detect movement. In this tutorial, we are going to use a MPU6050 sensor that has build-in accelerometer and gyroscope to detect movement.

The latter is often used in drones, balancing robots and other devices that need to measure position and angles.

We will have to use a few advanced techniques in order to increase the accuracy of the data extracted from the sensor. For the sake of simplicity, we will solely use the sensor here as a motion detector.

Let’s dive into the projet.

Parts needed

Making

Make sure you do not make any confusion while purchasing the MPU6050, it is similar to the MPU9250. If you use the MPU9250, there is just no guarantee all will work fine. The color of the LED is not important, feel free to use any color.

Here is the schematic

The MPU6050 uses an I2C for communication, the SCL and SDA pins are connected on the analog pin 4 and 5.

I will suggest you use a breadboard to connect the MPU, it makes it easier.

 

When the connections are ready, we can upload the program to the Arduino. It reads data from the MPU6050 and displays them on the serial monitor. At the same time, it checks if the sum of the absolute value of the gyroscope reads is bigger than 0.2. If it does, it will power the LED.

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup()
{
Serial.begin(9600);

Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Error");
delay(1000);
}
pinMode(13, OUTPUT);

mpu.setThreshold(2);
}

void loop()
{
Vector rawGyro = mpu.readRawGyro();
Vector normGyro = mpu.readNormalizeGyro();
float normal_x = normGyro.XAxis;
float normal_y = normGyro.YAxis;
float normal_z = normGyro.ZAxis;
Serial.print(" Xnorm = ");
Serial.print(normal_x);
Serial.print(" Ynorm = ");
Serial.print(normal_y);
Serial.print(" Znorm = ");
Serial.println(normal_z);

if(abs(normal_x) + abs(normal_y) + abs(normal_z) > 0.2){
digitalWrite(13, HIGH);
}else{
digitalWrite(13, LOW);
}

delay(10);
}

Here is the screenshot of serial monitor.

And there we go! The sensor is very sensitive. It can even detect a slight shift in position as shown in the video below when I know at the table.

This can be the beginning of a very exciting project. Over to you and your imagination.

tchouken2

Related Posts