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

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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:

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..!!

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:

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:

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 Dcube Store 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:

<p>import com.pi4j.io.i2c.I2CBus;</p><p>import com.pi4j.io.i2c.I2CDevice;</p><p>import com.pi4j.io.i2c.I2CFactory;</p><p>import java.io.IOException;</p><p>public class BMG160</p><p>{	</p><p>public static void main(String args[]) throws Exception</p><p>{		</p><p>// Create I2C bus		</p><p>I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);		</p><p>// Get I2C device, BMG160 I2C address is 0x68(104)		</p><p>I2CDevice device = bus.getDevice(0x68);</p><p>// Select range register		</p><p>// Configure full scale range, 2000 dps		</p><p>device.write(0x0F, (byte)0x80);		</p><p>// Select bandwidth register		</p><p>// Bandwidth 200 Hz		</p><p>device.write(0x10, (byte)0x04);		</p><p>Thread.sleep(500);
</p><p>// Read 6 bytes of data		</p><p>// xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb		</p><p>byte[] data = new byte[6];		</p><p>device.read(0x02, data, 0, 6);</p><p>// Convert data		</p><p>int xGyro = ((data[1] & 0xFF) * 256 + (data[0] & 0xFF));		</p><p>if(xGyro > 32767)		</p><p>{			</p><p>xGyro -= 65536;		</p><p>}</p><p>int yGyro = ((data[3] & 0xFF) * 256 + (data[2] & 0xFF));		</p><p>if(yGyro > 32767)		</p><p>{			</p><p>yGyro -= 65536;		</p><p>}</p><p>int zGyro = ((data[5] & 0xFF) * 256 + (data[4] & 0xFF));		</p><p>if(zGyro > 32767)		</p><p>{			</p><p>zGyro -= 65536;		</p><p>}
// Output data to screen		</p><p>System.out.printf("X-Axis of Rotation : %d %n", xGyro);		</p><p>System.out.printf("Y-axis of Rotation : %d %n", yGyro);		</p><p>System.out.printf("Z-axis of Rotation : %d %n", zGyro);	</p><p>}</p><p>}</p>

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.

<p>import com.pi4j.io.i2c.I2CBus;<br>import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;</p>

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.

<p>// Select range register		<br>// 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);</p><p>// 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);</p>

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

<p>int xGyro = ((data[1] & 0xFF) * 256 + (data[0] & 0xFF));		<br>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;		
}</p>

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

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

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

Step 5: 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.