Raspberry Pi - ADXL345 3-Axis Accelerometer Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
1657 Views, 1 Favorites, 0 Comments
Raspberry Pi - ADXL345 3-Axis Accelerometer Java Tutorial
The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through I2 C digital interface. Itmeasures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (3.9 mg/LSB) enables measurement of inclination changes less than 1.0°. Here is it demonstration with raspberry pi using java code.
What You Need..!!
Connections:
Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to ADXL345 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The java code for ADXL345 can be downloaded from our GitHub repository- Dcube Store
Here is the link for the same :
https://github.com/DcubeTechVentures/ADXL345
We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:
You can also copy the code from here, it is given as follows:
// 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.
// ADXL345
// This code is designed to work with the ADXL345_I2CS I2C Mini Module available in Dcube Store.
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class ADXL345
{
public static void main(String args[]) throws Exception
{
// Create I2C bus
I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, device I2C address is 0x53(83)
I2CDevice device = Bus.getDevice(0x53);
// Select Bandwidth rate register
// Normal mode, Output data rate = 100 Hz
device.write(0x2C, (byte)0x0A);
// Select Power control register
// Auto-sleep disable
device.write(0x2D, (byte)0x08);
// Select Data format register
// Self test disabled, 4-wire interface, Full resolution, range = +/-2g
device.write(0x31, (byte)0x08);
Thread.sleep(500);
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
byte[] data = new byte[6];
data[0] = (byte)device.read(0x32);
data[1] = (byte)device.read(0x33);
data[2] = (byte)device.read(0x34);
data[3] = (byte)device.read(0x35);
data[4] = (byte)device.read(0x36);
data[5] = (byte)device.read(0x37);
// Convert the data to 10-bits
int xAccl = ((data[1] & 0x03) * 256 + (data[0] & 0xFF));
if(xAccl > 511)
{
xAccl -= 1024;
}
int yAccl = ((data[3] & 0x03) * 256 + (data[2] & 0xFF));
if(yAccl > 511)
{
yAccl -= 1024;
}
int zAccl = ((data[5] & 0x03) * 256 + (data[4] & 0xFF));
if(zAccl > 511)
{
zAccl -= 1024;
}
// Output data to screen
System.out.printf("Acceleration in X-Axis : %d %n", xAccl);
System.out.printf("Acceleration in Y-Axis : %d %n", yAccl);
System.out.printf("Acceleration in Z-Axis : %d %n", zAccl);
}
}
Applications:
ADXL345 is a small, thin, ultralow power, 3-axis accelerometer which can be employed in Handsets, Medical instrumentation etc. Its application also includes Gaming and pointing devices, Industrial instrumentation, Personal navigation devices and Hard disk drive (HDD) protection.