ESP8266 NodeMCU and MPU6050 – How to measure Acceleration, Rotation and Temperature

In this tutorial, I am going to show you how to use MPU-6050 Gyroscope/Accelerometer.

Here you can learn how to read Acceleration, Rotation, and Temperature through just one sensor. It is named MPU-6050 or GY-521 and you can use this sensor with any board that has the I2C communication protocol (Arduino, Raspberry PI, ESP32….), in this case, I use the ESP8266 NodeMCU board. If you want to use a different board you can view the explanation to do this in the Electrical Circuit section in this tutorial.

The usability of this sensor is very dynamic because there are many libraries for its use. In this tutorial I try to abstract myself from most of the libraries that are commonly used for this sensor, using just the I2C communication library. Thus, it is more practical and easier to use and you don’t need to add specific libraries to your project. You only have to install the ‘Wire.h‘ library if you don’t have it installed, you can see this tutorial (link) to help you install the library.

Note: If you want to use any board that you cannot see in the Arduino IDE you should visit the next tutorial (link).

Parts Required

  • ESP8266 Board (or any other board with I2C);
  • USB cable;
  • Jumper wires;
  • Computer with Arduino IDE;
  • 1 Bradbord;
  • MPU-6050 sensor.

MPU-6050 Small Overview

The MPU-6050 is a device that combines a 3-axis gyroscope, a 3-axis accelerometer, and an embedded temperature sensor. The MPU6050 features six 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope and accelerometer outputs. all these quantities measured by the sensor are provided in 16-bit format

Electrical Circuit

In this section the electrical circuit connecting the MPU-6050 to the ESP-8266 is presented, as well as the options to change the circuit. As you can see in figure 1, the MPU-6050 has 4 pins connected and 4 pins that are not connected:

  • VCC -> ESP8266 VCC for energy suplly;
  • GND -> ESP8266 GND;
  • SCL -> ESP8266 SCL – D1 (I2C protocol);
  • SDA -> ESP8266 SDA – D2 (I2C protocol) ;
  • XDA -> This pin is the extra SDA pin for extra I2C;
  • XDL -> This pin is the extra SCL pin for extra I2C;
  • AD0 -> If connected to VCC, the MPU address change from 0x68 to 0x69;
  • INT -> Pin for interrupts (special use).
Figure 1

If you want to use any other board different from ESP8266 then the only changes you have to make is to change the board in the Arduino IDE and connect the SCL and SDA lines to the I2C communication pins of the board you are using, these pins may be different of D1 and D2 of this electrical circuit.
Note: Any doubts about changing the board in the Arduino IDE, you can see this link.

Code

Now connect USB to your Board, and you can copy the following code to Arduino IDE and upload it to your board (ESP8266 – in this case). This can be done by simply compiling and then uploading.


//Include Wire library to use I2C communication protocl
#include<Wire.h>
 
//I2C Address for MPU6050
const int MPU=0x68;  // Can be 0x69 if AD0 Pin is True (VCC)

//Variables for raw values from MPU ADC
int AcX,AcY,AcZ,GyX,GyY,GyZ;
int16_t Tmp;

int minVal=0;
int maxVal=65536; //2^16 levels from MPU6050 ADC 

int offsetTemp = 35; // from MPU datasheet
int offsetTempLevels = 521 // from MPU datasheet

void setup()
{
  Serial.begin(9600); // Begin Serial communication

  Wire.begin();
  Wire.beginTransmission(MPU); // begin transmission with MPU address
  Wire.write(0x6B); 
   
  //Start MPU-6050 sensor 
  Wire.write(0); 
  Wire.endTransmission(true);    
  
}
void loop()
{
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  //Get data from Sensor (14 consecutive bytes)
  Wire.requestFrom(MPU,14,true);  
  //Read data byte by byte (16 bits is 8 bits | 8 bits)
  //Each value is composed by 16 bits (2 bytes)
  GyX=Wire.read()<<8|Wire.read();  //0x3B (GYRO_XOUT_H) & 0x3C (GYRO_XOUT_L)     
  GyY=Wire.read()<<8|Wire.read();  //0x3D (GYRO_YOUT_H) & 0x3E (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  //0x3F (GYRO_ZOUT_H) & 0x40 (GYRO_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  //0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  AcX=Wire.read()<<8|Wire.read();  //0x43 (ACCEL_XOUT_H) & 0x44 (ACCEL_XOUT_L)
  AcY=Wire.read()<<8|Wire.read();  //0x45 (ACCEL_YOUT_H) & 0x46 (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  //0x47 (ACCEL_ZOUT_H) & 0x48 (ACCEL_ZOUT_L)

  //Adapts to range from 0 to 2000 m/s^2
  AcX = map(AcX,minVal,maxVal,2000,0);
  AcY = map(AcY,minVal,maxVal,2000,0);
  AcZ = map(AcZ,minVal,maxVal,2000,0);
  
  //Adapts to range from -180º to 180º
  int xAng = map(GyX,minVal,maxVal,180,-180);
  int yAng = map(GyY,minVal,maxVal,180,-180);
  int zAng = map(GyZ,minVal,maxVal,180,-180); 
   
  //Send X axis accelerometer value for serial monitor
  Serial.print("AcX = "); Serial.print(AcX);
   
  //Send Y axis accelerometer value for serial monitor
  Serial.print(" | AcY = "); Serial.print(AcY);

  //Send Z axis accelerometer value for serial monitor
  Serial.print(" | AcZ = "); Serial.print(AcZ);

   
  //Send Temperature value for serial
  //Calculates the temperature given the datasheet values (ºC)
  // 340 is the number of levels per ºC (from datasheet)
  Serial.print(" | Tmp = "); Serial.print(float(Tmp + offsetTempLevels)/340 + offsetTemp);
  
   
  //Send X axis gyroscope angle value for serial monitor
  Serial.print(" | GyX = "); Serial.print(xAng);

   
  //Send Y axis gyroscope angle value for serial monitor  
  Serial.print(" | GyY = "); Serial.print(yAng);
 
   
  //Send Z axis gyroscope angle value for serial monitor
  Serial.print(" | GyZ = "); Serial.println(zAng);
 
   
  //Wait 300 ms, after that, start new measurement and display.
  delay(300);
}

After the upload is completed don’t forget to open the serial monitor to view the results of the measurements, as you can see in the Results section (below).

Results

After performing all the tasks described in the previous sections, you will see on the serial monitor the result shown in Figure 2. To open the serial monitor, click on the Arduino IDE button on the right side of the figure.

Figure 2

When you move the sensor the displayed values will change, depending on the movement you make.
Note: If you can’t see these characters on the serial monitor then the baud rate communication speed is probably wrong, this speed is configured in the lower right corner of the serial monitor, as you can see in the figure, where the value “9600 baud” is presented.

References

https://www.alldatasheet.com/datasheet-pdf/pdf/517744/ETC1/MPU-6050.html

https://www.arduino.cc/en/reference/wire

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO