1

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(&amp;temp_event, &amp;pressure_event);
digitalWrite(DPS310_CS, HIGH);

Serial.print(&quot;Temperature = &quot;);
Serial.print(temp_event.temperature);
Serial.println(&quot; *C&quot;);
Serial.print(&quot;Pressure = &quot;);
Serial.print(pressure_event.pressure);
Serial.println(&quot; hPa&quot;); 

Serial.print(&quot;X: &quot;);
Serial.print(x);
Serial.print(&quot;  Y: &quot;);
Serial.print(y);
Serial.print(&quot;  Z: &quot;);
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.

newt
  • 25
  • 6

1 Answers1

1

The ADXL345 uses SPI_MODE3 and DPS310 the SPI_MODE0. The libraries for these devices don't use SPI transactions, but set the settings only in begin(), so they can't be used together.

Your options to solve this is either to put one sensor on software SPI or to put one or both sensors on I2C.

The ADXL345 can be used as I2C device. Its library supports I2C with the constructor when no parameters are provided. Does your module have an option to wire it to I2C?

The Adafruit library for DPS310 has built-in software SPI, so you can use it with any set of pins. And it too supports I2C.

newt
  • 25
  • 6
Juraj
  • 18,264
  • 4
  • 31
  • 49