Real-Time Motion & Tilt Monitoring With MPU6050, ESP8266 and KiwisIoT Dashboard

by Keerthana Dass in Circuits > Electronics

25 Views, 0 Favorites, 0 Comments

Real-Time Motion & Tilt Monitoring With MPU6050, ESP8266 and KiwisIoT Dashboard

cover one.png

In this project, we will build an IoT-based real-time motion, acceleration, and tilt monitoring system using an ESP8266 NodeMCU, MPU6050 sensor, and KiwisIoT platform.

The MPU6050 is a 6-axis motion sensor that contains an accelerometer and a gyroscope. It can measure acceleration and rotational movement along the X, Y, and Z axes.

The ESP8266 reads the sensor values and calculates the tilt of the sensor. It then connects to Wi-Fi and sends the collected data to the KiwisIoT platform.

The KiwisIoT dashboard displays the sensor information in real time using separate widgets for acceleration, gyroscope, and tilt values.

In this tutorial, we will start from the basics and go step by step, including software installation, ESP8266 setup, library installation, hardware connections, KiwisIoT dashboard configuration, programming, and testing.

By the end of this project, you will have a working system that can monitor the motion and orientation of the MPU6050 through a KiwisIoT dashboard.

Supplies

Hardware

  1. ESP8266 NodeMCU × 1
  2. MPU6050 Accelerometer & Gyroscope Module × 1
  3. Female-to-female jumper wires × 4
  4. Micro-USB cable × 1
  5. Computer/Laptop × 1
  6. Wi-Fi connection × 1

Software

  1. Arduino IDE
  2. ESP8266 Board Package
  3. Adafruit MPU6050 Library
  4. Adafruit Unified Sensor Library
  5. KiwisIoT Library
  6. KiwisIoT Account

Install Arduino IDE

Arduino IDE is the software we will use to program the ESP8266 NodeMCU.

If Arduino IDE is already installed on your computer, you can skip this step and continue to the next step.

Download and install Arduino IDE on your computer.

After installation, open Arduino IDE.

Connect the ESP8266 NodeMCU to your computer using a USB data cable.

Make sure the USB cable supports data transfer. A power-only cable will not allow Arduino IDE to communicate with the ESP8266.

Once the ESP8266 is connected, we can configure Arduino IDE to recognize the board.

Add ESP8266 Board Support

Arduino IDE does not include ESP8266 boards by default, so we first need to add ESP8266 support.

Open Arduino IDE and go to:

File → Preferences

Find the field called:

Additional Boards Manager URLs

Add the following URL:

https://arduino.esp8266.com/stable/package_esp8266com_index.json

Click OK.

Next, go to:

Tools → Board → Boards Manager

Search for:

ESP8266

Install:

esp8266 by ESP8266 Community

Wait until the installation is completed.

Select the ESP8266 Board and Port

After installing the ESP8266 board package, we need to select the correct board and COM port.

Go to:

Tools → Board → ESP8266 Boards

Select:

NodeMCU 1.0 (ESP-12E Module)

Next, go to:

Tools → Port

Select the COM port connected to your ESP8266.

The COM number may be different on every computer. For example:

COM7

Your port number may be different.

Once the board and port are selected, Arduino IDE is ready to communicate with the ESP8266.

Install the Required Libraries

Now we need to install the libraries required for the MPU6050, ESP8266, and KiwisIoT.

Open:

Sketch → Include Library → Manage Libraries

Search for the following libraries and install them.


Adafruit MPU6050

Search:

Adafruit MPU6050

Install the library by Adafruit.

Adafruit Unified Sensor

Search:

Adafruit Unified Sensor

Install the library by Adafruit.

KiwisIoT

Search:

KiwisIoT

Install the library by Mugeshwaran R

After installing the libraries, restart Arduino IDE if required.

The ESP8266 Wi-Fi library and Wire library are included with the ESP8266 environment and do not need to be installed separately for this project.

Connect the MPU6050 to the ESP8266

circuit_image.png

The MPU6050 communicates with the ESP8266 using the I²C communication protocol.

Only four connections are required for this project.

MPU6050 VCC → ESP8266 3.3V

MPU6050 GND → ESP8266 GND

MPU6050 SDA → ESP8266 D2

