You can easily have all of the materials we will use in our Raspberry Pi Lessons series with the sets we have prepared for you. Remote Connection Methods to Raspberry Pi (VNC, SSH, TTL) # 4 for materials up to our class Combo Kit, Alarm Circuit Making with Raspberry Pi 3 Relay Card # 11 for materials up to our class Super Starter Kitfor materials in all courses Project Set You can choose.

Hello friends. In this lesson, we will connect an accelerometer / gyroscope board (MPU6050) to our Raspberry Pi.

Necessary materials:

What is MPU6050 6-axis Gyroscope / Accelerometer Board?

The MPU6050 is an IMU (inertial measurement unit) sensor with a 3-axis gyroscope and a 3-axis accelerometer. It is used to measure the motion and acceleration of objects. The most basic sensor of unmanned aerial vehicles is this and similar IMUs. They are also used in devices such as balance robots, camera stabilization devices.

mpu6050-breakout
MPU6050 breakout card

Difference of gyroscope and accelerometer:

Difference between gyroscope and accelerometer; the movement of the gyroscope and the accelerometer measures the acceleration. As we know, the only acceleration that affects an inert object is gravity. In this way, we can obtain the orientation in three-dimensional space by measuring the gravitational acceleration acting on the object with the accelerometer. Information from the accelerometer, m / s2nd or multiples of g with a force of gravity. The gyroscope is used to measure the movement of the object on any axis. In a stationary object, the gyroscope measures zero or very close. As long as the object moves, the gyroscope sensor will give us the speed of this movement in a speed unit such as degrees / seconds.

Important Note: The sensor used in this course uses I2C connection. The I2C interface of our Raspberry Pi must be open for the circuit to work. If you did not follow the series in sequence Raspberry Pi Lessons 1: Initial Setup with raspi-config You can activate the interface by using our lesson.

The MPU6050 has an I2C communication interface. The I2C communication interface is a serial communication interface developed by the Philips semiconductor unit (now NXP). It is often referred to as abbreviations such as I2C (inter-integrated circuit), IIC and TWI (two-wire interface). The SMBus (System Management Bus) protocol, developed by Intel in 1996, is also based on I2C, which is why it is called by Linux. Since it uses 2 communication lines (SCL: serial clock and SDA: serial data), it has a very practical use.

Raspberry Pi – MPU6050 circuit diagram:

rPI-mpu6050
Raspberry Pi – MPU6050 connection diagram

After connecting our circuit, we run our Raspberry Pi. On a terminal screen when the operating system starts up

i2cdetect -y 1

command to list all I2C devices connected to the system. If our connection is correct as follows 68 Our sensor should appear at:

Note: If you are using the Model B version of Raspberry Pi with 256MB RAM, i2cdetect -y 0 We need to change to.

If our sensor was detected by the system without problems, we are ready to run our Python code:

import smbus
import math
import time

# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c

def read_byte (adr):
 return bus.read_byte_data (address, adr)

def read_word (adr):
 high = bus.read_byte_data (address, adr)
 low = bus.read_byte_data (address, adr + 1)
 val = (high << 8) + low
 return val

def read_word_2c(adr):
 val = read_word(adr)
 if (val >= 0x8000):
 return - ((65535-val) + 1)
 else:
 return val

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

def get_y_rotation (x, y, z):
 radians = math.atan2 (x, dist (y, z))
 return -math.degrees (radians)

def get_x_rotation (x, y, z):
 radians = math.atan2 (y, dist (x, z))
 return math.degrees (radians)


bus = smbus.SMBus (1)
address = 0x68 # MPU6050 I2C address

# Since MPU6050 is in sleep mode when it first starts, we run the following command to run it:
bus.write_byte_data (address, power_mgmt_1, 0)

while True:
 time.sleep (0.1)
 #Read gyroscope registers
 gyro_xout = read_word_2c (0x43)
 gyro_yout = read_word_2c (0x45)
 gyro_zout = read_word_2c (0x47)

 print "Gyroscope X:", gyro_xout, "scale:", (gyro_xout / 131)
 print "Gyroscope Y:", gyro_yout, "scale:", (gyro_yout / 131)
 print "Gyroscope Z:", gyro_zout, "scale:", (gyro_zout / 131)
 
 #Ivmeolcer read registers
 accel_xout = read_word_2c (0x3b)
 accel_yout = read_word_2c (0x3d)
 accel_zout = read_word_2c (0x3f)

 accel_xout_scaled = accel_xout of 16384.0
 accel_yout_scaled = accel_yout of 16384.0
 accel_zout_scaled = accel_zout of 16384.0

 print "Ivmeolcer X:", accel_xout, "scale:", accel_xout_scaled
 print "Ivmeolcer Y:", accel_yout, "scale:", accel_yout_scaled
 print "Ivmeolcer Z:", accel_zout, "scale:", accel_zout_scaled

 print "Freeze X:", get_x_rotation (accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
 print "Freeze Y:", get_y_rotation (accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)

 time.sleep (0.5)

While our code is running, it should give us the information as follows: