Skip to content

Commit 33228e3

Browse files
authored
Merge pull request #34 from kattni/offset
Add offset and offset calibration example.
2 parents ebc1cd2 + 850be54 commit 33228e3

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

adafruit_adxl34x.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@
2929
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
3030
"""
3131
from struct import unpack
32-
3332
from micropython import const
3433
from adafruit_bus_device import i2c_device
3534

3635
try:
3736
from typing import Tuple, Dict
3837

3938
# This is only needed for typing
40-
import busio # pylint: disable=unused-import
39+
import busio
4140
except ImportError:
4241
pass
4342

@@ -110,12 +109,12 @@ class DataRate: # pylint: disable=too-few-public-methods
110109
"""
111110

112111
RATE_3200_HZ: int = const(0b1111) # 1600Hz Bandwidth 140mA IDD
113-
RATE_1600_HZ: int = const(0b1110) # 800Hz Bandwidth 90mA IDD
114-
RATE_800_HZ: int = const(0b1101) # 400Hz Bandwidth 140mA IDD
115-
RATE_400_HZ: int = const(0b1100) # 200Hz Bandwidth 140mA IDD
116-
RATE_200_HZ: int = const(0b1011) # 100Hz Bandwidth 140mA IDD
117-
RATE_100_HZ: int = const(0b1010) # 50Hz Bandwidth 140mA IDD
118-
RATE_50_HZ: int = const(0b1001) # 25Hz Bandwidth 90mA IDD
112+
RATE_1600_HZ: int = const(0b1110) # 800Hz Bandwidth 90mA IDD
113+
RATE_800_HZ: int = const(0b1101) # 400Hz Bandwidth 140mA IDD
114+
RATE_400_HZ: int = const(0b1100) # 200Hz Bandwidth 140mA IDD
115+
RATE_200_HZ: int = const(0b1011) # 100Hz Bandwidth 140mA IDD
116+
RATE_100_HZ: int = const(0b1010) # 50Hz Bandwidth 140mA IDD
117+
RATE_50_HZ: int = const(0b1001) # 25Hz Bandwidth 90mA IDD
119118
RATE_25_HZ: int = const(0b1000) # 12.5Hz Bandwidth 60mA IDD
120119
RATE_12_5_HZ: int = const(0b0111) # 6.25Hz Bandwidth 50mA IDD
121120
RATE_6_25HZ: int = const(0b0110) # 3.13Hz Bandwidth 45mA IDD
@@ -195,7 +194,22 @@ def acceleration(self) -> Tuple[int, int, int]:
195194
x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
196195
y = y * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
197196
z = z * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
198-
return (x, y, z)
197+
return x, y, z
198+
199+
@property
200+
def raw_x(self) -> int:
201+
"""The raw x value."""
202+
return unpack("<h", self._read_register(_REG_DATAX0, 2))[0]
203+
204+
@property
205+
def raw_y(self) -> int:
206+
"""The raw y value."""
207+
return unpack("<h", self._read_register(_REG_DATAY0, 2))[0]
208+
209+
@property
210+
def raw_z(self) -> int:
211+
"""The raw z value."""
212+
return unpack("<h", self._read_register(_REG_DATAZ0, 2))[0]
199213

200214
@property
201215
def events(self) -> Dict[str, bool]:
@@ -312,7 +326,7 @@ def enable_freefall_detection(self, *, threshold: int = 10, time: int = 25) -> N
312326
self._enabled_interrupts["freefall"] = True
313327

314328
def disable_freefall_detection(self) -> None:
315-
"Disable freefall detection"
329+
"""Disable freefall detection"""
316330
active_interrupts = self._read_register_unpacked(_REG_INT_ENABLE)
317331
active_interrupts &= ~_INT_FREE_FALL
318332
self._write_register_byte(_REG_INT_ENABLE, active_interrupts)
@@ -379,7 +393,7 @@ def enable_tap_detection(
379393
)
380394

381395
def disable_tap_detection(self) -> None:
382-
"Disable tap detection"
396+
"""Disable tap detection"""
383397
active_interrupts = self._read_register_unpacked(_REG_INT_ENABLE)
384398
active_interrupts &= ~_INT_SINGLE_TAP
385399
active_interrupts &= ~_INT_DOUBLE_TAP
@@ -417,6 +431,23 @@ def range(self, val: int) -> None:
417431
# write the updated values
418432
self._write_register_byte(_REG_DATA_FORMAT, format_register)
419433

434+
@property
435+
def offset(self) -> Tuple[int, int, int]:
436+
"""
437+
The x, y, z offsets as a tuple of raw count values.
438+
439+
See offset_calibration example for usage.
440+
"""
441+
x_offset, y_offset, z_offset = unpack("<bbb", self._read_register(_REG_OFSX, 3))
442+
return x_offset, y_offset, z_offset
443+
444+
@offset.setter
445+
def offset(self, val: Tuple[int, int, int]) -> None:
446+
x_offset, y_offset, z_offset = val
447+
self._write_register_byte(_REG_OFSX, x_offset)
448+
self._write_register_byte(_REG_OFSY, y_offset)
449+
self._write_register_byte(_REG_OFSZ, z_offset)
450+
420451
def _read_clear_interrupt_source(self) -> int:
421452
return self._read_register_unpacked(_REG_INT_SOURCE)
422453

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import adafruit_adxl34x
7+
8+
i2c = board.STEMMA_I2C() # uses board.SCL and board.SDA
9+
10+
# For ADXL343
11+
accelerometer = adafruit_adxl34x.ADXL343(i2c)
12+
# For ADXL345
13+
# accelerometer = adafruit_adxl34x.ADXL345(i2c)
14+
15+
accelerometer.offset = 0, 0, 0
16+
17+
print("Hold accelerometer flat to set offsets to 0, 0, and -1g...")
18+
time.sleep(1)
19+
x = accelerometer.raw_x
20+
y = accelerometer.raw_y
21+
z = accelerometer.raw_z
22+
print("Raw x: ", x)
23+
print("Raw y: ", y)
24+
print("Raw z: ", z)
25+
26+
accelerometer.offset = (
27+
round(-x / 8),
28+
round(-y / 8),
29+
round(-(z - 250) / 8), # Z should be '250' at 1g (4mg per bit)
30+
)
31+
print("Calibrated offsets: ", accelerometer.offset)
32+
33+
while True:
34+
print("%f %f %f m/s^2" % accelerometer.acceleration)
35+
time.sleep(0.2)

0 commit comments

Comments
 (0)