Measurement of Acceleration Using ADXL345 and Particle Photon
by Dcube Tech Ventures in Circuits > Electronics
1517 Views, 0 Favorites, 0 Comments
Measurement of Acceleration Using ADXL345 and Particle Photon
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 particle photon has been illustrated. To read the acceleration values, we have used particle with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.
Hardware Required:
Hardware Hookup:
The hardware hookup section basically explains the wiring connections required between the sensor and the particle photon. 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.
Code for Measurement of Acceleration:
Lets start with the particle code now.
While using the sensor module with the particle, we include application.h and spark_wiring_i2c.h library. "application.h" and spark_wiring_i2c.h library contains the functions which facilitate the i2c communication between the sensor and the particle.
The entire particle code is given below for the convenience of the user:
#include<application.h>
#include<spark_wiring_i2c.h>
// ADXL345 I2C address is 0x53(83)
#define Addr 0x53
int xAccl = 0, yAccl = 0, zAccl = 0;
void setup()
{
// Set variable
Particle.variable("i2cdevice","ADXL345");
Particle.variable("xAccl",xAccl);
Particle.variable("yAccl",yAccl);
Particle.variable("zAccl",zAccl);
// 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);
// Select 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);
// Select auto sleep disable
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C transmission
Wire.beginTransmission(Addr);
// Select data format register
Wire.write(0x31);
// Select full resolution, +/-2g
Wire.write(0x08);
// End 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 from the device
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();
}
delay(300);
}
// 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 dashboard
Particle.publish("Acceleration in X-Axis is :", String(xAccl));
Particle.publish("Acceleration in Y-Axis is :", String(yAccl));
Particle.publish("Acceleration in Z-Axis is :", String(zAccl));
}
Particle.variable() function creates the variables to store the output of the sensor and Particle.publish() function displays the output on the dashboard of the site.
The sensor output is shown in the picture above for your reference.
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.