Introduction: Measurement of Acceleration Using H3LIS331DL and Raspberry Pi

H3LIS331DL, is a low-power high performance 3-axis linear accelerometer belonging to the “nano” family, with digital I²C serial interface. H3LIS331DL has user selectable full scales of ±100g/±200g/±400g and it is capable of measuring accelerations with output data rates from 0.5 Hz to 1 kHz. The H3LIS331DL is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

In this tutorial we are going to demonstrate the interfacing of H3LIS331DL with Raspberry Pi, using python as a programming language.

Step 1: Hardware Required:

Picture of Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. H3LIS331DL

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for raspberry pi

5. Ethernet Cable

Step 2: Hardware Hookup:

Picture of Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The H3LIS331DL will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!

Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Python Code for Acceleration Measurement:

Picture of Python Code for Acceleration Measurement:

he advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the python. Python is one of the easiest programming languages with easiest syntax. The python code for H3LIS331DL can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding you need to download the SMBus library in case of python, because this library supports the functions used in the code. So, to download the library you can visit the following link:

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

You can copy the working code from here also:

import smbus

import time

# Get I2C busbus = smbus.SMBus(1)

# H3LIS331DL address, 0x18(24)

# Select control register 1, 0x20(32)

# 0x27(39) Power ON mode, Data output rate = 50 Hz# X, Y, Z-Axis enabled

bus.write_byte_data(0x18, 0x20, 0x27)

# H3LIS331DL address, 0x18(24)# Select control register 4, 0x23(35)

# 0x00(00) Continuous update, Full scale selection = +/-100g

bus.write_byte_data(0x18, 0x23, 0x00)

time.sleep(0.5)

# H3LIS331DL address, 0x18(24)

# Read data back from 0x28(40), 2 bytes

# X-Axis LSB, X-Axis MSB

data0 = bus.read_byte_data(0x18, 0x28)

data1 = bus.read_byte_data(0x18, 0x29)

# Convert the dataxAccl = data1 * 256 + data0

if xAccl > 32767 :

xAccl -= 65536

# H3LIS331DL address, 0x18(24)

# Read data back from 0x2A(42), 2 bytes

# Y-Axis LSB, Y-Axis MSB

data0 = bus.read_byte_data(0x18, 0x2A)

data1 = bus.read_byte_data(0x18, 0x2B)

# Convert the data

yAccl = data1 * 256 + data0

if yAccl > 32767 :

yAccl -= 65536

# H3LIS331DL address, 0x18(24)

# Read data back from 0x2C(44), 2 bytes

# Z-Axis LSB, Z-Axis MSB

data0 = bus.read_byte_data(0x18, 0x2C)

data1 = bus.read_byte_data(0x18, 0x2D)

# Convert the data

zAccl = data1 * 256 + data0

if zAccl > 32767 :

zAccl -= 65536

# 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

The code is executed using the following command:

$> python H3LIS331DL.py

The output of the sensor is shown in the picture above for the reference of the user.

Step 4: Applications:

Picture of Applications:

Accelerometers like H3LIS331DL mostly find its application in the games and display profile switching. This sensor module is also employed in the advanced power management system for mobile applications. H3LIS331DL is a triaxial digital acceleration sensor which is incorporated with an intelligent on-chip motion triggered interrupt controller.

Comments