Hi all!
I've recently finished my first prototype of a project in an Arduino Uno involving an ADXL345 with an SPI comms, and Sparkfun ADXL345 library. I had to migrate to a Teensy 3.2 because of power and packaging size requirements, however, it seems that the ADXL345 does one of the following:
- returns non-changing values
- returns random values
- triggers certain registered interrupts
I've followed Sparkfun's hookup guide https://learn.sparkfun.com/tutorials/adxl345-hookup-guide, and have utilized their samples to test my accelerometer being integrated with the teensy board but no gives. Since Teensy 3.2 also has an SPI port in pins 10-13, i don't think there would be any problems with the connections. Below is how I connected the said connections:
Here is my sample code for reference (note: I have modified Sparkfun's sample for testing):
My question now is should I normally experience such behavior with Sparkfun ADXL345 library and Teensy3.2? Have others experienced this as well? If it does not work, what alternative could I use instead? Please help, thanks!
Lamey
I've recently finished my first prototype of a project in an Arduino Uno involving an ADXL345 with an SPI comms, and Sparkfun ADXL345 library. I had to migrate to a Teensy 3.2 because of power and packaging size requirements, however, it seems that the ADXL345 does one of the following:
- returns non-changing values
- returns random values
- triggers certain registered interrupts
I've followed Sparkfun's hookup guide https://learn.sparkfun.com/tutorials/adxl345-hookup-guide, and have utilized their samples to test my accelerometer being integrated with the teensy board but no gives. Since Teensy 3.2 also has an SPI port in pins 10-13, i don't think there would be any problems with the connections. Below is how I connected the said connections:
Teensy 3.2 | ADXL345 |
3.3V | VDD |
GND | GND |
10 | CS |
11 | DI |
12 | DO |
13 | CLK |
Here is my sample code for reference (note: I have modified Sparkfun's sample for testing):
Code:
#include <SparkFun_ADXL345.h> // SparkFun ADXL345 Library
/*********** COMMUNICATION SELECTION ***********/
/* Comment Out The One You Are Not Using */
ADXL345 adxl = ADXL345(10); // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
/****************** VARIABLES ******************/
/* */
int AccelMinX = 0;
int AccelMaxX = 0;
int AccelMinY = 0;
int AccelMaxY = 0;
int AccelMinZ = 0;
int AccelMaxZ = 0;
int x,y,z;
// for 16g
#define offsetX -1 // OFFSET values
#define offsetY -25
#define offsetZ 41
#define gainX -4 // GAIN factors
#define gainY -4
#define gainZ 4
/*//for 2g
#define offsetX -2 // OFFSET values
#define offsetY -193
#define offsetZ 277
#define gainX -29 // GAIN factors
#define gainY -28
#define gainZ 24*/
/******************** SETUP ********************/
/* Configure ADXL345 Settings */
void setup(){
Serial.begin(115200); // Start the serial terminal
Serial.println("SparkFun ADXL345 Accelerometer Hook Up Guide Example");
Serial.println();
adxl.powerOn(); // Power on the ADXL345
adxl.setRangeSetting(16); // Give the range settings
// Accepted values are 2g, 4g, 8g or 16g
// Higher Values = Wider Measurement Range
// Lower Values = Greater Sensitivity
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
adxl.setRate(100);
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(1);
adxl.singleTapINT(1);
}
/****************** MAIN CODE ******************/
/* Accelerometer Readings and Interrupt */
void loop(){
obtainSample();
ADXL_ISR();
delay(1000);
}
void obtainSample(){
// Accelerometer Readings
adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
/* Note: Must perform offset and gain calculations prior to seeing updated results
/ Refer to SparkFun ADXL345 Hook Up Guide: [url]https://learn.sparkfun.com/tutorials/adxl345-hookup-guide[/url]
/ offsetAxis = 0.5 * (Acel+1g + Accel-1g)
/ gainAxis = 0.5 * ((Acel+1g - Accel-1g)/1g) */
// UNCOMMENT SECTION TO VIEW NEW VALUES
x = (x - offsetX)/gainX; // Calculating New Values for X, Y and Z
y = (y - offsetY)/gainY;
z = (z - offsetZ)/gainZ;
if(x < AccelMinX) AccelMinX = x;
if(x > AccelMaxX) AccelMaxX = x;
if(y < AccelMinY) AccelMinY = y;
if(y > AccelMaxY) AccelMaxY = y;
if(z < AccelMinZ) AccelMinZ = z;
if(z > AccelMaxZ) AccelMaxZ = z;
}
/********************* ISR *********************/
/* Look for Interrupts and Triggered Action */
void ADXL_ISR() {
byte interrupts = adxl.getInterruptSource();
// Double Tap Detection
if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
Serial.println("*** DOUBLE TAP ***");
//add code here to do when a 2X tap is sensed
} else if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
Serial.println("*** TAP ***");
//add code here to do when a tap is sensed
}
}
Lamey