Skip to content
This repository was archived by the owner on Dec 21, 2018. It is now read-only.

Commit 9f42fe8

Browse files
committed
Initial commit.
1 parent 4121317 commit 9f42fe8

File tree

5 files changed

+511
-0
lines changed

5 files changed

+511
-0
lines changed

Adafruit_ADXL345/ADXL345.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import struct
2+
3+
# Minimal constants carried over from Arduino library
4+
ADXL345_ADDRESS = 0x53
5+
ADXL345_REG_DEVID = 0x00 # Device ID
6+
ADXL345_REG_DATAX0 = 0x32 # X-axis data 0 (6 bytes for X/Y/Z)
7+
ADXL345_REG_POWER_CTL = 0x2D # Power-saving features control
8+
ADXL345_REG_DATA_FORMAT = 0x31
9+
ADXL345_REG_BW_RATE = 0x2C
10+
ADXL345_DATARATE_0_10_HZ = 0x00
11+
ADXL345_DATARATE_0_20_HZ = 0x01
12+
ADXL345_DATARATE_0_39_HZ = 0x02
13+
ADXL345_DATARATE_0_78_HZ = 0x03
14+
ADXL345_DATARATE_1_56_HZ = 0x04
15+
ADXL345_DATARATE_3_13_HZ = 0x05
16+
ADXL345_DATARATE_6_25HZ = 0x06
17+
ADXL345_DATARATE_12_5_HZ = 0x07
18+
ADXL345_DATARATE_25_HZ = 0x08
19+
ADXL345_DATARATE_50_HZ = 0x09
20+
ADXL345_DATARATE_100_HZ = 0x0A # (default)
21+
ADXL345_DATARATE_200_HZ = 0x0B
22+
ADXL345_DATARATE_400_HZ = 0x0C
23+
ADXL345_DATARATE_800_HZ = 0x0D
24+
ADXL345_DATARATE_1600_HZ = 0x0E
25+
ADXL345_DATARATE_3200_HZ = 0x0F
26+
ADXL345_RANGE_2_G = 0x00 # +/- 2g (default)
27+
ADXL345_RANGE_4_G = 0x01 # +/- 4g
28+
ADXL345_RANGE_8_G = 0x02 # +/- 8g
29+
ADXL345_RANGE_16_G = 0x03 # +/- 16g
30+
31+
32+
class ADXL345(object):
33+
"""ADXL345 triple-axis accelerometer."""
34+
35+
def __init__(self, address=ADXL345_ADDRESS, i2c=None, **kwargs):
36+
"""Initialize the ADXL345 accelerometer using its I2C interface.
37+
"""
38+
# Setup I2C interface for the device.
39+
if i2c is None:
40+
import Adafruit_GPIO.I2C as I2C
41+
i2c = I2C
42+
self._device = i2c.get_i2c_device(address, **kwargs)
43+
# Check that the acclerometer is connected, then enable it.
44+
if self._device.readU8(ADXL345_REG_DEVID) == 0xE5:
45+
self._device.write8(ADXL345_REG_POWER_CTL, 0x08)
46+
else:
47+
raise RuntimeError('Failed to find the expected device ID register value, check your wiring.')
48+
49+
def set_range(self, value):
50+
"""Set the range of the accelerometer to the provided value. Range value
51+
should be one of these constants:
52+
- ADXL345_RANGE_2_G = +/-2G
53+
- ADXL345_RANGE_4_G = +/-4G
54+
- ADXL345_RANGE_8_G = +/-8G
55+
- ADXL345_RANGE_16_G = +/-16G
56+
"""
57+
# Read the data format register to preserve bits. Update the data
58+
# rate, make sure that the FULL-RES bit is enabled for range scaling
59+
format_reg = self._device.readU8(ADXL345_REG_DATA_FORMAT) & ~0x0F
60+
format_reg |= value
61+
format_reg |= 0x08 # FULL-RES bit enabled
62+
# Write the updated format register.
63+
self._device.write8(ADXL345_REG_DATA_FORMAT, format_reg)
64+
65+
def get_range(self):
66+
"""Retrieve the current range of the accelerometer. See set_range for
67+
the possible range constant values that will be returned.
68+
"""
69+
return self._device.readU8(ADXL345_REG_DATA_FORMAT) & 0x03
70+
71+
def set_data_rate(self, rate):
72+
"""Set the data rate of the aceelerometer. Rate should be one of the
73+
following constants:
74+
- ADXL345_DATARATE_0_10_HZ = 0.1 hz
75+
- ADXL345_DATARATE_0_20_HZ = 0.2 hz
76+
- ADXL345_DATARATE_0_39_HZ = 0.39 hz
77+
- ADXL345_DATARATE_0_78_HZ = 0.78 hz
78+
- ADXL345_DATARATE_1_56_HZ = 1.56 hz
79+
- ADXL345_DATARATE_3_13_HZ = 3.13 hz
80+
- ADXL345_DATARATE_6_25HZ = 6.25 hz
81+
- ADXL345_DATARATE_12_5_HZ = 12.5 hz
82+
- ADXL345_DATARATE_25_HZ = 25 hz
83+
- ADXL345_DATARATE_50_HZ = 50 hz
84+
- ADXL345_DATARATE_100_HZ = 100 hz
85+
- ADXL345_DATARATE_200_HZ = 200 hz
86+
- ADXL345_DATARATE_400_HZ = 400 hz
87+
- ADXL345_DATARATE_800_HZ = 800 hz
88+
- ADXL345_DATARATE_1600_HZ = 1600 hz
89+
- ADXL345_DATARATE_3200_HZ = 3200 hz
90+
"""
91+
# Note: The LOW_POWER bits are currently ignored,
92+
# we always keep the device in 'normal' mode
93+
self._device.write8(ADXL345_REG_BW_RATE, rate & 0x0F)
94+
95+
def get_data_rate(self):
96+
"""Retrieve the current data rate. See set_data_rate for the possible
97+
data rate constant values that will be returned.
98+
"""
99+
return self._device.readU8(ADXL345_REG_BW_RATE) & 0x0F
100+
101+
def read(self):
102+
"""Read the current value of the accelerometer and return it as a tuple
103+
of signed 16-bit X, Y, Z axis values.
104+
"""
105+
raw = self._device.readList(ADXL345_REG_DATAX0, 6)
106+
return struct.unpack('<hhh', raw)

