Get MPU6050 data through websocket

MPU-6050 is an 8 pin 6 axis gyro and accelerometer in a single chip. This module works on I2C serial communication by default but it can be configured for SPI interface by configuring it register. For I2C this has SDA and SCL lines.



MPU6050 Pins

  • VCC this pin is used for powering the MPU6050 module with respect to ground
  • GND this is a ground pin
  • SDA SDA pin is used for data between controller and mpu6050 module
  • SCL SCL pin is used for clock input
  • XDA This is sensor I2C SDA Data line for configuring and reading from external sensors (not use)
  • XCL This is sensor I2C SCL clock line for configuring and reading from external sensors (not use)
  • ADO I2C Slave Address LSB (not use)
  • INT Interrupt pin for indication of data ready. (not use)

GPIO Configuration



OrangePi PC Plus / Ubuntu 16.04
Python 3.7

Download driver


I wrote a simple flask-SocketIO program to communicate with mpu6050

import logging
from flask import Flask, request, render_template
from flask_socketio import SocketIO, emit
from app.mpu6050 import mpu6050

app = Flask(__name__)
socketio = SocketIO(app)
sensor = mpu6050(address=0x68, bus=0)
log = logging.getLogger('werkzeug')

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html', interval=100)

@socketio.on('connect', namespace='/mpu6050')
def on_mpu6050_connect():
    print(f'Client {request.remote_addr} connected')

@socketio.on('get_data', namespace='/mpu6050')
def on_mpu6050_get_data():
    accel_data = sensor.get_accel_data()
    gyro_data = sensor.get_gyro_data()
    temp = sensor.get_temp()
    emit('recv_data_mpu6050', dict(

Web template

<!doctype html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0,
          maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
        * {
            margin: 0;
            padding: 0;

        table.dataset {
            background: #333;
            color: #fff;

        table.dataset td {
            width: 80px;
            padding: 5px 10px;
            border: 1px solid #fff;
    <table class="dataset">
            <td id="accel_x"></td>
            <td id="accel_y"></td>
            <td id="accel_z"></td>
            <td id="gyro_x"></td>
            <td id="gyro_y"></td>
            <td id="gyro_z"></td>

<script src="/static/js/libs/"></script>
    const socket = io.connect(`${}/mpu6050`);
    let data = {};

    socket.on("connect", function () {
        setInterval(function () {
        }, {{ interval }});

    socket.on("recv_data_mpu6050", function (msg) {
        if (msg && msg.accel_data && msg.gyro_data && msg.centidegree) {
            data = msg;
            const accel = data.accel_data;
            const gyro = data.gyro_data;
            document.getElementById("accel_x").innerHTML = accel.x.toFixed(5);
            document.getElementById("accel_y").innerHTML = accel.y.toFixed(5);
            document.getElementById("accel_z").innerHTML = accel.z.toFixed(5);
            document.getElementById("gyro_x").innerHTML = gyro.x.toFixed(5);
            document.getElementById("gyro_y").innerHTML = gyro.y.toFixed(5);
            document.getElementById("gyro_z").innerHTML = gyro.z.toFixed(5);

The full program is at You can clone my code by:

$ git clone



[1] MPU6050 Gyro Sensor Interfacing with Raspberry Pi