Recent changes
Slingshot user guide
tag Guide, user
NFCLamp user guide
tag Guide, user
Homepage
MPL115A2
Compiler Error 42
From the mbed microcontroller Cookbook.  

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.

/media/uploads/aberk/adxl345.jpg

Hello World!

» Import this 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 Namembed pin
VccVout
GndGnd
SDAp5
SDOp6
SCLp7
CSp8

API for SPI

» Import this library into a program

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 this library into a programADXL345

ADXL345 triple axis, digital accelerometer library.

API for I2C

» Import this 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 this 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.

Reference




calendar Page history
Last modified 12 May 2011, by   user Peter Swanson   tag No tags | 4 comments  

4 comments on ADXL345 Accelerometer:

09 Nov 2010

hi, I'm pretty new to the mbed and microcontroller interface in general, but have a project requiring the use of the accelerometer, I've typed in the example code you have and built it successfully, but I do not know how the ADXL345 measures and how to see if it is working properly. Ideally, my goal is to be create a situation if the accelerometer is lifted, the accelerometer will notice the change and send an output to a speaker device.

Thanks for any help you can give.

24 Jan 2011

There's a bug in setFifoControl, which means it doesn't actually do anything! Specifically: Function setFifoControl incorrectly attempts to set the value of the ADXL345_FIFO_STATUS register, instead of ADXL345_FIFO_CTL. Since ADXL345_FIFO_STATUS is a read-only register, the only effect is that ADXL345_FIFO_CTL remains at its previous value (which is 0x00 after reset).

Aaron, any chance you can update your library? I have an update, based on the modified one Jose Padron published at

» Import this library into a programADXL345

Add functions to read Ax, Ay and Az individually

, but the Wiki links mostly go to your original, and I don't feel the need to change all of that. Thanks!

29 Jun 2011

Hello I used latest Owen library and the Hello world above, but still i get only '0' even if i move the sparkfun adxl board in all directions! am i doing anything wrong to test this accelerometer

17 Jan 2012

Hi Thanks The program works fine

Please login to post comments.