Adafruit_ADXL345/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .ADXL345 import *

examples/simpletest.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Simple demo of of the ADXL345 accelerometer library. Will print the X, Y, Z
2+
# axis acceleration values every half second.
3+
# Author: Tony DiCola
4+
# License: Public Domain
5+
import time
6+
7+
# Import the ADXL345 module.
8+
import Adafruit_ADXL345
9+
10+
11+
# Create an ADXL345 instance.
12+
accel = Adafruit_ADXL345.ADXL345()
13+
14+
# You can optionally change the range to one of:
15+
# - ADXL345_RANGE_2_G = +/-2G (default)
16+
# - ADXL345_RANGE_4_G = +/-4G
17+
# - ADXL345_RANGE_8_G = +/-8G
18+
# - ADXL345_RANGE_16_G = +/-16G
19+
# For example to set to +/- 16G:
20+
#accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_16_G)
21+
22+
# Or change the data rate to one of:
23+
# - ADXL345_DATARATE_0_10_HZ = 0.1 hz
24+
# - ADXL345_DATARATE_0_20_HZ = 0.2 hz
25+
# - ADXL345_DATARATE_0_39_HZ = 0.39 hz
26+
# - ADXL345_DATARATE_0_78_HZ = 0.78 hz
27+
# - ADXL345_DATARATE_1_56_HZ = 1.56 hz
28+
# - ADXL345_DATARATE_3_13_HZ = 3.13 hz
29+
# - ADXL345_DATARATE_6_25HZ = 6.25 hz
30+
# - ADXL345_DATARATE_12_5_HZ = 12.5 hz
31+
# - ADXL345_DATARATE_25_HZ = 25 hz
32+
# - ADXL345_DATARATE_50_HZ = 50 hz
33+
# - ADXL345_DATARATE_100_HZ = 100 hz (default)
34+
# - ADXL345_DATARATE_200_HZ = 200 hz
35+
# - ADXL345_DATARATE_400_HZ = 400 hz
36+
# - ADXL345_DATARATE_800_HZ = 800 hz
37+
# - ADXL345_DATARATE_1600_HZ = 1600 hz
38+
# - ADXL345_DATARATE_3200_HZ = 3200 hz
39+
# For example to set to 6.25 hz:
40+
#accel.set_data_rate(Adafruit_ADXL345.ADXL345_DATARATE_6_25HZ)
41+
42+
print('Printing X, Y, Z axis values, press Ctrl-C to quit...')
43+
while True:
44+
# Read the X, Y, Z axis acceleration values and print them.
45+
x, y, z = accel.read()
46+
print('X={0}, Y={1}, Z={2}'.format(x, y, z))
47+
# Wait half a second and repeat.
48+
time.sleep(0.5)

0 commit comments

Comments
 (0)