How to Interface MPU6050 (Accelerometer) with Raspberry pi using Python

Introduction

Hi guys , in this article we will see how to interface MPU6050 accelerator with raspberry pi using python Language. Acclerometer reads the acceleration of the sensor. This module gives the three axis values, X,Y, and Z axis. In this tutorial we are converting these values to two angles which represents the five positions, front, back, right, left and normal positions.

Components required

  • Raspberry pi – 1 (Raspbian os Installed)
  • MPU6050 – 1
  • Jumper wires

Schematic Diagram

MPU6050 module is an I2C module. SDA pin of MPU6050 is connected to physical pin 3 of raspberry pi, and SCL pin of MPU6050 is connected to physical pin 5 of raspberry pi. 3.3V is connected to VCC of MPU6050 and GND is connected to GND of MPU6050.

Code

import smbus			
from time import sleep          
import math
import RPi.GPIO as GPIO
import sys

PWR_MGMT_1   = 0x6B
SMPLRT_DIV   = 0x19
CONFIG       = 0x1A
GYRO_CONFIG  = 0x1B
INT_ENABLE   = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H  = 0x43
GYRO_YOUT_H  = 0x45
GYRO_ZOUT_H  = 0x47
bus = smbus.SMBus(1)

def MPU_Init():
	bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
	bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
	bus.write_byte_data(Device_Address, CONFIG, 0)
	bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
	bus.write_byte_data(Device_Address, INT_ENABLE, 1)

def read_raw_data(addr):
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)
        value = ((high << 8) | low)
        if(value > 32768):
                value = value - 65536
        return value


def dist(a, b):
    return math.sqrt((a*a) + (b*b))

def get_y_rotation(x, y, z):
    radians = math.atan2(y, z)
    return -(radians * (180.0 / math.pi))
 
def get_x_rotation(x, y, z):
    radians = math.atan2(x, dist(y, z))
    return -(radians * (180.0 / math.pi))

if __name__ == "__main__":
     	
    Device_Address = 0x68   
    MPU_Init()
    
    print("Reading MPU6050...")
    try:
        while True:
            acc_x = read_raw_data(ACCEL_XOUT_H)
            acc_y = read_raw_data(ACCEL_YOUT_H)
            acc_z = read_raw_data(ACCEL_ZOUT_H)
            
            acclX_scaled = acc_x * .000061 * 9.80665
            acclY_scaled = acc_y * .000061 * 9.80665
            acclZ_scaled = acc_z * .000061 * 9.80665
            
            x_angle = get_x_rotation(acclX_scaled, acclY_scaled, acclZ_scaled)
            y_angle = get_y_rotation(acclX_scaled, acclY_scaled, acclZ_scaled)
            print("X rotation: ", x_angle)
            print("Y rotation: ",y_angle)
            sleep(.50)
    except KeyboardInterrupt:
        sys.exit(0)
    except Exception as e:
        print(e)
        sys.exit(0)

You may also like...

Leave a Reply

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

%d bloggers like this: