Raspberry Pi and BMG160 angular rate sensor java example
The BMG160 is an ultra-small, digital 3-axis angular rate sensor with a measurement range up to 2000°/s and a digital resolution of 16 bit for consumer electronics applications. The BMG160 allows low-noise measurement of angular rates in 3 perpendicular axes and is designed for use in cellular phones, handhelds, computer peripherals, man-machine interfaces, virtual reality features, remote and game controllers.
With its small footprint of only 3 x 3 mm² the BMG160 is unique in the class of low-noise consumer electronics gyroscopes. The zero-rate offset and offset stability over temperature of the BMG160 are outstanding.
Parameter | Technical data |
---|---|
Digital resolution | 16 bit |
Measurement ranges (programmable) |
± 125 °/s, ± 250 °/s, ± 500 °/s, ± 1000 °/s, ± 2000 °/s |
Sensitivity (calibrated) | ± 125°/s: 262.4 LSB/°/s ± 250°/s: 131.2 LSB/°/s ± 500°/s: 65.5 LSB/°/s ± 1000°/s: 32.8 LSB/°/s ± 2000°/s: 16.4 LSB/°/s |
Zero-g offset (typ., over life-time) | ± 1 °/s |
Zero-rate offset over temperature | 0.015 °/s/K |
Noise density (typ.) | 0.014 °/s/√Hz |
Low-pass filter bandwiths (progr.) | 230, 116, 64, 47, 32, 23, 12 Hz |
Date rates (programmable) | 2000, 1000, 400, 200, 100 Hz |
Digital inputs/outputs | SPI, I²C, 2x digital interrupts |
Supply voltage (VDD) | 2.4 … 3.6 V |
I/0 supply voltage (VDDIO) | 1.2 … 3.6 V |
Temperature range | -40 … +85 °C |
Current consumption – full operation – low-power mode |
5.0 mA 2.5 mA |
FIFO data buffer | 100 samples depth (each axis) |
LGA package | 3 x 3 x 0.95 mm³ |
Shock resistance | 10,000 g x 200 μs |
Connection
Raspberry PI | Module |
3.3v | Vcc |
Gnd | Gnd |
SDA | SDA |
SCL | SCL |
Code
This time we explore the world of Java on the Raspberry Pi
First you need to install PI4j – http://pi4j.com/install.html . I’ll sum it up as its easy to install from a terminal
The simplest method to install Pi4J on your RaspberryPi is to execute the following command directly on your RaspberryPi.
curl -s get.pi4j.com | sudo bash
Now for the java code – this is courtesy of a controleverything example, I had to change the I2C address for my module
[codesyntax lang=”java”]
// Distributed with a free-will license. // Use it any way you want, profit or free, provided it fits in the licenses of its associated works. // BMG160 // This code is designed to work with the BMG160_I2CS I2C Mini Module available from ControlEverything.com. // https://www.controleverything.com/content/Gyro?sku=BMG160_I2CS#tabs-0-product_tabset-2#tabs-0-product_tabset-2 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 0x69 I2CDevice device = bus.getDevice(0x69); // 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); } }
[/codesyntax]
Now you have to compile and run the program like this
$> sudo pi4j BMG160.java
Testing
You should see the following
Link
CJMCU-160 Sensortec three axis gyro attitude sensor module BMG160