/
LIS3DHTR.py
executable file
·61 lines (50 loc) · 1.74 KB
/
LIS3DHTR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# LIS3DHTR
# This code is designed to work with the LIS3DHTR_I2CS I2C Mini Module available from dcubestore.com
# http://dcubestore.com/product/lis3dhtr-3-axis-accelerometer-digital-output-motion-sensor-i%C2%B2c-mini-module/
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# LIS3DHTR address, 0x18(24)
# Select control register1, 0x20(32)
# 0x27(39) Power ON mode, Data rate selection = 10 Hz
# X, Y, Z-Axis enabled
bus.write_byte_data(0x18, 0x20, 0x27)
# LIS3DHTR address, 0x18(24)
# Select control register4, 0x23(35)
# 0x00(00) Continuous update, Full-scale selection = +/-2G
bus.write_byte_data(0x18, 0x23, 0x00)
time.sleep(0.5)
# LIS3DHTR address, 0x18(24)
# Read data back from 0x28(40), 2 bytes
# X-Axis LSB, X-Axis MSB
data0 = bus.read_byte_data(0x18, 0x28)
data1 = bus.read_byte_data(0x18, 0x29)
# Convert the data
xAccl = data1 * 256 + data0
if xAccl > 32767 :
xAccl -= 65536
# LIS3DHTR address, 0x18(24)
# Read data back from 0x2A(42), 2 bytes
# Y-Axis LSB, Y-Axis MSB
data0 = bus.read_byte_data(0x18, 0x2A)
data1 = bus.read_byte_data(0x18, 0x2B)
# Convert the data
yAccl = data1 * 256 + data0
if yAccl > 32767 :
yAccl -= 65536
# LIS3DHTR address, 0x18(24)
# Read data back from 0x2C(44), 2 bytes
# Z-Axis LSB, Z-Axis MSB
data0 = bus.read_byte_data(0x18, 0x2C)
data1 = bus.read_byte_data(0x18, 0x2D)
# Convert the data
zAccl = data1 * 256 + data0
if zAccl > 32767 :
zAccl -= 65536
# Output data to screen
print "Acceleration in X-Axis : %d" %xAccl
print "Acceleration in Y-Axis : %d" %yAccl
print "Acceleration in Z-Axis : %d" %zAccl