Introduction
This experiment demonstrates how to use the ADXL345 accelerometer to measure acceleration in three axes (X, Y, Z). The data from the sensor is sent to the Arduino and displayed on the serial monitor.
Components Needed
- Arduino (e.g., Uno, Nano)
- ADXL345 Accelerometer
- Jumper wires
- Optional: LCD/OLED Display to show the data
Circuit Setup
- Connect the VCC pin of the ADXL345 to the 3.3V pin on the Arduino.
- Connect the GND pin to GND on the Arduino.
- Connect the SDA pin to A4 on the Arduino (I2C data pin).
- Connect the SCL pin to A5 on the Arduino (I2C clock pin).
Ensure that the sensor is properly connected and powered before uploading the code.
Code for ADXL345 Accelerometer
Upload the following code to your Arduino to read acceleration data from the ADXL345:
#include
#include
#include
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
void setup() {
Serial.begin(9600);
if (!accel.begin()) {
Serial.println("Couldn't find ADXL345 sensor");
while (1);
}
}
void loop() {
sensors_event_t event;
accel.getEvent(&event);
Serial.print("X: ");
Serial.print(event.acceleration.x);
Serial.print(" m/s^2, ");
Serial.print("Y: ");
Serial.print(event.acceleration.y);
Serial.print(" m/s^2, ");
Serial.print("Z: ");
Serial.print(event.acceleration.z);
Serial.println(" m/s^2");
delay(500);
}
Explanation
The ADXL345 sensor provides data for the acceleration along the X, Y, and Z axes. In the code:
accel.getEvent(&event)
: Retrieves the acceleration data from the sensor.event.acceleration.x
,event.acceleration.y
,event.acceleration.z
: Accesses the acceleration data for each axis (X, Y, and Z).- The acceleration data is printed to the serial monitor in meters per second squared (m/s²).
Troubleshooting
- If no data appears, check the wiring and make sure the ADXL345 is properly powered and connected to the correct pins.
- If the readings seem incorrect, ensure that the correct I2C address is used in the code.
- Make sure the necessary libraries for the ADXL345 are installed in your Arduino IDE.