ADXL345 Accelerometer Module Documentation
The ADXL345 Accelerometer Module is a high-resolution, low-power, 3-axis accelerometer that measures static acceleration due to gravity as well as dynamic acceleration resulting from motion, shock, or vibration. This module is suitable for a wide range of applications, including vibration monitoring, impact detection, and motion sensing.
The ADXL345 Accelerometer Module has the following pins:
VCC: Power supply pin (operating voltage: 2.0-3.6V)
GND: Ground pin
SCL: I2C clock pin
SDA: I2C data pin
INT1/INT2: Interrupt pins
Measurement range: 2g, 4g, 8g, or 16g
Resolution: 10-bit or 13-bit
Noise density: 220 g/Hz
Bandwidth: 0.5-1600 Hz
Supply current: 40 A (typical)
### Example 1: Basic Acceleration Reading using Arduino
This example demonstrates how to read acceleration data from the ADXL345 Accelerometer Module using Arduino.
#define ADXL345_ADDRESS 0x1D // I2C address of the ADXL345
#define ADXL345_DATAREAD 0x32 // Register address for reading acceleration data
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
int x, y, z;
Wire.beginTransmission(ADXL345_ADDRESS);
Wire.write(ADXL345_DATAREAD);
Wire.endTransmission();
Wire.requestFrom(ADXL345_ADDRESS, 6);
x = Wire.read() | (Wire.read() << 8);
y = Wire.read() | (Wire.read() << 8);
z = Wire.read() | (Wire.read() << 8);
Serial.print("Acceleration: ");
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
### Example 2: Motion Detection using Raspberry Pi (Python)
This example demonstrates how to use the ADXL345 Accelerometer Module to detect motion using a Raspberry Pi and Python.
```python
import smbus
import time
# Define the I2C bus and ADXL345 address
bus = smbus.SMBus(1)
ADXL345_ADDRESS = 0x1D
# Define the threshold for motion detection
THRESHOLD = 50
while True:
# Read acceleration data from the ADXL345
bus.write_byte(ADXL345_ADDRESS, 0x2A)
bus.write_byte(ADXL345_ADDRESS, 0x00) # Reset the offset registers
data = bus.read_i2c_block_data(ADXL345_ADDRESS, 0x00, 6)
x = data[1] | (data[0] << 8)
y = data[3] | (data[2] << 8)
z = data[5] | (data[4] << 8)
# Calculate the magnitude of the acceleration
magnitude = (x2 + y2 + z2) 0.5
# Check for motion
if magnitude > THRESHOLD:
print("Motion detected!")
else:
print("No motion detected.")
### Example 3: Interrupt-based Motion Detection using ESP32 (C++)
This example demonstrates how to use the ADXL345 Accelerometer Module to detect motion using an ESP32 board and the Arduino IDE.
```c++
#include <WiFi.h>
#include <Wire.h>
#define ADXL345_ADDRESS 0x1D
#define ADXL345_INT1_PIN 2 // Interrupt pin on the ESP32
volatile bool motionDetected = false;
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(ADXL345_INT1_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(ADXL345_INT1_PIN), motionISR, RISING);
}
void loop() {
if (motionDetected) {
Serial.println("Motion detected!");
motionDetected = false;
}
delay(100);
}
void motionISR() {
motionDetected = true;
}
```
In this example, the ADXL345 is configured to generate an interrupt when motion is detected, and the ESP32 board is configured to handle this interrupt using an interrupt service routine (ISR). When motion is detected, the ISR sets a flag that is checked in the main loop, and if set, prints a message to the serial console.