Introduction
Welcome to the realm of Arduino and motion sensing! In this guide, we’ll delve into How to Use an ADXL345 Accelerometer Sensor with Arduino. The ADXL345 represents a potent and adaptable sensor module enabling the measurement of acceleration across three axes. Whether you’re crafting a tilt sensor, motion tracker, or gesture recognition system, the ADXL345 accelerometer furnishes invaluable data for a myriad of projects. Let’s embark on a journey to explore the intricacies of the ADXL345 accelerometer and discover how to seamlessly integrate it with Arduino.
Hardware Required
You will require the following Hardware Components for the How to Interfacing ADXL345 Accelerometer Sensor with Arduino.
Components | # | Buy From Amazon |
---|---|---|
Arduino UNO | 1 | Buy Link |
ADX345 Accelerometer | 1 | Buy Link |
9v DC Adapter (Optional) | 1 | Buy Link |
Jumper Wires | 4 | Buy Link |
Breadboard | 1 | Buy Link |
What is an ADXL345 Accelerometer?
The ADXL345 stands as a three-axis accelerometer sensor crafted by Analog Devices. It is meticulously engineered to gauge acceleration along three orthogonal axes: X, Y, and Z. Facilitating communication with Arduino via the I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) interface, the ADXL345 proves facile to incorporate into Arduino projects. With its diminutive footprint, minimal power consumption, and impressive resolution, the ADXL345 accelerometer proves apt for diverse applications necessitating motion sensing and orientation detection.
Pinout
Pin Configuration
Pin Number | Pin Name | Description |
---|---|---|
1 | +5V | Power supply voltage input (5V) |
2 | +3V3 | Power supply voltage input (3.3V) |
3 | GND | Ground |
4 | SCL | Serial Clock (I2C) |
5 | SDA | Serial Data (I2C) |
6 | CS | Chip Select (SPI) |
7 | US | Unused or not connected (NC) |
8 | SDO | Serial Data Out (SPI) |
9 | IN1 | Input/Output pin 1 (function configurable) |
10 | IN2 | Input/Output pin 2 (function configurable) |
Features
- Three-Axis Acceleration Sensing: Simultaneous measurement of acceleration along the X, Y, and Z axes, facilitating comprehensive motion sensing capabilities.
- Digital Output: Furnishes digital output data for effortless integration with microcontrollers like Arduino.
- Low Power Consumption: Engineered for low-power operation, rendering it ideal for battery-powered applications.
- High Resolution: Delivers high-resolution measurements for accurate motion detection and analysis.
- Built-in FIFO Buffer: Includes a built-in FIFO (First In, First Out) buffer for efficient storage and retrieval of acceleration data, enhancing data processing efficiency.
Specifications
- Acceleration Measurement:
- Measurement Range: ±2g, ±4g, ±8g, ±16g (selectable).
- Resolution: Up to 13 bits.
- Sensitivity: Adjustable.
- Communication Interface:
- I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) interface for seamless communication with Arduino.
- Operating voltage: 2.0V to 3.6V.
- Sampling Rate:
- Programmable sampling rates up to 3200Hz.
- Temperature Range:
- Operating Temperature: -40°C to +85°C.
- Dimensions:
- Compact form factor conducive to integration into a myriad of electronic projects.
- Standard breakout board design streamlining connection with Arduino.
Circuit Diagram
The following circuit shows you the connection of the How to Use an ADXL345 Accelerometer Sensor with Arduino, Please make the connection carefully
Circuit Connections
Arduino | ADXL345 Sensor |
---|---|
+3.3V | VCC Pin |
+3.3V | CS |
GND | GND Pin |
A4 | SDA |
A5 | SCL |
Installing Arduino IDE Software
First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow
Installing Libraries
Now when you are Ready to upload the code, to the Arduino Board you will need first to add the Following Libraries in Arduino, If you Learn How to add the library in the Arduino step-by-step guide click on how to install the library Button given Blow
Code
//For more Projects: www.arduinocircuit.com
// Gnd - GND
// 3.3v - VCC
// 3.3v - CS
// Analog 4 - SDA
// Analog 5 - SLC
#include <Wire.h>
//Device address
const int DEVICE_ADDRESS = (0x53);
byte _buff[6];
//ADXL345 register addresses
char POWER_CTL = 0x2D;
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
void setup()
{
Serial.begin(57000);
Serial.print("Started");
Wire.begin();
writeTo(DEVICE_ADDRESS, DATA_FORMAT, 0x01); //Set ADXL345 to +- 4G
writeTo(DEVICE_ADDRESS, POWER_CTL, 0x08); //Put the ADXL345
}
void loop()
{
readAccel(); //Read acceleration x, y, z
delay(500);
}
void readAccel() {
//Read the data
uint8_t numBytesToRead = 6;
readFrom(DEVICE_ADDRESS, DATAX0, numBytesToRead, _buff);
//Read the values from the register and convert to int (Each axis has 10 bits, in 2 Bytes LSB)
int x = (((int)_buff[1]) << 8) | _buff[0];
int y = (((int)_buff[3]) << 8) | _buff[2];
int z = (((int)_buff[5]) << 8) | _buff[4];
Serial.print("x: ");
Serial.print( x );
Serial.print(" and: ");
Serial.print( and );
Serial.print(" z: ");
Serial.println( z );
}
//Auxiliary writing function
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device);
Wire.write(address);
Wire.write(val);
Wire.endTransmission();
}
//Auxiliary reading function
void readFrom(int device, byte address, int num, byte _buff[]) {
Wire.beginTransmission(device);
Wire.write(address);
Wire.endTransmission();
Wire.beginTransmission(device);
Wire.requestFrom(device, num);
int i = 0;
while(Wire.available())
{
_buff[i] = Wire.read();
i++;
}
Wire.endTransmission();
}
We can use libraries like Sparkfun’s for the ADXL345 In above, offering I2C and SPI communication, configuration, and interrupt handling for activity detection. The library includes code examples, with the following being a condensed version that achieves similar functionality with less code.
//For more Projects: www.arduinocircuit.com
#include <SPI.h>
#include <Wire.h>
#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345();
void setup()
{
Serial.begin(9600);
Serial.println("Start");
Serial.println();
adxl.powerOn();
adxl.setRangeSetting(16); //Define the range, values 2, 4, 8 or 16
}
void loop()
{
//read the values and print them
int x, y, z;
adxl.readAccel(&x, &y, &z);
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
}
Applications
- Tilt Sensing: Employ the ADXL345 accelerometer to discern tilt and orientation alterations in applications such as gaming controllers, robotics, and electronic levels.
- Motion Tracking: Implement motion tracking systems leveraging the ADXL345 accelerometer to monitor movement and acceleration in sports analysis, fitness tracking, and activity monitoring devices.
- Gesture Recognition: Develop gesture recognition systems harnessing the ADXL345 accelerometer to identify and interpret hand gestures for seamless human-computer interaction in smart devices and wearable technology.
- Vehicle Dynamics Monitoring: Integrate the ADXL345 accelerometer into vehicle dynamics monitoring systems to measure acceleration, inclination, and vibration, facilitating applications such as vehicle stability control and rollover detection.
- Structural Health Monitoring: Utilize the ADXL345 accelerometer for structural health monitoring in civil engineering applications, monitoring vibrations and movements in buildings, bridges, and infrastructure to detect structural defects and damage early on.
Conclusion
With its advanced features and adaptability, the ADXL345 accelerometer offers boundless possibilities for motion sensing and orientation tracking in Arduino projects. Whether crafting tilt sensors, motion trackers, gesture recognition systems, vehicle dynamics monitors, or structural health monitoring devices, the ADXL345 provides precise and reliable acceleration data, enhancing project functionality and performance. Let’s embark on the journey of employing the ADXL345 accelerometer with Arduino and unlock the potential of motion sensing in your projects!