Adxl345 SPI Arduino Uno the Code Runs Unstable

Hi everyone,

I have encountered a very weird situation that ruined my last week. I am trying to interrupt an adxl345 accelerometer with SPI communication using Sparkfun library for adxl345 and read the current time. The problem is that the code works sometimes fine as it is expected but when I changed small parameters, it stops working even if I turn the code back to its working one. Sometimes I plug out and in the connecting cable, and refresh my editor (I use online Arduino editor), and It may start working again or may not. I am so confused what causes this and so avoids me to do my work. I have tested several codes, and they result in the same unstable condition. Now, I will give an example to clarify it.

I have connected Adxl345 to Arduino Uno R3. Then I uploaded the code below.

#include <SparkFun_ADXL345.h>        
 
ADXL345 adxl = ADXL345(10);           
 
int interruptPin = 2;                
 
void ADXL_ISR();

void setup(){
   
  Serial.begin(9600);                 
  Serial.println("SparkFun ADXL345 Accelerometer Hook Up Guide Example");
  Serial.println();
   
  adxl.powerOn();                     
 
  adxl.setRangeSetting(16);           
 
  adxl.setSpiBit(0);                  
  adxl.setFullResBit(1);
  adxl.setActivityXYZ(1, 1, 1);       
  adxl.setActivityThreshold(10);      
  
  adxl.setImportantInterruptMapping(2, 2, 2, 1, 2);     

  adxl.InactivityINT(0);
  adxl.ActivityINT(1);
  adxl.FreeFallINT(0);
  adxl.doubleTapINT(0);
  adxl.singleTapINT(1);
   
attachInterrupt(digitalPinToInterrupt(interruptPin), ADXL_ISR, RISING);   // Attach Interrupt
}
 
/****** MAIN CODE ******/
/*     Accelerometer Readings and Interrupt    */
bool control = false;
void loop(){
    

  if(control) {
    control = false;
    Serial.println("inside");
      byte interrupts = adxl.getInterruptSource();
  }
 
}
 
/******* ISR *******/
/* Look for Interrupts and Triggered Action    */
void ADXL_ISR() {
  control = true;
 
  byte interrupts = adxl.getInterruptSource();
}

First, It ran and printed at the screen multiple "inside" which was expected because of the echos. Then I changed the threshold from 10 to 1. It started to run weirdly. When I started to tap on the sensor, it did not interrupt after a few taps. I changed it to 10 again. It ran perfectly. I again tried to change the activity threshold to 1. Wow, that time, the sensor decided to run as I hope. Then I decided to change

ADXL345 adxl = ADXL345(10);           
 
int interruptPin = 2;   

to

ADXL345 adxl = ADXL345(9);           
 
int interruptPin = 3;

of course, I also changed pin connections. Interrupt function stopped to run. You might say that cs pin should be connected to Arduino's ss pin (10) but I earlier tested 9 and got successful results without having any idea why it works. But the story does not end here. When I turn back the old code, and reconnected the pins, it was still not working. I would say those pins might be broken or not suitable for adxl345 if I did not try it in a different Arduino Uno board. I have encountered several different annoying conditions so similar to that I told with different adxl345 sensors and boards and cables. Now, I am close to lose my reality perception :sweat_smile:

I would be so glad if you have any explanation to what might be going on under the hood.

Thanks in advance.

You can use any digitial IO pin to drive the CS pin. The SS pin is only fixed if the Arduino is operating as an SPI slave.

The ADXL345 is a 3.3V device. You must not connect any voltage higher than 3.3V to any of it's pins! That include the SPI pins. The UNO runs on 5V, so all digital IOs have the same voltage if driven HIGH. Fortunately the pins aren't driven HIGH for long time frames using SPI, that's what probably saved the ADXL345 from being fried. You must use level converters for all SPI signals and if you want to use another device on the SPI bus the level converters must be SPI safe (tri-state on CS HIGH).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.