5 years, 12 months ago.

Help with using library ADXL345

Hi there! I am quite new to mbed OS, and so far I have only managed to get blinky to work. So far, I am using the offline mbed CLI using GCC as the bootloader (? i think) and the board that I'm using is Nucleo-L476RG. Now, I wanted to try using an ADXL345 accelerometer library. I have managed to add the library using:

mbed add http://os.mbed.com/users/kenjiArai/code/ADXL345/

and it successfully add the library to my program. The program which I was trying was from a "hello world" that I found here. Also, I have managed to set the pin names shown below:

main.cpp

#include "mbed.h"
#include "ADXL345.h"
 
ADXL345 accelerometer(PB_11, PB_10); //p5 = sda; p6 = sdo; p7 = SCL; p8 = CS
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)readings[2]);
 
    }
 
}

However, I have errors after compiling:

Errors:

[mbed] Detected "NUCLEO_L476RG" connected to "F:" and using com port "COM8"
Building project accl-test (NUCLEO_L476RG, GCC_ARM)
Scan: .
Scan: env
Scan: mbed
Compile [  9.4%]: main.cpp
[Warning] MbedCRC.h@34,32: unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]
[Error] main.cpp@12,55: 'class ADXL345' has no member named 'getDevId'
[Error] main.cpp@15,19: 'class ADXL345' has no member named 'setPowerControl'
[Error] main.cpp@18,19: 'class ADXL345' has no member named 'setDataFormatControl'
[Error] main.cpp@21,19: 'class ADXL345' has no member named 'setDataRate'
[Error] main.cpp@21,31: 'ADXL345_3200HZ' was not declared in this scope
[Error] main.cpp@24,19: 'class ADXL345' has no member named 'setPowerControl'
[Error] main.cpp@30,23: 'class ADXL345' has no member named 'getOutput'
[ERROR] In file included from ./mbed-os/mbed.h:90:0,
                 from .\main.cpp:1:
./mbed-os/drivers/MbedCRC.h:34:32: warning: unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]
 #pragma GCC diagnostic ignored "-Wshift-count-negative"
                                ^
.\main.cpp: In function 'int main()':
.\main.cpp:12:55: error: 'class ADXL345' has no member named 'getDevId'
     pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
                                                       ^
.\main.cpp:15:19: error: 'class ADXL345' has no member named 'setPowerControl'
     accelerometer.setPowerControl(0x00);
                   ^
.\main.cpp:18:19: error: 'class ADXL345' has no member named 'setDataFormatControl'
     accelerometer.setDataFormatControl(0x0B);
                   ^
.\main.cpp:21:19: error: 'class ADXL345' has no member named 'setDataRate'
     accelerometer.setDataRate(ADXL345_3200HZ);
                   ^
.\main.cpp:21:31: error: 'ADXL345_3200HZ' was not declared in this scope
     accelerometer.setDataRate(ADXL345_3200HZ);
                               ^
.\main.cpp:24:19: error: 'class ADXL345' has no member named 'setPowerControl'
     accelerometer.setPowerControl(0x08);
                   ^
.\main.cpp:30:23: error: 'class ADXL345' has no member named 'getOutput'
         accelerometer.getOutput(readings);
                       ^

[mbed] ERROR: "C:\Python27\python.exe" returned error code 1.
[mbed] ERROR: Command "C:\Python27\python.exe -u E:\Ontel Firmware\accl-test\mbed-os\tools\make.py -t GCC_ARM -m NUCLEO_L476RG --source . --build .\BUILD\NUCLEO_L476RG\GCC_ARM" in "E:\Ontel Firmware\accl-test"
---

So, my questions are:

1. How can I resolve this problem? 2. secondly, is the naming of the pin correct? at first it showed 4 different names : ADXL345 accelerometer(p5, p6, p7, p8); but as I only need 2 (which is SDA and SCL) I only named 2 of them.

Thank you in advance for your help, and I am sorry if the format of this post is a bit messy. Still learning on both writing a forum post and mbed OS :)

Cheers!

1 Answer

5 years, 11 months ago.

Hi Stanley,

It appears that you might be mixing the example source code for another ADXL library with the one from KenjiArai. Clearly that library doesn't have the methods that you are trying to use so the compile step is failing.

Doing some digging we see another ADXL345 library with those methods defined here: https://os.mbed.com/users/aberk/code/ADXL345/docs/bd8f0f20f433//classADXL345.html

As you pointed out you need a 2-pin version and there is an I2C variant here: https://os.mbed.com/users/peterswanson87/code/ADXL345_I2C/

We had a go at building this and had to upgrade the project to Mbed OS5. Here are the steps we used:

mbed import http://os.mbed.com/users/peterswanson87/code/ADXL345_I2C/
cd ADXL345_I2C/
mbed remove mbed
mbed add mbed-os
mbed compile -m NUCLEO_L476RG -t gcc_arm

This import includes a main.cpp so it's a self-contained example (not just a library you can import). After the import you can then replace it with your main.cpp if you like but effectively the program is the same (the delivered code is a bit more thorough). One difference we noted is that the getDevId() method was renamed to getDeviceID().

Does this help?

-Ralph, Team Mbed

Accepted Answer

Hi Ralph!

thank you so much for your answer, and yes it worked flawlessly!

Thanks again!! :)

posted by Stanley Setiawan 15 May 2018