I am using an Adafruit H3LIS331 high-range accelerometer and LSM9DS1 mid-range IMU over SPI. I was hoping to save myself some work by using the Adafuit Arduino libraries but they seem to be interfering with one another. They work individually but misbehave when connected. Here is my code:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_H3LIS331.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h>
// Used for software SPI
#define SCK 76
#define MISO 74
#define MOSI 75
// Used for hardware & software SPI
#define H3LIS331_CS 52
#define LSM9DS1_AGCS 4
#define LSM9DS1_MCS 10
Adafruit_H3LIS331 HighA = Adafruit_H3LIS331();
Adafruit_LSM9DS1 MidIMU = Adafruit_LSM9DS1(LSM9DS1_AGCS, LSM9DS1_MCS);
void getHighA(void);
void getMidIMU(void);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Test!");
//MidIMU setup
if (!MidIMU.begin()){
Serial.println("Couldnt start MidIMU");
while (1) yield();
}
Serial.println("Found LSM9DS1 9DOF");
MidIMU.setupAccel(MidIMU.LSM9DS1_ACCELRANGE_2G);
MidIMU.setupMag(MidIMU.LSM9DS1_MAGGAIN_4GAUSS);
MidIMU.setupGyro(MidIMU.LSM9DS1_GYROSCALE_245DPS);
//MidIMU.endTransaction()
//HighA setup
if(!HighA.begin_SPI(H3LIS331_CS,SCK,MISO,MOSI)){
Serial.println("Couldnt start HighA");
while (1) yield();
}
Serial.println("H3LIS331 found!");
HighA.setRange(H3LIS331_RANGE_100_G);
HighA.setDataRate(LIS331_DATARATE_1000_HZ);
}
void loop() {
// put your main code here, to run repeatedly:
getHighA();
getMidIMU();
delay(1000);
}
void getHighA(void){
sensors_event_t high_a_event;
HighA.getEvent(&high_a_event);
Serial.print("High Accel X: "); Serial.print(high_a_event.acceleration.x); Serial.print(" m/s^2");
Serial.print("\tY: "); Serial.print(high_a_event.acceleration.y); Serial.print(" m/s^2 ");
Serial.print("\tZ: "); Serial.print(high_a_event.acceleration.z); Serial.println(" m/s^2 ");
}
void getMidIMU(void){
MidIMU.read();
sensors_event_t a, m, g, temp;
MidIMU.getEvent(&a, &m, &g, &temp);
Serial.print("Mid Accel X: "); Serial.print(a.acceleration.x); Serial.print(" m/s^2");
Serial.print("\tY: "); Serial.print(a.acceleration.y); Serial.print(" m/s^2 ");
Serial.print("\tZ: "); Serial.print(a.acceleration.z); Serial.println(" m/s^2 ");
Serial.print("Mag X: "); Serial.print(m.magnetic.x); Serial.print(" uT");
Serial.print("\tY: "); Serial.print(m.magnetic.y); Serial.print(" uT");
Serial.print("\tZ: "); Serial.print(m.magnetic.z); Serial.println(" uT");
Serial.print("Gyro X: "); Serial.print(g.gyro.x); Serial.print(" rad/s");
Serial.print("\tY: "); Serial.print(g.gyro.y); Serial.print(" rad/s");
Serial.print("\tZ: "); Serial.print(g.gyro.z); Serial.println(" rad/s");
Serial.println();
}
The output of this yields correct data for the H3LIS331 high range accelerometer but the data out of LSM9DS1 is non-changing and incorrect. If the order of MidIMU and HighA are swapped in setup(), then the opposite occurs where the LSM9DS1 data is correct when H3LIS331 is fake.
After some research, it appears that SD card modules normally cause this issue. I will be adding an SD card to this project later but it is not even working in its current state. The only path forward that I see is to not use the premade libraries and to start coding my own SPI functions. Any help or insight with this issue would be greatly appreciated.