Black Lives Matter - Action and Equality.
0

ADXL345 Accelerometer Range
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ADXL345 Accelerometer Range

by Sebastianb28 on Sun May 10, 2020 3:44 pm

Hi,
I have been playing around with the ADXL345 accelerometer, but I can't set the sensitivity range and data rate properly, I kepep getting an error saying "accelerometer.setRange(ADXL345_RANGE_16_G) - AttributeError: 'ADXL345' object has no attribute 'setRange'

This is code I'm trying to run:
Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import adafruit_adxl34x

i2c = busio.I2C(board.SCL, board.SDA)
accelerometer = adafruit_adxl34x.ADXL345(i2c)
accelerometer.setRange(ADXL345_RANGE_16_G)
accelerometer.setDataRate(ADXL345_DATARATE_3200_HZ)

Sebastianb28
 
Posts: 4
Joined: Tue Apr 28, 2020 12:44 pm

Re: ADXL345 Accelerometer Range

by siddacious on Mon May 11, 2020 2:17 pm

Hi there,
There aren't any "setRange" or "setDataRate" method for the adxl34x library. You'll want to use the 'range' and 'data_rate' properties.

Additionally, the range and data rate constants are organized in classes that are peers to the ADXL345 class so you'll need to import it directly or reference it through the imported 'adafruit_adxl34x' module. While you're at it you can do the same for the ADXL345 class:

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import adafruit_adxl34x
from adafruit_adxl34x import ADXL345, Range, DataRate

i2c = busio.I2C(board.SCL, board.SDA)
accelerometer = ADXL345(i2c)
accelerometer.range - Range.RANGE_16_G
accelerometer.date_rate = DataRate.RATE_3200_HZ

print(accelerometer.acceleration)


May I ask what examples you based your code on? If there is an issue with the documentation or example code we'd like to know so we can fix it.

siddacious
 
Posts: 246
Joined: Fri Apr 21, 2017 3:09 pm

Re: ADXL345 Accelerometer Range

by siddacious on Mon May 11, 2020 2:20 pm

Additionally, the documentation for the ADXL34x library is available here:

https://circuitpython.readthedocs.io/pr ... t/api.html

For future reference, you can find a link to the the documentation for any CircuitPython library in the guide for the sensor in the Adafruit Learning System. As an example, see the "Python Docs" link on the left side of this page:

https://learn.adafruit.com/adxl345-digi ... elerometer

siddacious
 
Posts: 246
Joined: Fri Apr 21, 2017 3:09 pm

Please be positive and constructive with your questions and comments.