H3LIS331DL accelerometer sensor strange values and not working SPI.

Hi everyone.

I have a H3LIS331DL triple axis accelerometer from SparkFun. I hooked it up with a WeMos D1 Mini board using the hookup guide from SparkFun on the H3LIS331DL accelerometer. I have managed to read values from L2C but they are just random x, y, z values I guess, when the accelerometer is not in motion I still seeing data that is out of bound.. even through I know there is noise.

I have tried to use the SPI mode but I keep getting "-1" values for all of my axes. I have used the SPI example code that accompanies the H3LIS331DL accelerometer as the base of my code. I have removed the soldering from the CS jumper so I do not see where these values coming from..
SPI Code with readings:

#include "SparkFun_LIS331.h"
#include <SPI.h>

LIS331 xl;

void setup() 
{
  
  pinMode(16,INPUT);       // Interrupt pin input
  pinMode(15, OUTPUT);    // CS for SPI
  digitalWrite(15, HIGH); // Make CS high
  pinMode(13, OUTPUT);    // MOSI for SPI
  pinMode(12, INPUT);     // MISO for SPI
  pinMode(14, OUTPUT);    // SCK for SPI
  SPI.begin();
  
  xl.setSPICSPin(15);
  xl.begin(LIS331::USE_SPI);
  
  xl.intSrcConfig(LIS331::INT_SRC, 1);
  xl.setIntDuration(50, 1);
  xl.setIntThreshold(2, 1);
  xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, true);
  xl.setPowerMode(LIS331::NORMAL);
  xl.setODR(LIS331::DR_400HZ);
  xl.axesEnable(true);
  xl.setFullScale(LIS331::LOW_RANGE);
  
  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.print("Raw data:----------- \n");
    Serial.print("X:\t"); Serial.print(x); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(y); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(z); Serial.print("\n");

    Serial.print("G data:----------- \n");
    Serial.print("X:\t"); Serial.print(xl.convertToG(100,x)); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(xl.convertToG(100,y)); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(xl.convertToG(100 ,z)); Serial.print("\n");
    Serial.print("Total:\t"); Serial.print(sqrt(pow(xl.convertToG(100 ,x),2) + 
                                                pow(xl.convertToG(100 ,y),2) +
                                                pow(xl.convertToG(100 ,z),2)));
    Serial.print("\n\n");                
  }
  if (digitalRead(16) == HIGH)
  {
    Serial.println("Interrupt");
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
    Serial.print("\n");
  }
}

L2C Code with readings:

#include "SparkFun_LIS331.h"
#include <Wire.h>

LIS331 xl;

void setup() 
{
  pinMode(16,INPUT);       // Interrupt pin input
  Wire.begin();
  xl.setI2CAddr(0x19);
  xl.begin(LIS331::USE_I2C);

  xl.intSrcConfig(LIS331::INT_SRC, 1);
  xl.setIntDuration(50, 1);
  xl.setIntThreshold(2, 1);
  xl.enableInterrupt(LIS331::Z_AXIS, LIS331::TRIG_ON_HIGH, 1, false);
  xl.setPowerMode(LIS331::NORMAL);
  xl.setODR(LIS331::DR_400HZ);
  xl.axesEnable(true);
  xl.setFullScale(LIS331::HIGH_RANGE);
  Serial.begin(115200);
}

void loop() 
{
  static long loopTimer = 0;
  int16_t x, y, z;
  if (millis() - loopTimer > 500)
  {
    loopTimer = millis();
    xl.readAxes(x, y, z);
    Serial.print("Raw data:----------- \n");
    Serial.print("X:\t"); Serial.print(x); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(y); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(z); Serial.print("\n");

    Serial.print("G data:----------- \n");
    Serial.print("X:\t"); Serial.print(xl.convertToG(400,x)); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(xl.convertToG(400,y)); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(xl.convertToG(400 ,z)); Serial.print("\n");
    Serial.print("Total:\t"); Serial.print(sqrt(pow(xl.convertToG(400 ,x),2) + 
                                                pow(xl.convertToG(400 ,y),2) +
                                                pow(xl.convertToG(400 ,z),2)));
    Serial.print("\n\n");               
  }
  if (digitalRead(16) == HIGH)
  {
    Serial.println("Interrupt");
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
  }
}

L2C values are off the scale they are showing strange values even on stationary position. Could it be a faulty sensor ?

I hooked it up with a WeMos D1 Mini board using the hookup guide from SparkFun on the H3LIS331DL accelerometer.

