Introduction: 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.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: What You Need..!!
Step 2: 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.
Step 3: Code:
The java code for ADXL345 can be downloaded from our GitHub repository- Dcube Store
Here is the link for the same :
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.
// This code is designed to work with the ADXL345_I2CS I2C Mini Module available in Dcube Store.
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
// Select Power control register
// Auto-sleep disable
// Select Data format register
// Self test disabled, 4-wire interface, Full resolution, range = +/-2g
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
byte data = new byte;
data = (byte)device.read(0x32);
data = (byte)device.read(0x33);
data = (byte)device.read(0x34);
data = (byte)device.read(0x35);
data = (byte)device.read(0x36);
data = (byte)device.read(0x37);
// Convert the data to 10-bits
int xAccl = ((data & 0x03) * 256 + (data & 0xFF));
if(xAccl > 511)
xAccl -= 1024;
int yAccl = ((data & 0x03) * 256 + (data & 0xFF));
if(yAccl > 511)
yAccl -= 1024;
int zAccl = ((data & 0x03) * 256 + (data & 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);
Step 4: 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.