33
1. Import the I2C and accelerometer libraries and create an instance of ADXL345 accelerometer.
2. Initialize serial monitor and ADXL345 accelerometer.
3. Read the accelerometer values of three axis and store them in variables.
4. Print the accelerometer values of three axis.
5. Get the accelerometer values of three axis in G units and print.
Code overview
Code usage
Import the library of ADXL345 accelerometer. The calculation of acceleration is relatively complicated.
Fortunately, we can use the functions in this library to simplify the difficulty of using the accelerometer.
The I2C address of the accelerometer has been defined in the library file, so we will not declare the
I2C address of the accelerometer again in the program.
ADXL345 library: #include <ADXL345.h>
Sets the byte which holds the threshold value for detecting activity/inactivity. The data format is
unsigned, so the magnitude of the activity/inactivity event is compared with the value is compared with
the value in the THRESH_ACT/THRESH_INACT register. The scale factor is 62.5mg/LSB. A value of
0 may result in undesirable behavior if the activity/inactivity interrupt is enabled. The maximum value
is 255.
Activity and inactivity threshold: adxl.setActivityThreshold(75);
adxl.setInactivityThreshold(75);
The function of reading the acceleration data from the ADXL345. We need to pass the address of
three axis to this function. Each axis reading comes in 10-bit resolution, ie 2 bytes. Least Significant
Byte first!! Thus we are converting both bytes into one int.
Read acceleration data: adxl.readXYZ(&x, &y, &z);
Read the acceleration data in G unit. The parameters filled in the brackets are the arrays in which we
need to store the accelerometers of the three axes read. The first, second and third elements of the
array store the x-axis, y-axis and z Acceleration data.
Read acceleration in G unit: adxl.getAcceleration(xyz);
Summary of Contents for Crowtail Deluxe Kit
Page 47: ......