I am trying to connect a DPS310 and an ADXL345 to my Arduino using 4 wire SPI. When the individually tested (i.e. each has its own script), the output is correct, however when the scripts are combined the ADXL output is garbage, but the DPS functions.
What we expect to see:
Temperature = 24.14 *C
Pressure = 857.88 hPa
X: -4 Y: 4 Z: 29
What we actually see:
Temperature = 24.14 *C
Pressure = 857.88 hPa
X: 0 Y: 0 Z: 0 // and does not change when tilted
When the DPS310 line is commented out, the ADXL works fine (this line is indicated in script)
The whole script (with the faulty line indicated):
#include <SparkFun_ADXL345.h>
#include <Adafruit_DPS310.h>
// ADXL obj
#define ADXL345_CS 9 // SPI pin
ADXL345 ADXL = ADXL345_CS; // Instantiate obj with CS pin
int x = 0, y = 0, z = 0;
// DPS obj
#define DPS310_CS 10 // SPI pin
Adafruit_DPS310 dps; // Instantiate obj (pin is set in setup)
sensors_event_t temp_event, pressure_event;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
// ADXL SETUP
digitalWrite(ADXL345_CS, LOW); // START COMM
Serial.print("ADXL345: ");
ADXL.powerOn();
delay(50);
ADXL.setRangeSetting(16);
ADXL.setSpiBit(0);
ADXL.set_bw(ADXL345_BW_12_5);
ADXL.setInterrupt(0x84, true);
ADXL.setAxisOffset(0, 0, 5);
ADXL.readAccel(&x, &y, &z);
if(x != -1 && y != -1 && z != -1) {
Serial.println("[PASSED]");
} else {
Serial.println("[FAILED]");
}
digitalWrite(ADXL345_CS, HIGH); // END COMM
// DPS SETUP
digitalWrite(DPS310_CS, LOW); // START COMM
Serial.print("DPS310: ");
if (! dps.begin_SPI(DPS310_CS)) { /////////////////PROBLEM LIES HERE//////////////////
Serial.println(" [FAILED]");
}
Serial.println(" [PASSED]");
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
digitalWrite(DPS310_CS, HIGH); // END COMM
}
void loop() {
digitalWrite(ADXL345_CS, LOW);
ADXL.readAccel(&x, &y, &z);
digitalWrite(ADXL345_CS, HIGH);
digitalWrite(DPS310_CS, LOW);
dps.getEvents(&temp_event, &pressure_event);
digitalWrite(DPS310_CS, HIGH);
Serial.print("Temperature = ");
Serial.print(temp_event.temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(pressure_event.pressure);
Serial.println(" hPa");
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
delay(50);
}
I think the fault might lie in the classes written for these devices, for instance maybe they were written for single SPI device or have different frequencies/modes? (I'm probably wrong though XD). I have not yet tried to setup the SPI for each device manually (programming commandment: do no reinvent the wheel), so any help would be greatly appreciated.