Measurement of Acceleration Using H3LIS331DL and Particle Photon

Introduction: Measurement of Acceleration Using H3LIS331DL and Particle Photon

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 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:

<p>#include<application.h> </p><p>#include<spark_wiringg_i2c.h> </p><p>// H3LIS331DL I2C address is 0x18(24)</p><p>#define Addr 0x18</p><p>int xAccl = 0, yAccl =  0, zAccl = 0;</p><p>void setup()</p><p>{    </p><p>// Set variable    </p><p>Particle.variable("i2cdevice", "H3LIS331DL");    </p><p>Particle.variable("xAccl", xAccl);    </p><p>Particle.variable("yAccl", yAccl);    </p><p>Particle.variable("zAccl", zAccl);</p><p>// Initialise I2C communication as MASTER    </p><p>Wire.begin();    </p><p>// Initialize serial communication, set baud rate = 9600    </p><p>Serial.begin(9600);        </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Select control register 1    </p><p>Wire.write(0x20);    </p><p>// Enable X, Y, Z axis, power on mode, data output rate 50Hz    </p><p>Wire.write(0x27);    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();        </p><p>// Start I2C Transmission    </p><p>Wire.beginTransmission(Addr);    </p><p>// Select control register 4    </p><p>Wire.write(0x23);    </p><p>// Set full scale, +/- 100g, continuous update    </p><p>Wire.write(0x00);    </p><p>// Stop I2C Transmission    </p><p>Wire.endTransmission();    </p><p>delay(300);</p><p>}
void loop()</p><p>{    </p><p>unsigned int data[6];    </p><p>for(int i = 0; i < 6; i++)    </p><p>{        </p><p>// Start I2C Transmission        </p><p>Wire.beginTransmission(Addr);        </p><p>// Select data register        </p><p>Wire.write((40 + i));        </p><p>// Stop I2C Transmission        </p><p>Wire.endTransmission();                </p><p>// Request 1 byte of data        </p><p>Wire.requestFrom(Addr, 1);                </p><p>// Read 6 bytes of data        </p><p>// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb        </p><p>if(Wire.available() == 1)        </p><p>{            </p><p>data[i] = Wire.read();        </p><p>}        </p><p>delay(300);    </p><p>}        </p><p>// Convert the data   </p><p>int xAccl = ((data[1] * 256) + data[0]);    </p><p>if(xAccl > 32767)    </p><p>{        </p><p>xAccl -= 65536;  </p><p>}    </p><p>int yAccl = ((data[3] * 256) + data[2]);    </p><p>if(yAccl > 32767)    </p><p>{        </p><p>yAccl -= 65536;    </p><p>}    </p><p>int zAccl = ((data[5] * 256) + data[4]);    </p><p>if(zAccl > 32767)    </p><p>{        </p><p>zAccl -= 65536;    </p><p>}</p><p>// Output data to dashboard   </p><p>Particle.publish("Acceleration in X-Axis is :", String(xAccl));    </p><p>Particle.publish("Acceleration in Y-Axis is :", String(yAccl));    </p><p>Particle.publish("Acceleration in Z-Axis is :", String(zAccl));    </p><p>delay(300);</p><p>}</p>

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.

Be the First to Share

    Recommendations

    • Pocket-Sized Speed Challenge

      Pocket-Sized Speed Challenge
    • Super-Size Speed Challenge

      Super-Size Speed Challenge
    • Audio Challenge 2020

      Audio Challenge 2020

    Discussions