 #### 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():

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):
return -(radians * (180.0 / math.pi))

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

if __name__ == "__main__":

MPU_Init()

try:
while True:

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)
```