You are on page 1of 3

ADXL345 Accelerometer

ADXL345 Signal Name

mbed pin

Vcc

Vout

Gnd

Gnd

SDA

p5

SDO

p6

SCL

p7

CS

p8

The ADXL345 is a small, thin, low 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 either a SPI (3- or 4-wire) or I2C digital interface. Libraries for both
communication protocols are provided below.

main.cpp
#include "ADXL345.h"
ADXL345 accelerometer(p5, p6, p7, p8);
Serial pc(USBTX, USBRX);
int main() {
int readings[3] = {0, 0, 0};
pc.printf("Starting ADXL345 test...\n");
pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
//Go into standby mode to configure the device.
accelerometer.setPowerControl(0x00);
//Full resolution, +/-16g, 4mg/LSB.
accelerometer.setDataFormatControl(0x0B);
//3.2kHz data rate.
accelerometer.setDataRate(ADXL345_3200HZ);
//Measurement mode.
accelerometer.setPowerControl(0x08);
while (1) {
wait(0.1);
accelerometer.getOutput(readings);
//13-bit, sign extended values.
pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)rea
dings[2]);
}
}

You might also like