MPU6050 SCL → ESP8266 D1

SDA is used for transferring data between the MPU6050 and ESP8266.

SCL provides the clock signal for I²C communication.

VCC supplies power to the sensor.

GND provides the common ground.

Double-check all four connections before powering the circuit.

Create the KiwisIoT Dashboard

Now that the hardware is connected, we need to prepare the KiwisIoT dashboard.

Log in to your KiwisIoT account and create a new dashboard/panel.

Give the dashboard a suitable name, such as:

Motion and Tilt Monitoring

After creating the dashboard, note down the Topic ID.

We will use this Topic ID in the Arduino program so that the ESP8266 knows where to send the sensor data.

Configure KiwisIoT Dashboard Widgets

We will use four channels to organize the sensor data.

Configure the dashboard as follows:

Channel 0 → Acceleration X, Y, Z

Channel 1 → Gyroscope X, Y, Z

Channel 2 → Tilt X

Channel 3 → Tilt Y

Add four widgets to the dashboard.

Use the following names:

ACCELERATION

GYROSCOPE

TILT_X

TILT_Y

Assign each widget to its corresponding channel.

Program the ESP8266

code-image.png

Now that the hardware is connected and the KiwisIoT dashboard is ready, we can program the ESP8266.

The program performs four main tasks:

  1. Reads acceleration data from the MPU6050.
  2. Reads gyroscope data from the MPU6050.
  3. Calculates the X and Y tilt angles.
  4. Sends the sensor data to the KiwisIoT dashboard through Wi-Fi.

Before uploading the program, replace the Wi-Fi name, Wi-Fi password, and KiwisIoT Topic ID with your own details.

Required Libraries

The program uses the following libraries:

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <math.h>
#include <ESP8266WiFi.h>
#include <KiwisIoT.h>

Enter Your Wi-Fi Details

Replace the example values with your own Wi-Fi credentials:

const char* ssid = "YOUR_WIFI_NAME";
const char* pass = "YOUR_WIFI_PASSWORD";

For example:

const char* ssid = "MyWiFi";
const char* pass = "123456789";

Enter Your KiwisIoT Topic ID

Replace the Topic ID with the Topic ID of your KiwisIoT dashboard:

const char* topic = "YOUR_TOPIC_ID";

For example:

const char* topic = "dash_1774069905560";

Complete Arduino Code

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <math.h>
#include <ESP8266WiFi.h>
#include <KiwisIoT.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* pass = "YOUR_WIFI_PASSWORD";

const char* topic = "YOUR_TOPIC_ID";

KiwisIoT kiwisiot(ssid, pass, topic);

Adafruit_MPU6050 mpu;

void setup() {

Serial.begin(115200);

if (!mpu.begin()) {
Serial.println("MPU6050 not found!");

while (1) {
delay(10);
}
}

Serial.println("MPU6050 found!");

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

kiwisiot.begin();

Serial.println("KiwisIoT started!");
Serial.println("--------------------------------");
}

void loop() {

sensors_event_t a, g, temp;

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

float ax = a.acceleration.x;
float ay = a.acceleration.y;
float az = a.acceleration.z;

float gx = g.gyro.x;
float gy = g.gyro.y;
float gz = g.gyro.z;

float tiltX =
atan2(ay, sqrt(ax * ax + az * az))
* 180.0 / PI;

float tiltY =
atan2(-ax, sqrt(ay * ay + az * az))
* 180.0 / PI;

Serial.println("========== MPU6050 ==========");

Serial.println("Acceleration:");

Serial.print("X: ");
Serial.print(ax, 2);
Serial.print(" m/s^2 | Y: ");
Serial.print(ay, 2);
Serial.print(" m/s^2 | Z: ");
Serial.print(az, 2);
Serial.println(" m/s^2");

Serial.println();

Serial.println("Gyroscope:");

Serial.print("X: ");
Serial.print(gx, 2);
Serial.print(" rad/s | Y: ");
Serial.print(gy);
Serial.print(" rad/s | Z: ");
Serial.print(gz, 2);
Serial.println(" rad/s");

Serial.println();

Serial.println("Tilt:");

Serial.print("X: ");
Serial.print(tiltX, 2);
Serial.println(" degrees");

Serial.print("Y: ");
Serial.print(tiltY, 2);
Serial.println(" degrees");

Serial.println("==============================");

String accelerationData =
String(ax, 2) + "," +
String(ay, 2) + "," +
String(az, 2);

String gyroscopeData =
String(gx, 2) + "," +
String(gy, 2) + "," +
String(gz, 2);

kiwisiot.send("0", accelerationData);

kiwisiot.send("1", gyroscopeData);

kiwisiot.send("2", String(tiltX, 2));

kiwisiot.send("3", String(tiltY, 2));

kiwisiot.run();

delay(3000);
}


