H3LIS331DL accelerometer sensor not working in SPI Mode

Hi everybody

I have a H3LIS331DL triple axis accelerometer from SparkFun. I hooked it up with a WeMos Lolin32 board using the hookup guide from SparkFun on the H3LIS331DL accelerometer. The problem I am having is that I can’t get it working in SPI mode. I keep getting “-1” values for all my axes. In I2C mode the accelerometer works fine. I have used the SPI example code that accompanies the H3LIS331DL accelerometer as the basis of my own code except that I do not need and use interrupt pins as in the example code. Below is the SPI example code that accompanies the H3LIS331DL accelerometer and below that is my own adaption of the code. The SPI pins for the WeMos Lolin32 board is:

CS: pin 5
MOSI: pin 23
MISO: pin 19
SCK or SCL: pin 18

I know that I have hooked it up correctly to my board. Can anyone please help me or suggest a solution. I need it to work in SPI mode since I will be sampling quite quick during my tests, at about 1000 Hz. I have tested the I2C mode at this sampling rate and I know that I2C is to slow. Please help me with my problem.

SPI example code that accompanies the H3LIS331DL:

#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.
  // 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(6,x)); // The convertToG() function
    Serial.println(xl.convertToG(6,y)); // accepts as parameters the
    Serial.println(xl.convertToG(6,z)); // raw value and the current
    Serial.println(" ");                // maximum g-rating.
  }
  if (digitalRead(9) == HIGH)
  {
    Serial.println("Interrupt");
  }
}

My own adaption of the code:

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

LIS331 xl;

void setup() {
  
  pinMode(5,OUTPUT);    // CS for SPI
  digitalWrite(5,HIGH);
  pinMode(23,OUTPUT);    // MOSI for SPI
  pinMode(19,INPUT);     // MISO for SPI
  pinMode(18,OUTPUT);    // SCK for SPI
  
  SPI.begin();
  
  xl.setSPICSPin(5);
  xl.begin(LIS331::USE_SPI);

  Serial.begin(115200);

}

void loop() {
  
  int16_t x,y, z;
  xl.readAxes(x,y,z);
  
  Serial.print(x);Serial.print(" ");
  Serial.print(y);Serial.print(" ");
  Serial.println(z);

  delay(1000); 

}

I know that I have hooked it up correctly to my board.

Did you remove the solder bridge as Sparkfun explains in the hookup guide? Otherwise the SPI mode is not activated. If you closed the SA0 jumper you must remove that too.

Thank you Pylon. It works now. I somehow didn't read that part of the hookup guide.