Introduction: Measurement of Acceleration Using H3LIS331DL and Raspberry Pi

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.

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:

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:

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:

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 DCUBE Store.

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:

<p>import smbus</p><p>import time</p><p># Get I2C busbus = smbus.SMBus(1)</p><p># H3LIS331DL address, 0x18(24)</p><p># Select control register 1, 0x20(32)</p><p>#		0x27(39)	Power ON mode, Data output rate = 50 Hz#					X, Y, Z-Axis enabled</p><p>bus.write_byte_data(0x18, 0x20, 0x27)</p><p># H3LIS331DL address, 0x18(24)# Select control register 4, 0x23(35)</p><p>#		0x00(00)	Continuous update, Full scale selection = +/-100g</p><p>bus.write_byte_data(0x18, 0x23, 0x00)</p><p>time.sleep(0.5)
</p><p># H3LIS331DL address, 0x18(24)</p><p># Read data back from 0x28(40), 2 bytes</p><p># X-Axis LSB, X-Axis MSB</p><p>data0 = bus.read_byte_data(0x18, 0x28)</p><p>data1 = bus.read_byte_data(0x18, 0x29)</p><p># Convert the dataxAccl = data1 * 256 + data0</p><p>if xAccl > 32767 :	</p><p>xAccl -= 65536
</p><p># H3LIS331DL address, 0x18(24)</p><p># Read data back from 0x2A(42), 2 bytes</p><p># Y-Axis LSB, Y-Axis MSB</p><p>data0 = bus.read_byte_data(0x18, 0x2A)</p><p>data1 = bus.read_byte_data(0x18, 0x2B)</p><p># Convert the data</p><p>yAccl = data1 * 256 + data0</p><p>if yAccl > 32767 :	</p><p>yAccl -= 65536
</p><p># H3LIS331DL address, 0x18(24)</p><p># Read data back from 0x2C(44), 2 bytes</p><p># Z-Axis LSB, Z-Axis MSB</p><p>data0 = bus.read_byte_data(0x18, 0x2C)</p><p>data1 = bus.read_byte_data(0x18, 0x2D)</p><p># Convert the data</p><p>zAccl = data1 * 256 + data0</p><p>if zAccl > 32767 :	</p><p>zAccl -= 65536
</p><p># Output data to screen</p><p>print "Acceleration in X-Axis : %d" %xAccl</p><p>print "Acceleration in Y-Axis : %d" %yAccl</p><p>print "Acceleration in Z-Axis : %d" %zAccl</p>

The code is executed using the following command:

$> python H3LIS331DL.py
gt; python H3LIS331DL.py

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

Step 4: 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.