ADXL345 SPI Communication

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
msawaftah
 
Posts: 5
Joined: Mon Oct 25, 2021 5:36 am

ADXL345 SPI Communication

Post by msawaftah »

Hello everyone,

i am trying to commuicate with a adxl345 mems-sensor with help of my ESP32 board. The I²C communication works fine without any problems but i am not getting the SPI communication working. I am getting zeros on the 3 axes. I have googled the problem and alot of people says that the spi interface should first be activated before using it

dose anyone know how to activate the spi interface?

My connections:

pin 19 -> MISO
Pin 18 -> CLK
Pin5 -> CS
Pin 23 -> MOSI

Here is my code

Code: Select all

#include <SparkFun_ADXL345.h>



long lastMsg = 0;
char msg[50];

ADXL345 adxl = ADXL345(5); // use pin number 5 for chipselect

void setup() {
  Serial.begin(9600);
  initADXL();

}




void loop() {
  int x, y, z;


  long now = millis();
  if (now - lastMsg > 250) {
    adxl.readAccel(&x, &y, &z);
    Serial.print(x);
    Serial.print(", ");
    Serial.print(y);
    Serial.print(", ");
    Serial.println(z);


  }
}

void initADXL() {
  delay (500);
  adxl.powerOn();                     // Power on the ADXL345
  delay (500);
  adxl.setRangeSetting(16);           // Give the range settings
  // Accepted values are 2g, 4g, 8g or 16g
  // Higher Values = Wider Measurement Range
  // Lower Values = Greater Sensitivity
  delay (500);
  adxl.setSpiBit(0);                  // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
  // Default: Set to 1
  // SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library
  delay (500);
  adxl.setActivityXYZ(1, 0, 0);       // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  adxl.setActivityThreshold(75);      // 62.5mg per increment   // Set activity   // Inactivity thresholds (0-255)
  adxl.setInactivityXYZ(1, 0, 0);     // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  adxl.setInactivityThreshold(75);    // 62.5mg per increment   // Set inactivity // Inactivity thresholds (0-255)
  adxl.setTimeInactivity(10);         // How many seconds of no activity is inactive?
  adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
  // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
  adxl.setTapThreshold(50);           // 62.5 mg per increment
  adxl.setTapDuration(15);            // 625 μs per increment
  adxl.setDoubleTapLatency(80);       // 1.25 ms per increment
  adxl.setDoubleTapWindow(200);       // 1.25 ms per increment
  // Set values for what is considered FREE FALL (0-255)
  adxl.setFreeFallThreshold(7);       // (5 - 9) recommended - 62.5mg per increment
  adxl.setFreeFallDuration(30);       // (20 - 70) recommended - 5ms per increment
  // Setting all interupts to take place on INT1 pin
  //adxl.setImportantInterruptMapping(1, 1, 1, 1, 1);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
  // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
  // This library may have a problem using INT2 pin. Default to INT1 pin.

  // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
  adxl.InactivityINT(1);
  adxl.ActivityINT(1);
  adxl.FreeFallINT(1);
  adxl.doubleTapINT(1);
  adxl.singleTapINT(1);
}

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: ADXL345 SPI Communication

Post by adafruit_support_carter »

Please post a photo of your setup showing how everything is connected.

User avatar
msawaftah
 
Posts: 5
Joined: Mon Oct 25, 2021 5:36 am

Re: ADXL345 SPI Communication

Post by msawaftah »

Here is a Schematic of the setup
Attachments
schematic.PNG
schematic.PNG (98.84 KiB) Viewed 293 times

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: ADXL345 SPI Communication

Post by adafruit_support_carter »

Can you post a photo of the actual setup?

User avatar
msawaftah
 
Posts: 5
Joined: Mon Oct 25, 2021 5:36 am

Re: ADXL345 SPI Communication

Post by msawaftah »

Sure

Here is my actual setup
Attachments
ss.PNG
ss.PNG (449.99 KiB) Viewed 282 times

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: ADXL345 SPI Communication

Post by adafruit_support_carter »

Are the header pins on the breakout soldered?
pins.jpg
pins.jpg (38.64 KiB) Viewed 278 times

User avatar
msawaftah
 
Posts: 5
Joined: Mon Oct 25, 2021 5:36 am

Re: ADXL345 SPI Communication

Post by msawaftah »

yeah they are solderd and everything is working fine with I²C (with the same sensor and same ESP32)

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: ADXL345 SPI Communication

Post by adafruit_support_carter »

Try with the Adafruit library:
https://github.com/adafruit/Adafruit_ADXL345

It allows specifying the specific pins being used:
https://github.com/adafruit/Adafruit_AD ... #L130-L131

User avatar
msawaftah
 
Posts: 5
Joined: Mon Oct 25, 2021 5:36 am

Re: ADXL345 SPI Communication

Post by msawaftah »

Sorry for the late reply and thank you for your answer.

Actually its working now with the sparkfunlibrary. I needed to add delays and to increase the Baudrate to 115200. The UART speed (Baudrate) was the limitation factor.

