i want to get data X Y Z from adxl345 using i2c and also get interrupt via INT_1 of adxl345 if there is activity
it works to get xyz, but i cant get the interrupt when i move sensor.
here is code
#include <Wire.h>
const int ADXL345_ADDRESS = 0x53; // I2C address of ADXL345
const int INT1_PIN = 23; // Interrupt pin connected to ADXL345 INT1
volatile boolean adxl345_new_motion = false;
void setup() {
Serial.begin(9600);
Wire.begin();
// Configure ADXL345
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x2D); // Power control register
Wire.write(0x08); // Set to normal operation mode
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x2C); // Data rate register
Wire.write(0x09); // Set data rate to 0.78 Hz
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x31); // Data format register
Wire.write(0x0B); // Full resolution, +/- 4G range
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x27); // Activity threshold register
Wire.write(0x1E); // Set activity threshold (adjust as needed)
Wire.endTransmission();
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x2E); // Interrupt enable register
Wire.write(0x10); // Enable activity interrupt on INT1
Wire.endTransmission();
pinMode(INT1_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(INT1_PIN), adxl345_new_motionISR, RISING);
}
void loop() {
// Get X, Y, Z readings even without interrupt
int16_t x, y, z;
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x32); // Start reading at data register
Wire.endTransmission();
Wire.requestFrom(ADXL345_ADDRESS, 6);
if (Wire.available() >= 6) { // Check for available data before reading
x = Wire.read() | Wire.read() << 8;
y = Wire.read() | Wire.read() << 8;
z = Wire.read() | Wire.read() << 8;
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
} else {
Serial.println("No data available from ADXL345");
}
// Check for interrupt and handle it
if (adxl345_new_motion) {
// You can add specific actions here when activity is detected
Serial.println("Activity detected!");
adxl345_new_motion = false;
// Clear any pending interrupts
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(0x30); // Interrupt status register
Wire.endTransmission();
Wire.requestFrom(ADXL345_ADDRESS, 1);
Wire.read();
}
delay(1000);
}
void adxl345_new_motionISR() {
adxl345_new_motion = true;
}
if i use this library:
ADXL345_WE/src/ADXL345_WE.h at main · wollewald/ADXL345_WE · GitHub
i set settings like this but idk why in my code doesnt work if i try not to use this lib
adxl.setDataRate(ADXL345_DATA_RATE_0_78); //HZ: _3200 _1600 _800 _400 _200 _100 _50 _25 _12_5 _6_25 _3_13 _1_56 _0_78 _0_39 _0_20 _0_10
adxl.setRange(ADXL345_RANGE_4G); //ADXL345_RANGE_16G ADXL345_RANGE_8G ADXL345_RANGE_4G ADXL345_RANGE_2G
adxl.setActivityParameters(ADXL345_AC_MODE, ADXL345_XY0, 0.3); //AC_MODE nese differenca ndermjet infove qe vin me e madhe se shembull 0.3g , kurse DC_mode nese x ose y kalon 0.5g
adxl.setInterrupt(ADXL345_ACTIVITY, INT_PIN_1);
adxl.readAndClearInterrupts();
if (adxl345_new_motion == true) {
String axes = adxl.getActTapStatusAsString();
byte intSource = adxl.readAndClearInterrupts();
if (adxl.checkInterrupt(intSource, ADXL345_ACTIVITY)) {
Serial.print("Activity at: ");
Serial.println(axes);
i++;
}
delay(1000);
adxl.readAndClearInterrupts();
adxl345_new_motion = false;
}
}