Introduction: Measurement of Acceleration Using ADXL345 and Arduino Nano
The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through I2 C digital interface. Itmeasures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (3.9 mg/LSB) enables measurement of inclination changes less than 1.0°.
In this tutorial the interfacing of the ADXL345 sensor module with arduino nano has been illustrated. To read the acceleration values, we have used arduino with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.
Step 1: Hardware Required:
Step 2: Hardware Hookup:
The hardware hookup section basically explains the wiring connections required between the sensor and the arduino nano. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:
The ADXL345 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.
Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic.
All you need is four wires! Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.
These connections are demonstrated in the pictures above.
Step 3: Code for Measurement of Acceleration:
Lets start with the arduino code now.
While using the sensor module with the arduino, we include Wire.h library. "Wire" library contains the functions which facilitate the i2c communication between the sensor and the arduino board.
The entire arduino code is given below for the convenience of the user:
#include<Wire.h>
// ADXL345 I2C address is 0x53(83)
#define Addr 0x53
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select bandwidth rate register
Wire.write(0x2C);
// Normal mode, Output data rate = 100 Hz
Wire.write(0x0A);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select power control register
Wire.write(0x2D);
// Auto-sleep disable
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data format register
Wire.write(0x31);
// Self test disabled, 4-wire interface, Full resolution, Range = +/-2g
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
} void loop()
{
unsigned int data[6];
for(int i = 0; i < 6; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((50 + i));
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
}
// Convert the data to 10-bits
int xAccl = (((data[1] & 0x03) * 256) + data[0]);
if(xAccl > 511)
{
xAccl -= 1024;
}
int yAccl = (((data[3] & 0x03) * 256) + data[2]);
if(yAccl > 511)
{
yAccl -= 1024;
}
int zAccl = (((data[5] & 0x03) * 256) + data[4]);
if(zAccl > 511)
{
zAccl -= 1024;
}
// Output data to serial monitor
Serial.print("Acceleration in X-Axis is : ");
Serial.println(xAccl);
Serial.print("Acceleration in Y-Axis is : ");
Serial.println(yAccl);
Serial.print("Acceleration in Z-Axis is : ");
Serial.println(zAccl);
delay(300);
}
In wire library Wire.write() and Wire.read() is used to write the commands and read the sensor output.
Serial.print() and Serial.println() is used to display the output of the sensor on the serial monitor of the Arduino IDE.
The output of the sensor is shown in the picture above.
Step 4: Applications:
ADXL345 is a small, thin, ultralow power, 3-axis accelerometer which can be employed in Handsets, Medical instrumentation etc. Its application also includes Gaming and pointing devices, Industrial instrumentation, Personal navigation devices and Hard disk drive (HDD) protection.