Introduction: Interfacing of 3-Axis Gyroscope Sensor BMG160 With Particle

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 Particle Photon.

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

1. BMG160

2. Particle photon

3. I2C Cable

4. I2C Shield for Particle Photon

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. Particle Photon

3. I2C Cable

4. I2C Shield For Particle Photon

Step 3: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the Particle. 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 Particle Code:

Lets start with the particle code now.

While using the sensor module with the arduino, we include application.h and spark_wiring_i2c.h library. "application.h" and spark_wiring_i2c.h library contains the functions which facilitate the i2c communication between the sensor and the particle.

The entire particle code is given below for the convenience of the user:

<p>#include<application.h></p><p>#include<spark_wiring_i2c.h> </p><p>// BMG160 I2C address is 0x68(104)</p><p>#define Addr 0x68
int xGyro = 0, yGyro = 0, zGyro = 0;</p><p>void setup()</p><p>{    </p><p>// Set variable    </p><p>Particle.variable("i2cdevice","BMG160");    </p><p>Particle.variable("xGyro",xGyro);    </p><p>Particle.variable("yGyro",yGyro);    </p><p>Particle.variable("zGyro",zGyro);</p><p>// Initialise I2C communication as MASTER     </p><p>Wire.begin();    </p><p>// Initialise Serial Communication    </p><p>Serial.begin(9600);        </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Select Range register    </p><p>Wire.write(0x0F);    </p><p>// Configure full scale 2000 dps    </p><p>Wire.write(0x80);    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Select Bandwidth register    </p><p>Wire.write(0x10);    </p><p>// Set Bandwidth = 200 Hz    </p><p>Wire.write(0x04);    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();    </p><p>delay(300);</p><p>}</p><p>void loop()</p><p>{    </p><p>unsigned int data[6];    </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Select data register   </p><p>Wire.write(0x02);    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();    </p><p>// Request 6 bytes of data    </p><p>Wire.requestFrom(Addr, 6);    </p><p>// Read 6 bytes of data    </p><p>// xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb    </p><p>if(Wire.available() == 6)    </p><p>{        </p><p>data[0] = Wire.read();        </p><p>data[1] = Wire.read();        </p><p>data[2] = Wire.read();        </p><p>data[3] = Wire.read();        </p><p>data[4] = Wire.read();        </p><p>data[5] = Wire.read();   </p><p> }    </p><p>delay(300);</p><p>// Convert the data    </p><p>xGyro = ((data[1] * 256) + data[0]);   </p><p>if (xGyro > 32767)    </p><p>{       </p><p> xGyro -= 65536;    </p><p>}    </p><p>yGyro = ((data[3] * 256) + data[2]);    </p><p>if (yGyro > 32767)    </p><p>{</p><p>yGyro -= 65536;    </p><p>}    </p><p>zGyro = ((data[5] * 256) + data[4]);    </p><p>if (zGyro > 32767)   </p><p>{</p><p>zGyro -= 65536;    </p><p>}        </p><p>// Output data to dashboard    </p><p>Particle.publish("X-Axis of Rotation :",  String(xGyro));    </p><p>Particle.publish("Y-Axis of Rotation :",  String(yGyro));    </p><p>Particle.publish("Z-Axis of Rotation :",  String(zGyro));    </p><p>delay(1000);</p><p>}</p>

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.