Real-time Graphing with Arduino and PyQt (Using MPU6050 and Kalman Filter)

Posted by


In this tutorial, we will create a real-time graph using Arduino and PyQt to display data from an MPU6050 accelerometer and gyroscope sensor with a Kalman filter. This real-time graph will display the filtered sensor data in a smooth and visually appealing way.

To begin with, you will need the following components:

  1. Arduino Uno or similar board
  2. MPU6050 sensor module
  3. USB cable for connecting Arduino to your computer
  4. Breadboard and jumper wires
  5. A computer with Arduino IDE and Python installed

Step 1: Connect the MPU6050 sensor to Arduino

  1. Connect VCC and GND pins of the MPU6050 to 5V and GND pins on Arduino, respectively.
  2. Connect SDA and SCL pins of the MPU6050 to the A4 and A5 pins on Arduino, respectively.

Step 2: Calibrate the MPU6050 sensor

Before using the MPU6050 sensor, it is important to calibrate it to ensure accurate readings. You can use the MPU6050 calibration code provided in the MPU6050 library for Arduino.

Step 3: Install the necessary libraries

Download and install the MPU6050 library for Arduino and the PyQtGraph library for Python from their respective websites.

Step 4: Upload the Arduino code

Upload the following code to your Arduino board:

#include <Wire.h>
#include <MPU6050_tockn.h>

MPU6050 mpu6050(Wire);

void setup() {
  Wire.begin();
  mpu6050.setI2cClock(40000);
  mpu6050.setAccelRange(MPU6050_ACCEL_FS_16);
  mpu6050.setGyroRange(MPU6050_GYRO_FS_2000);
}

void loop() {
  mpu6050.update();
  Serial.print(mpu6050.getAccX());
  Serial.print(",");
  Serial.print(mpu6050.getAccY());
  Serial.print(",");
  Serial.print(mpu6050.getAccZ());
  Serial.print(",");
  Serial.print(mpu6050.getGyroX());
  Serial.print(",");
  Serial.print(mpu6050.getGyroY());
  Serial.print(",");
  Serial.println(mpu6050.getGyroZ());
}

Step 5: Create the PyQt application

Create a new Python script and import the necessary libraries:

import sys
import serial
from PyQt5.QtWidgets import QApplication, QMainWindow
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore

Step 6: Define the main window class

Create a MainWindow class that inherits from QMainWindow and add a few methods for initializing the serial connection and updating the graph:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.data = {'AccX': [], 'AccY': [], 'AccZ': [], 'GyroX': [], 'GyroY': [], 'GyroZ': []}
        self.ptr = 0
        self.app = QApplication([])

        self.win = pg.GraphicsLayoutWidget(show=True)
        self.win.resize(800, 600)
        self.win.setWindowTitle('Realtime Data')

        self.accel_plot = self.win.addPlot(title="Accelerometer Data")
        self.accel_plot.setYRange(-30, 30)
        self.accel_curve_x = self.accel_plot.plot(pen='r')
        self.accel_curve_y = self.accel_plot.plot(pen='g')
        self.accel_curve_z = self.accel_plot.plot(pen='b')

        self.gyro_plot = self.win.addPlot(title="Gyroscope Data")
        self.gyro_plot.setYRange(-200, 200)
        self.gyro_curve_x = self.gyro_plot.plot(pen='r')
        self.gyro_curve_y = self.gyro_plot.plot(pen='g')
        self.gyro_curve_z = self.gyro_plot.plot(pen='b')

        self.serial_data = serial.Serial('COM3', 115200)

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(0)

    def update(self):
        data = self.serial_data.readline().decode('utf-8').strip().split(',')
        if len(data) == 6:
            self.data['AccX'].append(float(data[0]))
            self.data['AccY'].append(float(data[1]))
            self.data['AccZ'].append(float(data[2]))
            self.data['GyroX'].append(float(data[3]))
            self.data['GyroY'].append(float(data[4]))
            self.data['GyroZ'].append(float(data[5]))

            self.accel_curve_x.setData(self.data['AccX'])
            self.accel_curve_y.setData(self.data['AccY'])
            self.accel_curve_z.setData(self.data['AccZ'])

            self.gyro_curve_x.setData(self.data['GyroX'])
            self.gyro_curve_y.setData(self.data['GyroY'])
            self.gyro_curve_z.setData(self.data['GyroZ'])

def main():
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Step 7: Run the application

Run the Python script and you should see a window with two plots displaying the accelerometer and gyroscope data in real-time.

That’s it! You have successfully created a real-time graph using Arduino, PyQt, and a Kalman filter for the MPU6050 sensor. You can further customize the graph appearance and add additional features as needed.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sivapraveens9643
2 months ago

Thanks

@abderrazaqhamdache1518
2 months ago

thanks

@ank7560
2 months ago

Excellent work