Introduction: Interfacing of 3-Axis Gyroscope Sensor BMG160 With Raspberry Pi

In today's world, more than half of the youth and kids are fond of gaming and all those who are fond of it, fascinated by the technical aspects of gaming knows the importance of motion sensing in this domain. We were also amazed by the same thing and just to bring it on the boards, we thought of working on a gyroscope sensor which can measure the angular rate of any object. So, the sensor we took up to deal with the task is BMG160. BMG160 is a 16-bit, digital, triaxial, gyroscope sensor which can measure the angular rate in three perpendicular room dimensions.

In this tutorial, we are going to demonstrate the working of BMG160 with Raspberry pi, using Java as the programming language.

Hardware that you are going to need for this purpose are as follows:

1. BMG160

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for Raspberry Pi

5. Ethernet Cable

Step 1: BMG160 Overview:

Picture of BMG160 Overview:

First of all we would like to familiarize you with the basic features of the sensor module that is BMG160 and the communication protocol on which it works.

BMG160 is basically a 16-bit, digital, triaxial, gyroscope sensor which can measure angular rates. It is capable of computing angular rates in three perpendicular room dimensions, the x-, y- and z-axis, and providing the corresponding output signals. It can communicate with the raspberry pi board using the I2C communication protocol. This particular module is designed to meet requirements for consumer applications as well as industrial purposes.

The communication protocol on which the sensor works is I2C. I2C stands for the inter-integrated circuit. It is a communication protocol in which the communication takes place through SDA(serial data) and SCL(serial clock) lines. It allows connecting multiple devices at the same time. It is one of the simplest and most efficient communication protocol.

Step 2: What You Need..!!

Picture of What You Need..!!

The materials that we need for accomplishing our goal includes the following hardware components:

1. BMG160

2. Raspberry Pi

3. I2C Cable

4. I2C Shield For Raspberry Pi

5. Ethernet Cable

Step 3: Hardware Hookup:

Picture of Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The BMG160 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!

Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 4: 3-Axis Gyroscope Measurement Using Java Code:

Picture of 3-Axis Gyroscope Measurement Using Java Code:

The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the Java. The Java code for BMG160 can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also: As the first step of coding you need to download the pi4j library in case of java, because this library supports the functions used in the code. So, to download the library you can visit the following link:

http://pi4j.com/install.html

You can copy the working java code for this sensor from here also:

import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

public class BMG160

{

public static void main(String args[]) throws Exception

{

// Create I2C bus

I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);

// Get I2C device, BMG160 I2C address is 0x68(104)

I2CDevice device = bus.getDevice(0x68);

// Select range register

// Configure full scale range, 2000 dps

device.write(0x0F, (byte)0x80);

// Select bandwidth register

// Bandwidth 200 Hz

device.write(0x10, (byte)0x04);

Thread.sleep(500);

// Read 6 bytes of data

// xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb

byte[] data = new byte[6];

device.read(0x02, data, 0, 6);

// Convert data

int xGyro = ((data[1] & 0xFF) * 256 + (data[0] & 0xFF));

if(xGyro > 32767)

{

xGyro -= 65536;

}

int yGyro = ((data[3] & 0xFF) * 256 + (data[2] & 0xFF));

if(yGyro > 32767)

{

yGyro -= 65536;

}

int zGyro = ((data[5] & 0xFF) * 256 + (data[4] & 0xFF));

if(zGyro > 32767)

{

zGyro -= 65536;

} // Output data to screen

System.out.printf("X-Axis of Rotation : %d %n", xGyro);

System.out.printf("Y-axis of Rotation : %d %n", yGyro);

System.out.printf("Z-axis of Rotation : %d %n", zGyro);

}

}

The library which facilitates i2c communication between the sensor and the board is pi4j, its various packages I2CBus, I2CDevice and I2CFactory help to establish the connection.

import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice; import com.pi4j.io.i2c.I2CFactory; import java.io.IOException;

This part of the code makes the sensor measure the angular rate by writing the respective commands using the write() function and then the data is read using the read() function.

// Select range register
// Configure full scale range, 2000 dps device.write(0x0F, (byte)0x80); // Select bandwidth register // Bandwidth 200 Hz device.write(0x10, (byte)0x04); Thread.sleep(500);

// Read 6 bytes of data // xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb byte[] data = new byte[6]; device.read(0x02, data, 0, 6);

The data received from the sensor is converted to the appropriate format by using the following:

int xGyro = ((data[1] & 0xFF) * 256 + (data[0] & 0xFF));
if(xGyro > 32767) { xGyro -= 65536; } int yGyro = ((data[3] & 0xFF) * 256 + (data[2] & 0xFF)); if(yGyro > 32767) { yGyro -= 65536; } int zGyro = ((data[5] & 0xFF) * 256 + (data[4] & 0xFF)); if(zGyro > 32767) { zGyro -= 65536; }

The output is printed using the System.out.println() function, in the following format.

System.out.println("X-Axis of Rotation : %d %n", xGyro);
System.out.println("Y-axis of Rotation : %d %n", yGyro); System.out.println("Z-axis of Rotation : %d %n", zGyro);

The output of the sensor is shown in the picture above.

Step 5: Applications:

Picture of Applications:

BMG160 has a varied number of applications in devices like cell phones, human machine interface devices. This sensor module has been designed to meet requirements for consumer applications such as image stabilization(DSC and camera-phone), gaming and pointing devices. It is also employed in systems which require gesture recognition and the systems used in indoor navigation.

Comments