ADXL345

ADXL345

This page is to Setup and Config ADXL345

Hardware and Software

Hardware

ADXL345, Arduino or Digispark / ESP8266

Software

Arduino IDE

Sanki Notes

    • I2C :

    • SDA : A4 (Digispark D0)

    • SCL : A5 (Digispark D2)

    • SDO : GND

    • CS : V++

    • Example : http://www.dfrobot.com/wiki/index.php?title=Triple_Axis_Accelerometer_Breakout_-_ADXL345_(SKU:SEN0032)

Examples

This example is ..........

            1. #include <Wire.h>

            2. #define DEVICE (0x53) //ADXL345 device address

            3. #define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)

            4. byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device

            5. char str[512]; //string buffer to transform data before sending it to the serial port

            6. int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345

            7. int x, y, z; //three axis acceleration data

            8. double roll = 0.00, pitch = 0.00; //Roll & Pitch are the angles which rotate by the axis X and y

            9. //in the sequence of R(x-y-z),more info visit

            10. // https://www.dfrobot.com/wiki/index.php?title=How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing#Introduction

            11. void setup() {

            12. Wire.begin(); // join i2c bus (address optional for master)

            13. Serial.begin(9600); // start serial for output

            14. //Turning on the ADXL345

            15. writeTo(DEVICE, 0x2D, 0);

            16. writeTo(DEVICE, 0x2D, 16);

            17. writeTo(DEVICE, 0x2D, 8);

            18. }

            19. void loop() {

            20. readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345

            21. //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!

            22. //thus we are converting both bytes in to one int

            23. x = (((int)buff[1]) << 8) | buff[0];

            24. y = (((int)buff[3])<< 8) | buff[2];

            25. z = (((int)buff[5]) << 8) | buff[4];

            26. //we send the x y z values as a string to the serial port

            27. Serial.print("The acceleration info of x, y, z are:");

            28. sprintf(str, "%d %d %d", x, y, z);

            29. Serial.print(str);

            30. Serial.write(10);

            31. //Roll & Pitch calculate

            32. RP_calculate();

            33. Serial.print("Roll:"); Serial.println( roll );

            34. Serial.print("Pitch:"); Serial.println( pitch );

            35. Serial.println("");

            36. //It appears that delay is needed in order not to clog the port

            37. delay(500);

            38. }

            39. //---------------- Functions

            40. //Writes val to address register on device

            41. void writeTo(int device, byte address, byte val) {

            42. Wire.beginTransmission(device); //start transmission to device

            43. Wire.write(address); // send register address

            44. Wire.write(val); // send value to write

            45. Wire.endTransmission(); //end transmission

            46. }

            47. //reads num bytes starting from address register on device in to buff array

            48. void readFrom(int device, byte address, int num, byte buff[]) {

            49. Wire.beginTransmission(device); //start transmission to device

            50. Wire.write(address); //sends address to read from

            51. Wire.endTransmission(); //end transmission

            52. Wire.beginTransmission(device); //start transmission to device

            53. Wire.requestFrom(device, num); // request 6 bytes from device

            54. int i = 0;

            55. while(Wire.available()) //device may send less than requested (abnormal)

            56. {

            57. buff[i] = Wire.read(); // receive a byte

            58. i++;

            59. }

            60. Wire.endTransmission(); //end transmission

            61. }

            62. //calculate the Roll&Pitch

            63. void RP_calculate(){

            64. double x_Buff = float(x);

            65. double y_Buff = float(y);

            66. double z_Buff = float(z);

            67. roll = atan2(y_Buff , z_Buff) * 57.3;

            68. pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;

            69. }