H3LIS331DL Accelerometer Breakout Hookup Guide

Pages
Contributors: SFUptownMaker
Favorited Favorite 4

Examples

Hardware Hookup

The H3LIS331DL supports I2C, SPI, and three-wire SPI data transfer. The library supports I2C and SPI mode. Obviously, since SPI requires four wires and I2C only requires two, there are different circuit configurations for each mode. Now would be a good time to solder the female headers to the Arduino Pro 3.3V/8MHz and breakaway headers to the H3LIS331DL sensor before connecting the boards together.

I2C Mode

The board is labeled for I2C mode. Here you can see it connected to a 3.3V Arduino Pro. Note that connecting the board to a 5V Arduino can damage it.

I2C Mode circuit diagram

SPI Mode

In SPI mode, the SDA pin becomes MOSI, the SCL pin becomes clock, the address select pin SA0 become MISO, and the CS pin is used for chip select.

SPI Mode circuit diagram

Example Code

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

You will also need FTDI drivers installed in order to upload code to the Arduino Pro. If this is your first time using an FTDI, make sure to follow our tutorial: USB Serial Driver Quick Install.

To follow along with the examples, the code requires the LIS331 Arduino library. Make sure that the library has been installed.

For the most part, the example code for SPI mode and I2C mode is identical. The only part that differs is the intial setup where you configure the pins to be used and the library's settings.

I2C Mode Setup

Here's an example of the same section of code from an I2C configured system. It's important to note that order matters here: Wire.begin() and xl.setI2CAddr() must be called before xl.begin().

language:c
#include "SparkFun_LIS331.h"
#include <Wire.h>

LIS331 xl;

void setup() 
{
  // put your setup code here, to run once:
  pinMode(9,INPUT);       // Interrupt pin input
  Wire.begin();
  xl.setI2CAddr(0x19);    // This MUST be called BEFORE .begin() so 
                          //  .begin() can communicate with the chip
  xl.begin(LIS331::USE_I2C); // Selects the bus to be used and sets
                          //  the power up bit on the accelerometer.
                          //  Also zeroes out all accelerometer
                          //  registers that are user writable.

SPI Mode Setup

Here we have the first few lines of an SPI mode sketch. Again, order is important: pinMode(), SPI.begin(), and xl.setSPICSPin() functions must all be called before the xl.begin() function is called.

language:c
#include "SparkFun_LIS331.h"
#include <SPI.h>

LIS331 xl;

void setup() 
{
  // put your setup code here, to run once:
  pinMode(9,INPUT);       // Interrupt pin input
  pinMode(10, OUTPUT);    // CS for SPI
  digitalWrite(10, HIGH); // Make CS high
  pinMode(11, OUTPUT);    // MOSI for SPI
  pinMode(12, INPUT);     // MISO for SPI
  pinMode(13, OUTPUT);    // SCK for SPI
  SPI.begin();
  xl.setSPICSPin(10);     // This MUST be called BEFORE .begin() so 
                          //  .begin() can communicate with the chip
  xl.begin(LIS331::USE_SPI); // Selects the bus to be used and sets
                          //  the power up bit on the accelerometer.
                          //  Also zeroes out all accelerometer
                          //  registers that are user writable.

After this point, the code for either mode of operation is the same. Note that this example code includes only the second half of the setup function, and if you're copy/pasting from this example, you must copy the other half of the setup function from one of the above code chunks.

language:c
  // This next section configures an interrupt. It will cause pin
  //  INT1 on the accelerometer to go high when the absolute value
  //  of the reading on the Z-axis exceeds a certain level for a
  //  certain number of samples.
  xl.intSrcConfig(LIS331::INT_SRC, 1); // Select the source of the
                          //  signal which appears on pin INT1. In
                          //  this case, we want the corresponding
                          //  interrupt's status to appear. 
  xl.setIntDuration(50, 1); // Number of samples a value must meet
                          //  the interrupt condition before an
                          //  interrupt signal is issued. At the
                          //  default rate of 50Hz, this is one sec.
  xl.setIntThreshold(2, 1); // Threshold for an interrupt. This is
                          //  not actual counts, but rather, actual
                          //  counts divided by 16.
  xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, true);
                          // Enable the interrupt. Parameters indicate
                          //  which axis to sample, when to trigger
                          //  (in this case, when the absolute mag
                          //  of the signal exceeds the threshold),
                          //  which interrupt source we're configuring,
                          //  and whether to enable (true) or disable
                          //  (false) the interrupt.
  Serial.begin(115200);
}

void loop() 
{
  static long loopTimer = 0;
  int16_t x, y, z;
  if (millis() - loopTimer > 1000)
  {
    loopTimer = millis();
    xl.readAxes(x, y, z);  // The readAxes() function transfers the
                           //  current axis readings into the three
                           //  parameter variables passed to it.
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
    Serial.println(xl.convertToG(100,x)); // The convertToG() function
    Serial.println(xl.convertToG(100,y)); // accepts as parameters the
    Serial.println(xl.convertToG(100,z)); // raw value and the current
    Serial.println(" ");                // maximum g-rating.
  }
  if (digitalRead(9) == HIGH)
  {
    Serial.println("Interrupt");
  }
}

After placing the code into the Arduino IDE, select the board definition and COM port to upload. Once compiled, check out the sensor readings by opening up a serial monitor set to 115200 baud.