-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Default is 2G scale, with 100Hz output data rate, and full resolution mode
from ADXL345 import *
myAccelerometer = ADXL345() # use defaults
It is import to understand that the sensitivity of your accelerometer might not be the typical value which is why it can be specified at initialization.
You can find the sensitivity by doing a stationary test and looking at the Z-axis value.
Below is an example output from example_poll.py
XYZ: [0.42084900000000003, 0.0, 9.143901]
XYZ: [0.38259, 0.0, 9.143901]
XYZ: [0.42084900000000003, 0.0, 9.143901]
XYZ: [0.42084900000000003, 0.0, 9.18216]
As you can see, the Z-axis value is not quite what one would expect (~9.81). This is because my ADXL345 doesn't have the typical 3.9mg/LSB sensitivity. The value is less than one would expect, which means the sensitivity needs to be higher. If it were more than one would expect, then it means the sensitivity needs to be lower. The min, typical, and max sensitivities are provided in ADXL345.py
ADXL345_ACC_SENSITIVITY_2G_MIN = 3.5 * 1e-3 * 9.81 # mg/LSB
ADXL345_ACC_SENSITIVITY_2G_TYP = 3.9 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_2G_MAX = 4.3 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_4G_MIN = 7.1 * 1e-3 * 9.81 # mg/LSB
ADXL345_ACC_SENSITIVITY_4G_TYP = 7.8 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_4G_MAX = 8.7 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_8G_MIN = 14.1 * 1e-3 * 9.81 # mg/LSB
ADXL345_ACC_SENSITIVITY_8G_TYP = 15.6 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_8G_MAX = 17.5 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_16G_MIN = 28.6 * 1e-3 * 9.81 # mg/LSB
ADXL345_ACC_SENSITIVITY_16G_TYP = 31.2 * 1e-3 * 9.81
ADXL345_ACC_SENSITIVITY_16G_MAX = 34.5 * 1e-3 * 9.81
Let's try the max sensitivity and see if it outputs the expected value. Note that full resolution must be declared false.
myAccelerometer = ADXL345(sensitivity=ADXL345_ACC_SENSITIVITY_2G_MAX, full_resolution=False)
The output now looks like...
XYZ: [0.46401300000000006, 0.0, 10.039554]
XYZ: [0.5061960000000001, 0.0, 9.997371000000001]
XYZ: [0.46401300000000006, -0.042183000000000005, 10.081737]
XYZ: [0.5061960000000001, 0.0, 10.081737]
Now the value is higher than expected. Play around with it a bit. This is an imperfect process but you should be able to find a sensitivity that provides an acceptable accuracy for your needs.
For my ADXL345, I found the sensitivity was about 4.186. When I set it to that, the output was ~9.81
XYZ: [0.45171125999999995, 0.0, 9.81445374]
XYZ: [0.45171125999999995, 0.0, 9.81445374]
Resolution is configured at initialization. Fixed resolution is 10-bits. Full resolution up to 13-bit resolution at +/-16 g with 4 mg/LSB scale factor in all g ranges. Full resolution is the default selection.
myAccelerometer = ADXL345(full_resolution=False) # use fixed 10-bit resolution
Supports 2g, 4g, 8g, 16g.
Use the constants defined in ADXL345.py
ADXL345_ACC_SCALE_2G = 0
ADXL345_ACC_SCALE_4G = 1
ADXL345_ACC_SCALE_8G = 2
ADXL345_ACC_SCALE_16G = 3
Note: Sensitivity varies based on the range when not in full resolution mode so be sure to use the appropriate value based on the desired range.
4g range example
myAccelerometer = ADXL345(sensitivity=ADXL345_ACC_SENSITIVITY_4G_TYP, scale=ADXL345_ACC_SCALE_4G, full_resolution=False)
You can get data from 1 or all axes.
X-axis example
>>> myAccelerometer.getX()
0.459108
All axes example
>>> myAccelerometer.getXYZ()
(0.42084900000000003, 0.0, 9.18216) # (X, Y, Z)
All axes raw data example
>>> myAccelerometer.getXYZ(raw=True)
(11, 0, 237)
Example calculation of measurement
>>> c_int16(237).value*ADXL345_ACC_SENSITIVITY_2G_TYP
9.067383
I plan to keep developing the ADXL345 in my free time but you don't need to wait for me. You can use the ADXL345 module to read and write registers yourself which allows you the ability to easily develop the additional things that you need for your project. WARNING: Be sure to read the datasheet to ensure that you know what you are doing when using this feature.
Grab the register map like so
>>> from ADXL345 import *
>>> myAccelerometer = ADXL345() # using defaults
>>> myAccelerometer.regs
{'DATAZ0': 54, 'DATAZ1': 55, 'Latent': 34, 'DATAX0': 50, 'DATAX1': 51, 'Window': 35, 'ACT_INACT_CTL': 39, 'INT_ENABLE': 46, 'TIME_INACT': 38, 'TAP_AXES': 42, 'OFSZ': 32, 'OFSX': 30, 'OFSY': 31, 'THRESH_FF': 40, 'DATAY1': 53, 'DATAY0': 52, 'DEVID': 0, 'THRESH_INACT': 37, 'THRESH_ACT': 36, 'DUR': 33, 'INT_MAP': 47, 'FIFO_STATUS': 57, 'POWER_CTL': 45, 'INT_SOURCE': 48, 'DATA_FORMAT': 49, 'ACT_TAP_STATUS': 43, 'FIFO_CTL': 56, 'TIME_FF': 41, 'THRESH_TAP': 29, 'BW_RATE': 44}
Example of reading the BW_RATE
register
>>> myAccelerometer.readRegister(myAccelerometer.regs['BW_RATE'], 1)
[10]
Example of writing the BW_RATE
register then reading it back
>>> myAccelerometer.writeRegister(myAccelerometer.regs['BW_RATE'], [11])
>>> myAccelerometer.readRegister(myAccelerometer.regs['BW_RATE'], 1)
[11]