Hi, I want to use the ADXL345 accelerometer with Bela, but I'm a bit confused about how to make this work with Pure Data and a Linux library. Has anyone done this and can provide a clear step by step on the process?
Thanks!
Bela and ADXL345 ?
- Edited
did you see this previous discussion? https://forum.bela.io/d/1086-issues-connecting-adxl345-for-airharp/2
My recommendations are still the same. For instance, start from this library: https://github.com/cagayakemiracl/adxl345-i2c-linux/
The first step is to create a new C++ Bela project and delete the default render.cpp file. Then copy the .c
and .h
files from the link above onto the project and run it. It should build and then fail at run time because it is attempting to use the I2C0 bus which is not exposed. on the pin headers. Make sure you connect the sensor to the I2C1 pins and then edit the main.cpp file so that the line
adxl345_default_init();
becomes
adxl345_init(I2C_DEVICE_1, ADXL345_ADDR_LOW, ADXL345_DATARATE_12_5_HZ);
. You'll also want to add #include <unistd.h>
at the top of the main.cpp file and add a line usleep(100000)
inside the while(1)
loop to throttle the printing.
If running this program prints meaningful values from the sensor, we can move on to the next step.
Hi thank you! So I did see that but I wasn't sure how to implement the library and the code. I use Pure Data almost exclusively with Bela, so C++ can be daunting (although I have done some).
Just to double check - I'm connecting the accelerometer to the Bela in the same manner as this diagram?: https://cdn.sparkfun.com/assets/learn_tutorials/5/4/8/ADXL345Fix.png
?
- Edited
Well yes except that to a Bela :-) Use the pins highlighted as I2C in the board diagram for your board. You can find that in the IDE or here https://learn.bela.io/pin-diagram/ . Those will correspond to I2C1, and so should work with the above code.
Also when using those pins you shouldn't strictly need those pull-up resistors, as they are already included on the Bela cape.
ok yes, all hooked up, and uploaded the files, but they are adxl345.c and adxl.h .... and then there is another file called main.c (but I didn't upload this to Bela yet...) in that code I downloaded from the link: https://github.com/cagayakemiracl/adxl345-i2c-linux/ So I'm not sure how to move forward ... upload main.c as well?
amd upload main.c as well?
yes
as a matter of fact, some of the instructions I listed above include editing that very file.
perfect! thank you! it works, the terminal is printing meaningful values from the sensor -- ex: x: 65534 y: 65531 z: 28 ... And now how can I use these values with a pure data patch?
I added an example to the Bela code that doe sthis.
Update your board to the latest dev branch following the instructions here. Once that's done, go to the examples and look for PureData/custom-render-ADXL345
- Edited
giuliomoro hmm ok, so the update seems to have gone well but I don't see the example PureData/custom-render-ADXL345 in the examples. I also restarted Bela and my computer to see if that helped and it did not. And - but I'm not sure if this matters - the C++ project code from the beginning doesn't seem to work anymore.
- Edited
it may be that you updated to the master
branch and not dev
. To update to dev
, follow the instructions for "Updating to an experimental release" at the link I sent earlier and select the dev
branch before clilcking download as zip
giuliomoro
Ok, so did the dev branch update instead successfully, found custom-render-ADXL345, and built it. Doesn't seem to have a _main.pd file though? Here's what I got in the terminal:
Build finished
Running project ...
Running Pd 0.51-4
Error file ./main.pd not found. The main.pd file should be your main patch.
Error: unable to initialise audio
Makefile:613: recipe for target 'runide' failed
make: *** [runide] Error 1
Bela stopped
Oh right, it seems that I forgot to add it. You can download it here: https://github.com/BelaPlatform/Bela/blob/dev/examples/PureData/custom-render-ADXL345/_main.pd?raw=1
ok great thank you. So now I don't think I'm getting as meaningful values as I was with the C++ code. And with the pd sample, I'm not getting sound when I move the sensor and the values don't seem correct? Here's the terminal when I move the accel:
print: 0.000671387 0.000671387 0.000671387
sending
print: 4.57764e-05 4.57764e-05 4.57764e-05
sending
print: 0.999756 0.999756 0.999756
sending
print: 0.999969 0.999969 0.999969
sending
print: 0.000106812 0.000106812 0.000106812
sending
print: 1.52588e-05 1.52588e-05 1.52588e-05
And when it's still I sometimes get print: 0 0 0 and print: 0.999985 0.999985 0.999985
It is just sending acceleration and not orientation maybe?
I found another mistake in render.cpp, get the latest version from here and replace the content of the file on your board when you open the example https://raw.githubusercontent.com/BelaPlatform/Bela/dev/examples/PureData/custom-render-ADXL345/render.cpp
This will send the same values as before, just rescaled from the original range of 0 to 65535 to a float range of 0 to 1.
Still no sound, here are some values :
print: 9.15527e-05 0.999878 0.00111389
sending
print: 0 0.999939 0.000518799
sending
print: 0.999985 0.999939 0.000427246
print: 0.000473022 1.52588e-05 0.999832
sending
print: 0.000473022 0 0.999924
sending
print: 0.000473022 1.52588e-05 0.999954
do those value make sense for the orientation your are holding the sensor at? If you multiply them by 65536, they should be the same that you were getting in the earlier C program.
You should definitely get some sound from at least one audio channel, especially when the values are above 0.1. I further amended both the Pd patch and C++ file (still available at the links above) so that it also removes a DC offset, but that should at most cause extra distortion, not just prevent sound from coming out. Anyhow, once you see that reasonable values are being printed, then you should be able to use those values in your program with whatever sound generator you see fit.
my mistake audio is working - cable was bad Thank you!
- Edited
great, there are some questions about bandwidth and sampling rate of that sensor. Currently it's using the library's default value for bandwidth:
ADXL345_DATARATE_12_5_HZ // 6.25Hz Bandwidth 50µA IDD
but there are other values that could be used instead, see: https://github.com/BelaPlatform/Bela/blob/dev/examples/PureData/custom-render-ADXL345/adxl345.h#L14-L29
Additionally, we attempt to read and send this Pd every 90ms: (https://github.com/BelaPlatform/Bela/blob/dev/examples/PureData/custom-render-ADXL345/render.cpp#L76).
One seeking to increase the sampling rate of the sensor should increase the former and decrease the latter.