ADXL345 Accelerometer
Note
This page has been moved to the Components section of the website. Please see http://developer.mbed.org/components/ADXL345-Accelerometer/ .
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.
Hello World!¶
Import program
00001 #include "ADXL345.h" 00002 00003 ADXL345 accelerometer(p5, p6, p7, p8); 00004 Serial pc(USBTX, USBRX); 00005 00006 int main() { 00007 00008 int readings[3] = {0, 0, 0}; 00009 00010 pc.printf("Starting ADXL345 test...\n"); 00011 pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); 00012 00013 //Go into standby mode to configure the device. 00014 accelerometer.setPowerControl(0x00); 00015 00016 //Full resolution, +/-16g, 4mg/LSB. 00017 accelerometer.setDataFormatControl(0x0B); 00018 00019 //3.2kHz data rate. 00020 accelerometer.setDataRate(ADXL345_3200HZ); 00021 00022 //Measurement mode. 00023 accelerometer.setPowerControl(0x08); 00024 00025 while (1) { 00026 00027 wait(0.1); 00028 00029 accelerometer.getOutput(readings); 00030 00031 //13-bit, sign extended values. 00032 pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); 00033 00034 } 00035 00036 }
ADXL345 Signal Name | mbed pin |
---|---|
Vcc | Vout |
Gnd | Gnd |
SDA | p5 |
SDO | p6 |
SCL | p7 |
CS | p8 |
API for SPI¶
Import library
Public Member Functions |
|
ADXL345 (PinName mosi, PinName miso, PinName sck, PinName cs) | |
Constructor.
|
|
int | getDevId (void) |
Read the device ID register on the device.
|
|
int | getTapThreshold (void) |
Read the tap threshold on the device.
|
|
void | setTapThreshold (int threshold) |
Set the tap threshold.
|
|
int | getOffset (int axis) |
Get the current offset for a particular axis.
|
|
void | setOffset (int axis, char offset) |
Set the offset for a particular axis.
|
|
int | getTapDuration (void) |
Get the tap duration required to trigger an event.
|
|
void | setTapDuration (int duration_us) |
Set the tap duration required to trigger an event.
|
|
float | getTapLatency (void) |
Get the tap latency between the detection of a tap and the time window.
|
|
void | setTapLatency (int latency_ms) |
Set the tap latency between the detection of a tap and the time window.
|
|
float | getWindowTime (void) |
Get the time of window between tap latency and a double tap.
|
|
void | setWindowTime (int window_ms) |
Set the time of the window between tap latency and a double tap.
|
|
int | getActivityThreshold (void) |
Get the threshold value for detecting activity.
|
|
void | setActivityThreshold (int threshold) |
Set the threshold value for detecting activity.
|
|
int | getInactivityThreshold (void) |
Get the threshold value for detecting inactivity.
|
|
void | setInactivityThreshold (int threshold) |
Set the threshold value for detecting inactivity.
|
|
int | getTimeInactivity (void) |
Get the time required for inactivity to be declared.
|
|
void | setTimeInactivity (int timeInactivity) |
Set the time required for inactivity to be declared.
|
|
int | getActivityInactivityControl (void) |
Get the activity/inactivity control settings.
|
|
void | setActivityInactivityControl (int settings) |
Set the activity/inactivity control settings.
|
|
int | getFreefallThreshold (void) |
Get the threshold for free fall detection.
|
|
void | setFreefallThreshold (int threshold) |
Set the threshold for free fall detection.
|
|
int | getFreefallTime (void) |
Get the time required to generate a free fall interrupt.
|
|
void | setFreefallTime (int freefallTime_ms) |
Set the time required to generate a free fall interrupt.
|
|
int | getTapAxisControl (void) |
Get the axis tap settings.
|
|
void | setTapAxisControl (int settings) |
Set the axis tap settings.
|
|
int | getTapSource (void) |
Get the source of a tap.
|
|
void | setPowerMode (char mode) |
Set the power mode.
|
|
void | setDataRate (int rate) |
Set the data rate.
|
|
int | getPowerControl (void) |
Get the power control settings.
|
|
void | setPowerControl (int settings) |
Set the power control settings.
|
|
int | getInterruptEnableControl (void) |
Get the interrupt enable settings.
|
|
void | setInterruptEnableControl (int settings) |
Set the interrupt enable settings.
|
|
int | getInterruptMappingControl (void) |
Get the interrupt mapping settings.
|
|
void | setInterruptMappingControl (int settings) |
Set the interrupt mapping settings.
|
|
int | getInterruptSource (void) |
Get the interrupt source.
|
|
int | getDataFormatControl (void) |
Get the data format settings.
|
|
void | setDataFormatControl (int settings) |
Set the data format settings.
|
|
void | getOutput (int *readings) |
Get the output of all three axes.
|
|
int | getFifoControl (void) |
Get the FIFO control settings.
|
|
void | setFifoControl (int settings) |
Set the FIFO control settings.
|
|
int | getFifoStatus (void) |
Get FIFO status.
|
Library for SPI¶
Import libraryADXL345
ADXL345 triple axis, digital accelerometer library.
API for I2C¶
Import programADXL345_I2C
This library enables users to communicate with the ADXL345 accelerometer through the I2C bus on the mbed. The API names are similar and work nearly the same way as those made in the SPI libraries for the ADXL345.
Library for I2C¶
Import programADXL345_I2C
This library enables users to communicate with the ADXL345 accelerometer through the I2C bus on the mbed. The API names are similar and work nearly the same way as those made in the SPI libraries for the ADXL345.