I am trying now to read the data using DATA_Ready_Interrupt but unfortunatlly i am not getting any data.

I am only getting once sensor data when i upload the code at the first begging and after that i dont recieve anything. I also have connected a saleae logic analayzer to the SPI bus but i dont see anything on the CLK or on the MISO or CS lines.

Am i using missing anything to install the interrupt?

I have commented the serialprint lines so that they dont slow down the communication. I had the same issue with interrupts with i2c until i commented the serialprint lines.

Here is my code

Code: Select all

#include <SparkFun_ADXL345.h>



const int int2Pin = 4;  // interrupt pin esp32 side
volatile bool dataReady = false;


ADXL345 adxl = ADXL345(5); // use pin number 5 for chipselect esp32 side

void setup() {
  Serial.begin(115200);
  pinMode(int2Pin, INPUT);
  
  //adxl.setRate (1600);
  //int rate = adxl.getRate ();
  //Serial.print("rate: ");
  //Serial.println(rate);
  //void setRate(double rate);

 
  initADXL();
   
   /*The first parameter to attachInterrupt() is an interrupt number. Normally you should use digitalPinToInterrupt(pin) to translate the actual digital pin to the specific interrupt number.
   The ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
    mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
          LOW:      to trigger the interrupt whenever the pin is low,
          CHANGE:   to trigger the interrupt whenever the pin changes value
          RISING:   to trigger when the pin goes from low to high,
          FALLING:  for when the pin goes from high to low. 
   */
 
  attachInterrupt(digitalPinToInterrupt(int2Pin), dataReadyISR, RISING);
  
  // If Set (0) Sets the Interrupts to Active HIGH
  // If Set (1) Sets the Interrupts to Active LOW
  adxl.setInterruptLevelBit(0);
  adxl.setInterruptMapping(ADXL345_DATA_READY, ADXL345_INT2_PIN); //interrupt configuration which Interrupt and which int pin on the ADXL345
  adxl.setInterrupt(ADXL345_DATA_READY, 1); //enable the interrupt

}




void loop() {
  int x, y, z;


  //long now = millis();
  if (dataReady==true) {
    adxl.readAccel(&x, &y, &z);
    //Serial.print(x);
    //Serial.print(", ");
    //Serial.print(y);
    //Serial.print(", ");
    //Serial.println(z);

    dataReady = false;

  }
}

void dataReadyISR(){
  dataReady = true;
}


void initADXL() {
  delay (500);
  adxl.powerOn();                     // Power on the ADXL345
  delay (500);
  adxl.setRangeSetting(16);           // Give the range settings
  // Accepted values are 2g, 4g, 8g or 16g
  // Higher Values = Wider Measurement Range
  // Lower Values = Greater Sensitivity
  delay (500);
  adxl.setSpiBit(0);                  // Configure the device to be in 4 wire SPI mode when set to '0' or 3 wire SPI mode when set to 1
  // Default: Set to 1
  // SPI pins on the ATMega328: 11, 12 and 13 as reference in SPI Library
  delay (500);
  adxl.setActivityXYZ(1, 0, 0);       // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  
  
  adxl.setActivityThreshold(75);      // 62.5mg per increment   // Set activity   // Inactivity thresholds (0-255)
  adxl.setInactivityXYZ(1, 0, 0);     // Set to detect inactivity in all the axes "adxl.setInactivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
  adxl.setInactivityThreshold(75);    // 62.5mg per increment   // Set inactivity // Inactivity thresholds (0-255)
  adxl.setTimeInactivity(10);         // How many seconds of no activity is inactive?
  adxl.setTapDetectionOnXYZ(0, 0, 1); // Detect taps in the directions turned ON "adxl.setTapDetectionOnX(X, Y, Z);" (1 == ON, 0 == OFF)
  // Set values for what is considered a TAP and what is a DOUBLE TAP (0-255)
  adxl.setTapThreshold(50);           // 62.5 mg per increment
  adxl.setTapDuration(15);            // 625 μs per increment
  adxl.setDoubleTapLatency(80);       // 1.25 ms per increment
  adxl.setDoubleTapWindow(200);       // 1.25 ms per increment
  // Set values for what is considered FREE FALL (0-255)
  adxl.setFreeFallThreshold(7);       // (5 - 9) recommended - 62.5mg per increment
  adxl.setFreeFallDuration(30);       // (20 - 70) recommended - 5ms per increment
  // Setting all interupts to take place on INT1 pin
  //adxl.setImportantInterruptMapping(1, 1, 1, 1, 1);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);"
  // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
  // This library may have a problem using INT2 pin. Default to INT1 pin.

  // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
  adxl.InactivityINT(0);
  adxl.ActivityINT(0);
  adxl.FreeFallINT(0);
  adxl.doubleTapINT(0);
  adxl.singleTapINT(0);

  
}
Thank you very much
Attachments
logic.PNG
logic.PNG (101.57 KiB) Viewed 258 times

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”