ESP8266 NodeMCU With MPU-6050: Monitor Acceleration, Gyroscope, and Temp

by AKP in Circuits > Arduino

254 Views, 2 Favorites, 0 Comments

ESP8266 NodeMCU With MPU-6050: Monitor Acceleration, Gyroscope, and Temp

esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino.png

In this guide, you’ll learn how to use the  MPU-6050  accelerometer and  gyroscope module with the ESP8266 NodeMCU. The  MPU-6050 is an Inertial Measurement Unit (IMU) featuring a 3-axis  accelerometer and a 3-axis gyroscope. The accelerometer measures gravitational acceleration, while the gyroscope measures rotational velocity. Additionally, this module can measure temperature, making it ideal for determining the orientation of a moving object.

Supplies

  • ESP8266 NodeMCU CP2102 Amazon
  • MPU6050 3 Axis Accelerometer Gyroscope Module 6 DOF 6-axis Amazon
  • 0.96 Inch OLED I2C IIC Display Module Amazon

Introducing the MPU-6050 Gyroscope Accelerometer Sensor

MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg
roll-pitch-yaw.jpg

The  MPU-6050 module includes a 3-axis  accelerometer and a 3-axis  gyroscope.

The  gyroscope measures rotational velocity (in rad/s), which is the rate of change of angular position over time along the X, Y, and Z axes (roll, pitch, and yaw). This helps determine an object’s orientation.

The  accelerometer measures acceleration, the rate of change of an object’s velocity. It can detect static forces like gravity (9.8 m/s²) and dynamic forces such as vibrations or movement. The MPU-6050 measures acceleration along the X, Y, and Z axes. For a stationary object, the Z-axis acceleration equals the gravitational force, and the X and Y axes should read zero.

Using accelerometer values, roll and pitch angles can be calculated through trigonometry, though yaw cannot be determined this way.

Combining data from both the accelerometer and gyroscope provides more accurate information about the sensor’s orientation.

Schematic Diagram – ESP8266 NodeMCU With MPU-6050

MPU6050-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.jpg

To connect the ESP8266 to the MPU-6050 sensor, follow this schematic: connect the SCL pin to GPIO 5 and the SDA pin to GPIO 4.

Code – Obtaining MPU-6050 Sensor Readings

#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_5_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);
}


Schematic Diagram – ESP8266 NodeMCU With MPU-6050 and OLED Display

MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.jpg

Connect the components as shown in the schematic diagram below. Since the OLED display and the MPU-6050 sensor use different I2C addresses, you can connect them to the same I2C bus (same pins on the ESP8266).

Code – Display MPU-6050 Sensor Readings on OLED Display

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

Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

void setup() {
Serial.begin(115200);
// while (!Serial);
Serial.println("MPU6050 OLED demo");

if (!mpu.begin()) {
Serial.println("Sensor init failed");
while (1)
yield();
}
Serial.println("Found a MPU-6050 sensor");

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display();
delay(500); // Pause for 2 seconds
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
}

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

display.clearDisplay();
display.setCursor(0, 0);

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

display.println("Accelerometer - m/s^2");
display.print(a.acceleration.x, 1);
display.print(", ");
display.print(a.acceleration.y, 1);
display.print(", ");
display.print(a.acceleration.z, 1);
display.println("");

Serial.print("Gyroscope ");
Serial.print("X: ");
Serial.print(g.gyro.x, 1);
Serial.print(" rps, ");
Serial.print("Y: ");
Serial.print(g.gyro.y, 1);
Serial.print(" rps, ");
Serial.print("Z: ");
Serial.print(g.gyro.z, 1);
Serial.println(" rps");

display.println("Gyroscope - rps");
display.print(g.gyro.x, 1);
display.print(", ");
display.print(g.gyro.y, 1);
display.print(", ");
display.print(g.gyro.z, 1);
display.println("");

display.display();
delay(100);
}


See Full Article Here