The mail man delivered some pin headers (2€ for 5 x 40 pins on Ebay) last week and I found some time to solder them to my ADXL345 board. So it was time to start playing with the ADXL345 accelerometer. I had a look around the various tutorials on the internet and found lots of different diagrams for connecting it to an Arduino. So I consulted the data sheet, and read that the ADXL345 can be used on the SPI or the I2C bus. I will be using it on the I2C bus as the Arduino library for the ADXL345 is written for I2C and it’s the easiest way to connect it. My ADXL345 breakout board has 8 pins: GND, VDD, SCL, SDA, SDO, INT2, INT1 and CS. The ADXL is not 5V tolerant so do not connect the VDD pin with the 5V but with the 3,3V pin on your Arduino. To use the I2C bus the datasheet states that pin CS should be high, so connect that also to 3,3V. Since the I2C bus on the Arduino is 5V we need to use pullup resistors to 3,3V. I used 2 4k7 resistors between SDA and SCL and 3,3V. Something else I found in the datasheet is that you can select the I2C address by pulling the SDO pin high or low. So you can choose between 2 addresses. This is handy if you have other devices on the I2C bus. The library has 2 examples, I compiled both and let them run. After some fiddling I managed to get the ADXLRun345 example sketch running. I could see the x, y and z values change on the serial monitor while moving the board around, it works 🙂 I needed to add “Accel.powerOn();” in void setup, I assume my ADXL345 doesn’t “start” automatically when it receives power. After taking a good look at these example sketches I’ve come to realise that I still have a lot to learn. I don’t understand much of them. The ADXL345 is a pretty advanced sensor with lots of features. I think it’s going to take a long time before I can do something useful with it.
/************************************************************************** * * * ADXL345 Driver for Arduino * * * *************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the MIT License. * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MIT License for more details. * * * *************************************************************************** * * Revision History * * Date By What * 20100515 TJS Initial Creation * 20100524 TJS Modified to run with Kevin Stevenard's driver */ #include "Wire.h" #include "ADXL345.h" ADXL345 Accel; void setup(){ Serial.begin(57600); delay(1); Wire.begin(); delay(1); Serial.println("Here"); Accel.powerOn(); Accel.set_bw(ADXL345_BW_12); Serial.print("BW_OK? "); Serial.println(Accel.status, DEC); delay(1000); } void loop(){ int i; double acc_data[3]; Accel.get_Gxyz(acc_data); if(Accel.status){ float length = 0.; for(i = 0; i < 3; i++){ length += (float)acc_data[i] * (float)acc_data[i]; Serial.print(acc_data[i]); Serial.print(" "); } length = sqrt(length); Serial.print(length); Serial.println(""); delay(40); } else{ Serial.println("ERROR: ADXL345 data read error"); } }
Some interesting links I found while searching for info on the ADXL345:
- http://www.starlino.com/imu_guide.html
- http://www.varesano.net/blog/fabio/new-homebrew-diy-breakout-board-pcb-adxl345-accelerometer
- http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html
Can you post the link to the ADXL345 library that you used for this? I have one but it does not contain the procedures of your code. Thanks!
Link is in the blog post: https://code.google.com/p/adxl345driver/