Introduction: Measurement of Acceleration Using H3LIS331DL and Particle Photon

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 particle photon.

Step 1: Hardware Required:

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

1. H3LIS331DL

2. Particle Photon

3. I2C Cable

4. I2C shield for particle photon

Step 2: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the particle photon. 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: Code for Acceleration Measurement:

Lets start with the particle code now.

While using the sensor module with the arduino, we include application.h and spark_wiring_i2c.h library. "application.h" and spark_wiring_i2c.h library contains the functions which facilitate the i2c communication between the sensor and the particle.

The entire particle code is given below for the convenience of the user:

#include<application.h>

#include<spark_wiringg_i2c.h>

// H3LIS331DL I2C address is 0x18(24)

#define Addr 0x18

int xAccl = 0, yAccl = 0, zAccl = 0;

void setup()

{

// Set variable

Particle.variable("i2cdevice", "H3LIS331DL");

Particle.variable("xAccl", xAccl);

Particle.variable("yAccl", yAccl);

Particle.variable("zAccl", zAccl);

// Initialise I2C communication as MASTER

Wire.begin();

// Initialize serial communication, set baud rate = 9600

Serial.begin(9600);

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Select control register 1

Wire.write(0x20);

// Enable X, Y, Z axis, power on mode, data output rate 50Hz

Wire.write(0x27);

// Stop I2C Transmission

Wire.endTransmission();

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Select control register 4

Wire.write(0x23);

// Set full scale, +/- 100g, continuous update

Wire.write(0x00);

// Stop I2C Transmission

Wire.endTransmission();

delay(300);

} void loop()

{

unsigned int data[6];

for(int i = 0; i < 6; i++)

{

// Start I2C Transmission

Wire.beginTransmission(Addr);

// Select data register

Wire.write((40 + i));

// Stop I2C Transmission

Wire.endTransmission();

// Request 1 byte of data

Wire.requestFrom(Addr, 1);

// Read 6 bytes of data

// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb

if(Wire.available() == 1)

{

data[i] = Wire.read();

}

delay(300);

}

// Convert the data

int xAccl = ((data[1] * 256) + data[0]);

if(xAccl > 32767)

{

xAccl -= 65536;

}

int yAccl = ((data[3] * 256) + data[2]);

if(yAccl > 32767)

{

yAccl -= 65536;

}

int zAccl = ((data[5] * 256) + data[4]);

if(zAccl > 32767)

{

zAccl -= 65536;

}

// Output data to dashboard

Particle.publish("Acceleration in X-Axis is :", String(xAccl));

Particle.publish("Acceleration in Y-Axis is :", String(yAccl));

Particle.publish("Acceleration in Z-Axis is :", String(zAccl));

delay(300);

}

Particle.variable() function creates the variables to store the output of the sensor and Particle.publish() function displays the output on the dashboard of the site.

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

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.

Comments