How to use the MPU6050 sensor module with Raspberry Pi board

How to use the MPU6050 sensor module with a Raspberry Pi board

Hello and welcome back. In this tutorial, we will learn how to use the MPU6050 sensor module with a Raspberry Pi board. Through this sensor, we can get 3-Axis Accelerometer and 3-Axis Gyroscope values. To get these values, this sensor includes a DMP(Digital Motion Processor) and it is called MPU6050. Also, we can get these values ​​using I2c communication. Finally, I want to say that we can use this tutorial knowledge to make self-balancing robots, aircraft, drones, and so on. Therefore, I think this tutorial is more important for electronics hobbyist creators.

  • Click this link for more information about this sensor – More info

Also, I used a servo motor to explain this tutorial. That is, I used the accelerometer Y-axis values to rotate the servo motor. The specialty of this is that the servo motor rotates in the same direction as the sensor moves. Also, please use a GPIO ribbon cable and an extension board to easily work with the Raspberry Pi board. But, I haven’t used these components in this tutorial.

OK, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect the MPU6050 sensor to the Raspberry Pi board. For that, you can use the circuit diagram below.

How to use the MPU6050 sensor module with a Raspberry Pi board

Step 3

Thirdly, connect the servo motor to the Raspberry PI board.

Step 4

Now, connect the mouse, keyboard, monitor, and SD card(OS installed) to the Raspberry Pi board. And then, provide a suitable power supply to this board.

Step 5

Then, enable I2C communication to receive sensor values. For that, use the pictures below.

  • If you want to check whether I2C communication is enabled or not, please run the following command on the terminal. Then, you can see the I2C address.
  • i2cdetect -y 1

Step 6

Now, copy and paste the following python script on the Thonny IDE and save it. And then, let’s set the servo motor center point. For that, uncomment the “setAngle” function and comment on the “angle” function. After that, run this script.

  • Full details of this project — Download
#Include the library files
import RPi.GPIO as GPIO
import smbus 
from time import sleep

# Setup GPIO pins
# Set the servo motor pin as output pin

pwm = GPIO.PWM(4,50)

#some MPU6050 Registers and their Address
PWR_MGMT_1   = 0x6B
SMPLRT_DIV   = 0x19
CONFIG       = 0x1A
INT_ENABLE   = 0x38
GYRO_XOUT  = 0x43
GYRO_YOUT  = 0x45
GYRO_ZOUT  = 0x47

bus = smbus.SMBus(1) # or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68 # MPU6050 device address

def angle(Angle):
    duty = Angle / 18 + 2
#     sleep(1)
#     pwm.ChangeDutyCycle(0)
def setAngle():

def MPU_Init():
    #write to sample rate register
    bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)

    #Write to power management register
    bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)

    #Write to Configuration register
    bus.write_byte_data(Device_Address, CONFIG, 0)

    #Write to Gyro configuration register
    bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)

    #Write to interrupt enable register
    bus.write_byte_data(Device_Address, INT_ENABLE, 1)

def read_raw_data(addr):
    #Accelero and Gyro value are 16-bit
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)
        #concatenate higher and lower value
        value = ((high << 8) | low)
        #to get signed value from mpu6050
        if(value > 32768):
                value = value - 65536
        return value


while True:
    #Read Accelerometer raw value
    acc_x = read_raw_data(ACCEL_XOUT)
    acc_y = read_raw_data(ACCEL_YOUT)
    acc_z = read_raw_data(ACCEL_ZOUT)

    #Read Gyroscope raw value
    gyro_x = read_raw_data(GYRO_XOUT)
    gyro_y = read_raw_data(GYRO_YOUT)
    gyro_z = read_raw_data(GYRO_ZOUT)

    Ax = acc_x/16384.0
    Ay = acc_y/16384.0 
    Az = acc_z/16384.0

    Gx = gyro_x/131.0
    Gy = gyro_y/131.0
    Gz = gyro_z/131.0

# Uncomment below line to see the Accelerometer and Gyroscope values   
#    print ("Gx=%.2f" %Gx, u'\u00b0'+ "/s", "\tGy=%.2f" %Gy, u'\u00b0'+ "/s", "\tGz=%.2f" %Gz, u'\u00b0'+ "/s", "\tAx=%.2f g" %Ax, "\tAy=%.2f g" %Ay, "\tAz=%.2f g" %Az) 	
    in_min = 1
    in_max = -1
    out_min = 0
    out_max = 180
    setAngle() # Use this function to set the servo motor point
    # Convert accelerometer Y axis values from 0 to 180   
    value = (Ay - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
    value = int(value)
    if value >= 0 and value <= 180:
        # Write these values on the servo motor
#         angle(value) # Rotate the servo motor using the sensor values

Step 7

Finally, comment on the “setAngle” function and uncomment the “angle” function. After that, run this script.

OK, enjoy this tutorial. The full video guide is below. So, see you in the next tutorial or project.

How to use the MPU6050 sensor module with a Raspberry Pi board

Similar Posts

Leave a Reply

Your email address will not be published.