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

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.

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 python code.

Step 1: What You Need..!!

1. Raspberry Pi

2. MMA8452Q

3. I²C Cable

4. I²C Shield for Raspberry Pi

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

Here is the link.

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

https://pypi.python.org/pypi/smbus-cffi/0.5.1

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.

import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# MMA8452Q address, 0x1C(28)

# Select Control register, 0x2A(42)

# 0x00(00) StandBy mode

bus.write_byte_data(0x1C, 0x2A, 0x00)

# MMA8452Q address, 0x1C(28)

# Select Control register, 0x2A(42)

# 0x01(01) Active mode

bus.write_byte_data(0x1C, 0x2A, 0x01)

# MMA8452Q address, 0x1C(28)

# Select Configuration register, 0x0E(14)

# 0x00(00) Set range to +/- 2g

bus.write_byte_data(0x1C, 0x0E, 0x00)

time.sleep(0.5)

# MMA8452Q address, 0x1C(28)

# Read data back from 0x00(0), 7 bytes

# Status register, X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB

data = bus.read_i2c_block_data(0x1C, 0x00, 7)

# Convert the data

xAccl = (data[1] * 256 + data[2]) / 16

if xAccl > 2047 :

xAccl -= 4096

yAccl = (data[3] * 256 + data[4]) / 16

if yAccl > 2047 :

yAccl -= 4096 z

Accl = (data[5] * 256 + data[6]) / 16

if zAccl > 2047 :

zAccl -= 4096

# Output data to screen

print "Acceleration in X-Axis : %d" %xAccl

print "Acceleration in Y-Axis : %d" %yAccl

print "Acceleration in Z-Axis : %d" %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.