Introduction: Raspberry Pi MMA8452Q 3-Axis 12-bit/8-bit Digital Accelerometer Java Tutorial

The MMA8452Q is a smart, low-power, three-axis, capacitive, micromachined accelerometer with 12 bits of resolution. Flexible user programmable options are provided with the aid of embedded functions in the accelerometer, configurable to two interrupt pins. It has user selectable full scales of ±2g/±4g/±8g with high-pass filter filtered data as well as non-filtered data available real-time. Here is its demonstration with raspberry pi using java code.

Step 1: What You Need..!!

1. Raspberry Pi

2. MMA8452Q

LINK : https://www.controleverything.com/content/Accelorometer?sku=MMA8452Q_I2CS

3. I²C Cable

LINK : https://www.controleverything.com/content/I2C-Cables?sku=I2CAFF3

4. I²C Shield for Raspberry Pi

LINK : https://www.controleverything.com/content/I2C-Master?sku=TOUTPI2

5. Ethernet Cable

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 MMA8452Q 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 MMA8452Q can be downloaded from our github repository- ControlEverythingCommunity

Here is the link for the same :

https://github.com/ControlEverythingCommunity/MMA8...

The datasheet of MMA8452Q can be found here:

https://s3.amazonaws.com/controleverything.media/c...

We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:

http://pi4j.com/install.html

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.

// MMA8452Q

// This code is designed to work with the MMA8452Q_I2CS I2C Mini Module available from ControlEverything.com.

// https://www.controleverything.com/content/Accelor...

import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

public class MMA8452Q

{

public static void main(String args[]) throws Exception

{

// Create I2C bus

I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);

// Get I2C device, MMA8452Q I2C address is 0x1C(28)

I2CDevice device = bus.getDevice(0x1C);

// Send standby command

device.write(0x2A, (byte)0x00);

// Send active command

device.write(0x2A, (byte)0x01);

// Set Range upto +/-2g

device.write(0x0E, (byte)0x00);

Thread.sleep(500);

// Read 7 bytes of data from address 0x00(0)

// Status, X msb, X lsb, Y msb, Y lsb, Z msb, Z lsb

byte[] data = new byte[7];

device.read(0x00, data, 0, 7);

// Convert the values

int xAccl = (((data[1] & 0xFF) * 256) + (data[2] & 0xFF)) / 16;

if (xAccl > 2047)

{

xAccl = xAccl - 4096;

}

int yAccl = (((data[3] & 0xFF) * 256) + (data[4] & 0xFF)) / 16;

if (yAccl > 2047)

{

yAccl = yAccl - 4096;

}

int zAccl = (((data[5] & 0xFF) * 256) + (data[6] & 0xFF)) / 16;

if (zAccl > 2047)

{

zAccl = zAccl - 4096;

}

// Output data to screen

System.out.printf("X-Axis : %d %n", xAccl);

System.out.printf("Y-Axis : %d %n", yAccl);

System.out.printf("Z-Axis : %d %n", zAccl);

}

}

Step 4: Applications:

MMA8452Q has various applications which include E-Compass applications, Static orientation detection which incorporate Portrait/Landscape, Up/Down, Left/Right, Back/ Front position identification, Notebook, e-reader, and Laptop Tumble and Freefall Detection, Real-time orientation detection including virtual reality and gaming 3D user position feedback, Real-time activity analysis such as pedometer step counting, freefall drop detection for HDD, dead-reckoning GPS backup and much more.

Comments