I cannot find a hookup guide from Sparkfun about how to connect that board to a D1 Mini. Please post a link to that. If you meant the Arduino hookup guide post the wiring you actually used (a D1 Mini is different from a Arduino Pro 3.3V!).

I have managed to read values from L2C but they are just random x, y, z values

I guess you meant I2C instead of L2C.

The posted code is for an AVR Arduino. If you used the same wiring you must fail. Post a complete wiring diagram for both variants!

Sorry, it was a typo I meant I2C. As you said Arduino pro is different, but Wemos d1 supports both SPI and I2C. The following images demonstrate the wiring for each Interface(I made these up). To enable SPI mode I have removed the CS jumper from the sensor. There is no hook up guide for this specific board (Wemos D1 mini). The wiring that I have provided should be good. Correct me if not.

To enable SPI mode I have removed the CS jumper from the sensor.

That's not enough. You have to remove the I2C pull-up jumper too (JP1).

L2C values are off the scale they are showing strange values even on stationary position. Could it be a faulty sensor ?

Post example output.

I didn't find any obvious error in your code or your wiring. Focus on the I2C connection for the moment. Ensure the pull-up jumper is connected to both sides and CS is pulled high.
Then run an I2C scanner on the Wemos, it should show up 0x19. If that doesn't happen your sensor may be fried already.

That's not enough. You have to remove the I2C pull-up jumper too (JP1).

I did not know that I have to remove the I2C jumber too.. Do you think it would make a difference in SPI output?

Post example output.

These are the values I'm getting on I2C.

After removing the JP1 Jumper I'm still getting -1 on x, y and z on SPI.

Are you absolutely sure that your sensor board is made by Sparkfun?

Have you run the I2C scanner?

I'm sure the posted output is not generated by the posted code. You replaced the maxScale value in the convertToG() calls to 400. What else did you change without telling us?

These values aren't that strange. Keep in mind this is a sensor that measures up to 400g, so the measured values are more or less to be expected. Do you get (slightly) better values if you set fullscale to 100g?

The code differs in the matter of formatting the output, otherwise exactly the same. It is made by Sparkfun. I2C scanner seen the device. On 100g the x, y, z axes raw data output is bigger, but on conversion it is the same. However the sensor facing up on stationary position, it should definitely measure +1g on the z axes. Datasheet said no calibration needed.

On my last soldering I think I’ve fried the sensor as it doesn’t communicate with the computer at all. Ordered new ones and hopefully we will see some improvements on the output.

#include "SparkFun_LIS331.h"
#include <Wire.h>

LIS331 xl;

void setup() 
{
  // put your setup code here, to run once:
  //pinMode(0,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.
  xl.axesEnable(true);
  // 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.
                          
  xl.setFullScale(LIS331::LOW_RANGE);
  xl.setPowerMode(LIS331::NORMAL);
  xl.setODR(LIS331::DR_1000HZ);//DR_50HZ
  
  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.print("Raw data:----------- \n");
    Serial.print("X:\t"); Serial.print(x); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(y); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(z); Serial.print("\n");

    Serial.print("G data:----------- \n");
    Serial.print("X:\t"); Serial.print(xl.convertToG(100,x)); Serial.print("\n");
    Serial.print("Y:\t"); Serial.print(xl.convertToG(100,y)); Serial.print("\n");
    Serial.print("Z:\t"); Serial.print(xl.convertToG(100 ,z)); Serial.print("\n");
    Serial.print("Total:\t"); Serial.print(sqrt(pow(xl.convertToG(100 ,x),2) + 
                                                pow(xl.convertToG(100 ,y),2) +
                                                pow(xl.convertToG(100 ,z),2)));
    Serial.print("\n\n");                
  }
//  if (digitalRead(0) == HIGH)
//  {
//    Serial.println("Interrupt");
//    
//  }
}

The code differs in the matter of formatting the output, otherwise exactly the same.

That's wrong as I wrote in my last answer. You at least changed the calculation.

However the sensor facing up on stationary position, it should definitely measure +1g on the z axes.

I would expect it to be in the range 0.5 to 3g. If you get these results you might have to change your expectations or use a sensor that measures more exactly but doesn't allow to measure up to 400g.

I have received the sensors today, I believe the accelerometer was defective. I'm reading 1g at most on the z axis when the sensor facing upwards and the scale is set to 100g.

I would expect it to be in the range 0.5 to 3g. If you get these results you might have to change your expectations or use a sensor that measures more exactly but doesn't allow to measure up to 400g.

On 200g scale, the readings was around 3g and 5g on 400g scale. Hopefully I can get along with it and continue with my project.

Thank you for your time, and help. Much appreciated. :sunglasses: