ADXL345 Accelerometer Range

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Sebastianb28
 
Posts: 4
Joined: Tue Apr 28, 2020 12:44 pm

ADXL345 Accelerometer Range

Post by Sebastianb28 »

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

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)

User avatar
siddacious
 
Posts: 407
Joined: Fri Apr 21, 2017 3:09 pm

Re: ADXL345 Accelerometer Range

Post by siddacious »

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

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.

User avatar
siddacious
 
Posts: 407
Joined: Fri Apr 21, 2017 3:09 pm

Re: ADXL345 Accelerometer Range

Post by siddacious »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”