Home CodeMBed MBed LPC1768 and ADXL345 GY-291 example

MBed LPC1768 and ADXL345 GY-291 example

by shedboy71
The ADXL345 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16g. 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.

The ADXL345 is well suited for mobile device applications. It measures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (4 mg/LSB) enables measurement of inclination changes less than 1.0°.

Several special sensing functions are provided. Activity and inactivity sensing detect the presence or lack of motion and if the acceleration on any axis exceeds a user-set level. Tap sensing detects single and double taps. Free-fall sensing detects if the device is falling. These functions can be mapped to one of two interrupt output pins. An integrated, patent pending 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor intervention.

adxl345-module

Connection

I used the following connection from the module above to my MBED 1768

MBED Connection Module Connection
3v3 – VOUT VCC
Gnd Gnd
SDA – Pin 28 SDA
SCL – Pin 27 SCL

 

This is a layout showing the connection

mbed1768-and-adxl345_bb

Code

This needs the following example – https://developer.mbed.org/users/peterswanson87/code/ADXL345_I2C/ – Add this to the MBed compiler

Here is the code

[codesyntax lang=”cpp”]

#include "ADXL345_I2C.h"

 ADXL345_I2C accelerometer(p28, p27);
 Serial pc(USBTX, USBRX);

 int main() {
 pc.baud(115200);
 int readings[3] = {0, 0, 0};
 
 pc.printf("Starting ADXL345 test...\n");
 wait(.001);
 pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
 wait(.001);
 
 // These are here to test whether any of the initialization fails. It will print the failure
 if (accelerometer.setPowerControl(0x00)){
 pc.printf("didn't intitialize power control\n"); 
 return 0; }
 //Full resolution, +/-16g, 4mg/LSB.
 wait(.001);
 
 if(accelerometer.setDataFormatControl(0x0B)){
 pc.printf("didn't set data format\n");
 return 0; }
 wait(.001);
 
 //3.2kHz data rate.
 if(accelerometer.setDataRate(ADXL345_3200HZ)){
 pc.printf("didn't set data rate\n");
 return 0; }
 wait(.001);
 
 //Measurement mode.
 
 if(accelerometer.setPowerControl(MeasurementMode)) {
 pc.printf("didn't set the power control to measurement\n"); 
 return 0; } 
 
 while (1) {
 
 wait(0.1);
 
 accelerometer.getOutput(readings);
 

 pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
 }
 
 }

[/codesyntax]

 

Links
GY-291 ADXL345 3-Axis Digital Gravity Sensor Acceleration Module IIC/SPI transmission

Share

You may also like