ADXL345 3-axis Acceleration Sensor sample

Dependencies:   ADXL345_I2C mbed

Fork of ADXL345 by James Allen

About ADXL345

ADXL345 is a 3-axis acceleration sensor and can be controlled by using the I2C.

About sample program

This program outputs the acceleration information of 3-axis to the terminal.
The 3-axis is as follows:
/media/uploads/1050186/adxl345_axis_1.png
In the graph, the acceleration information of 3-axis showes.
/media/uploads/1050186/tilt.png /media/uploads/1050186/adxl345_output_to_graph_1.png

About wiring

SensorGR-PEACH
VDD3.3V
GNDGND
SCLD15
SDAD14
Committer:
1050186
Date:
Thu Apr 28 11:23:58 2016 +0000
Revision:
1:c835cfa8b41c
Parent:
0:75c7bfd01ea0
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jrha 0:75c7bfd01ea0 1 #include "mbed.h"
jrha 0:75c7bfd01ea0 2 #include "ADXL345_I2C.h"
jrha 0:75c7bfd01ea0 3
1050186 1:c835cfa8b41c 4 ADXL345_I2C accelerometer(I2C_SDA, I2C_SCL);
jrha 0:75c7bfd01ea0 5 Serial pc(USBTX, USBRX);
jrha 0:75c7bfd01ea0 6
jrha 0:75c7bfd01ea0 7 int main()
jrha 0:75c7bfd01ea0 8 {
jrha 0:75c7bfd01ea0 9
jrha 0:75c7bfd01ea0 10 int readings[3] = {0, 0, 0};
jrha 0:75c7bfd01ea0 11
jrha 0:75c7bfd01ea0 12 pc.printf("Starting ADXL345 test...\n");
jrha 0:75c7bfd01ea0 13 pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
jrha 0:75c7bfd01ea0 14
jrha 0:75c7bfd01ea0 15 //Go into standby mode to configure the device.
jrha 0:75c7bfd01ea0 16 accelerometer.setPowerControl(0x00);
jrha 0:75c7bfd01ea0 17
jrha 0:75c7bfd01ea0 18 //Full resolution, +/-16g, 4mg/LSB.
jrha 0:75c7bfd01ea0 19 accelerometer.setDataFormatControl(0x0B);
jrha 0:75c7bfd01ea0 20
jrha 0:75c7bfd01ea0 21 //3.2kHz data rate.
jrha 0:75c7bfd01ea0 22 accelerometer.setDataRate(ADXL345_3200HZ);
jrha 0:75c7bfd01ea0 23
jrha 0:75c7bfd01ea0 24 //Measurement mode.
jrha 0:75c7bfd01ea0 25 accelerometer.setPowerControl(0x08);
jrha 0:75c7bfd01ea0 26
jrha 0:75c7bfd01ea0 27 while (1)
jrha 0:75c7bfd01ea0 28 {
jrha 0:75c7bfd01ea0 29
jrha 0:75c7bfd01ea0 30 wait(0.1);
jrha 0:75c7bfd01ea0 31
jrha 0:75c7bfd01ea0 32 accelerometer.getOutput(readings);
jrha 0:75c7bfd01ea0 33
jrha 0:75c7bfd01ea0 34 //13-bit, sign extended values.
jrha 0:75c7bfd01ea0 35 pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
jrha 0:75c7bfd01ea0 36 }
jrha 0:75c7bfd01ea0 37
jrha 0:75c7bfd01ea0 38 }