Upload the Program to ESP8266

Before uploading, make sure the ESP8266 board and COM port are selected correctly.

Click the Upload button in Arduino IDE.

Wait for the compilation and upload process to finish.

After the upload is complete, the ESP8266 will restart and begin reading data from the MPU6050.

Check the Sensor Readings

serial monitor output-image.png

Open:

Tools → Serial Monitor

Set the baud rate to:

115200

The Serial Monitor should display the acceleration, gyroscope, and tilt values.

View Real-Time Data on KiwisIoT

KiwisIoT output.png

Once the ESP8266 connects to Wi-Fi and starts sending data, the KiwisIoT dashboard will begin displaying the sensor readings.

The dashboard provides four different views:

  1. Acceleration X, Y, Z
  2. Gyroscope X, Y, Z
  3. Tilt X
  4. Tilt Y

Move or tilt the MPU6050 and observe the values changing on the dashboard.

Understanding the Tilt Calculation

The MPU6050 gives acceleration values along three axes: X, Y, and Z.

We can use these acceleration values to estimate how much the sensor is tilted.

In this project, we calculate two tilt values: Tilt X and Tilt Y.

Tilt X

The Tilt X angle is calculated using the Y-axis acceleration and the combined acceleration from the X and Z axes.

float tiltX =
atan2(ay, sqrt(ax * ax + az * az))
* 180.0 / PI;

This converts the calculated angle from radians to degrees.

Tilt Y

The Tilt Y angle is calculated using the X-axis acceleration and the combined acceleration from the Y and Z axes.

float tiltY =
atan2(-ax, sqrt(ay * ay + az * az))
* 180.0 / PI;

The negative sign in the Tilt Y calculation determines the direction of the angle according to the sensor orientation used in this project.

As the MPU6050 is tilted, the acceleration values change. The ESP8266 uses these changing values to calculate the corresponding Tilt X and Tilt Y angles.

The calculated angles are then sent to the KiwisIoT dashboard, allowing us to monitor the sensor's orientation in real time.

Test the Motion and Tilt Monitoring System

Place the MPU6050 on a flat surface and observe the readings.

Then slowly tilt the sensor:

  1. Forward
  2. Backward
  3. Left
  4. Right

Observe how the Tilt X and Tilt Y values change.

You can also rotate the sensor and observe the gyroscope readings.

The acceleration and gyroscope values will change according to the movement and orientation of the sensor.

Final Result

Final one.png

The project is now complete.

The ESP8266 successfully reads data from the MPU6050 and sends it through Wi-Fi to the KiwisIoT platform.

The KiwisIoT dashboard provides real-time monitoring of:

Acceleration: X, Y, and Z

Gyroscope: X, Y, and Z

Tilt X: X-axis orientation

Tilt Y: Y-axis orientation

This provides a simple way to monitor motion and orientation using an IoT dashboard.

Applications

This type of motion and orientation monitoring can be used as a starting point for:

  1. Robotics
  2. Motion monitoring
  3. Tilt monitoring
  4. Vehicle orientation monitoring
  5. Equipment monitoring
  6. Gesture-based systems
  7. IoT sensor monitoring
  8. Educational IoT projects

Conclusion

In this project, we built an IoT-based real-time motion, acceleration, and tilt monitoring system using an ESP8266, MPU6050, and KiwisIoT.

We learned how to connect an MPU6050 to the ESP8266 using I²C communication, read accelerometer and gyroscope data, calculate tilt angles, and transmit the sensor information through Wi-Fi.

The KiwisIoT platform provides an easy way to visualize the sensor data through a real-time dashboard.

This project can also be extended by adding more sensors, charts, alerts, and automation features to create more advanced IoT applications.