How to Use Esp 32 With MPU6050

by Buwaneka in Circuits > Arduino

1519 Views, 0 Favorites, 0 Comments

How to Use Esp 32 With MPU6050

IMG_20220706_084333.jpg

let's look at how MPU 6050 works and use it with esp 32

Supplies

download.jpg
mpu-6050-gyro-sensor-module-500x500.jpg

esp 32 board , MPU 6050 with some jumper wires

Theory of MPU 6050

mpu-6050-gyro-sensor-module-500x500.jpg

MPU 6050 can be used to measure temperature, acceleration, angular rotation 

first let's see how the accelerometer works 

Imagine that if a ball is inside a box and that the box is in outer space so the ball is weightless. If we move the box to the left with an acceleration of 1g, the ball will hit the wall of the box.

1G is equivalent to the gravitational acceleration of 9.8 m/s^2. This only happens x-axis of the box, this process is the same as in the y and z-axis according to above method.

when we consider the MPU 6050 MEMS,(MEMS means micro electronic Mechanical system), there is micro machined structure built on top of a silicon wafer, this structure is surrounded by polysilicon springs, and when there is a movement these plates move,which leads to generating deflection. due to deflection, the capacitance between fixed plates and attached to the suspended structure is changed .This change in capacitance is proportional to the acceleration on that axis.


Now let's see how the gyroscope work,

An accelerometer measures the change of linear velocity, but in a gyroscope, it measures the change of angular velocity. The main theory that is used to detect angular rotation from the gyroscope is known as the corolis effect.

When a mass moves in a particular direction with a velocity and external angular velocity is applied, the Coriolis effect generates a force that causes a perpendicular displacement of the mass. the value of this displacement is directly related to the angular rate applied. it means that when an object moves straight, it will appear to curve when you are rotating. hurricanes are a result of this effect. 

The MEMS sensor is composed of a proof mass (containing 4 parts M1, M2, M3, and M4) which is kept in a continuously oscillating movement so that it reacts to the Coriolis effect. They move inward and outward simultaneously in the Horizontal plane.

Wiring Esp 32 With MPU 6050

Untitled Sketch 2_bb.png
PINCONFIG.JPG

As above shown you can connect all components together

Installing Libraries

To work with the sensor we want several libraries, they are Adafruit_MPU6050.h, Adafruit_Sensor.h ,Wire.h

first you have to download above libraries,as shown in the video, type adafruit MPU6050, bus io and adafruit unified sensor in the search bar to download the above libraries

Code Explain

we are using esp 32 , but we can use the same Arduino IDE to program the esp 32 ,, you can easily find a youtube tutorial , if don't know how to configure arduino IDE to program esp 32.


// Basic demo for accelerometer readings from Adafruit MPU6050


#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>


Adafruit_MPU6050 mpu;


void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens


  Serial.println("Adafruit MPU6050 test!");


  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");


  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }


  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }


  Serial.println("");
  delay(100);
}


void loop() {


  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);


  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");


  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");


  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");


  Serial.println("");
  delay(500);
}


okay now let's see the code explanation line by line

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

first, we should include those installed libraries to Arduino IDE for that #include keyword is used, then include those libraries

adafruit_mpu6050 library includes hardware functions of mpu6050, therefore we do not have to worry about deep Hardwear components.

adafruit_sensor library is the unified sensor abstraction layer, and the wire library is important to include which is pre-installed in arduino ide because this allows us to communicate with i2c devices

I2c devices mean inter-integrated circuits, i2c protocol is used to create communication between two or more ICs.


Adafruit_MPU6050 mpu;

In this line we create new class using Adafruit_MPU6050 , so that we can access it's related functions



 if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

in setup part you can see this code snippet, mpu. begin() function initialize i2c interface and check the chip id, then it will soft-reset and configure other settings .


  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }


Before using the sensor, we have to initialize the sensor in a sensitive range ,

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

Therefore we use the above line to initialize it. MPU6050_RANGE_8_G means that the 8G of force is equivalent to 8*gravitational acceleration(9.8 m/s^2)

and in the switch case, the movement according to range is displayed in the serial monitor.

 mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }


  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;

this is as same as the above explanation, we have to initialize in a sensitive range both gyroscope and temperature

sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

sensor_event_t is creating an object in memory to hold our results. that holds many types of sensor data such as acceleration, gyro, temperature, light, pressure, and many more.from the getevent() function we can convert them into appropriate scale and si unit.


Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");


  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");


  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

Finally, we can print our output in the serial monitor, from the object created above, we can access desired value from a.acceleration.x,g.gyro.x , temp.temperature..etc. then the values can be easily printed on the serial monitor

The important thing to remember, when using the serial monitor is that , we should change the baud rate to 115200 because the data receiving speed is a bit high , so it is good to use a high baud rate.