Beaglebone and LIS2DH three-axis linear accelerometer example in python

The LIS2DH is an ultra low-power high performance three-axis linear accelerometer belonging to the “femto” family, with digital I2C/SPI serial interface standard output.

The LIS2DH has dynamically user selectable full scales of ±2g/±4g/±8g/±16g and it is capable of measuring accelerations with output data rates from 1 Hz to 5.3 kHz.

The self-test capability allows the user to check the functioning of the sensor in the final application.

The device may be configured to generate interrupt signals by two independent inertial wake-up/free-fall events as well as by the position of the device itself.

The LIS2DH is available in small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

Key Features

Wide supply voltage, 1.71 V to 3.6 V
Independent IOs supply (1.8 V) and supply voltage compatible
Ultra low-power mode consumptiondown to 2 µA
±2g/±4g/±8g/±16g dynamically selectable full-scale
I2 C/SPI digital output interface
2 independent programmable interrupt generators for free-fall and motion detection
6D/4D orientation detection
“Sleep to wake” and “return to sleep” function
Freefall detection
Motion detection
Embedded temperature sensor
Embedded FIFO

Parts Required

Name Link
Beaglebone BeagleBone Black TI AM335x Cortex-A8 development BB-Black Rev.C
LIS2DHTR LIS2DHTR CJMCU- three axis accelerometer module
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Connection

Beaglebone Module
3.3v – P9.3 Vcc
Gnd – P9.1 Gnd
SDA – P9.20 SDA
SCL – P9.19 SCL

Code

[codesyntax lang=”python”]

 This code is designed to work with the LIS2DHTR_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Accelorometer?sku=LIS2DHTR_I2CS#tabs-0-product_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(2)

# LIS2DHTR address, 0x19(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(0x19, 0x20, 0x27)

# LIS2DHTR address, 0x19(24)
# Select control register4, 0x23(35)
# 0x00(00) Continuous update, Full-scale selection = +/-2G
bus.write_byte_data(0x19, 0x23, 0x00)
time.sleep(0.5)

# LIS2DHTR address, 0x19(24)
# Read data back from 0x28(40), 2 bytes
# X-Axis LSB, X-Axis MSB
data0 = bus.read_byte_data(0x19, 0x28)
data1 = bus.read_byte_data(0x19, 0x29)
# Convert the data
xAccl = data1 * 256 + data0
if xAccl > 32767 :
    xAccl -= 65536

# LIS2DHTR address, 0x19(24)
# Read data back from 0x2A(42), 2 bytes
# Y-Axis LSB, Y-Axis MSB
data0 = bus.read_byte_data(0x19, 0x2A)
data1 = bus.read_byte_data(0x19, 0x2B)
# Convert the data
yAccl = data1 * 256 + data0
if yAccl > 32767 :
    yAccl -= 65536

# LIS2DHTR address, 0x19(24)
# Read data back from 0x2C(44), 2 bytes
# Z-Axis LSB, Z-Axis MSB
data0 = bus.read_byte_data(0x19, 0x2C)
data1 = bus.read_byte_data(0x19, 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

[/codesyntax]

 

Output

Here are a couple of runs

debian@beaglebone:/var/lib/cloud9/$ python LIS2DHTR.py
Acceleration in X-Axis : -13248
Acceleration in Y-Axis : -8832
Acceleration in Z-Axis : 3520
debian@beaglebone:/var/lib/cloud9/$ python LIS2DHTR.py
Acceleration in X-Axis : -24832
Acceleration in Y-Axis : 3968
Acceleration in Z-Axis : -4736
debian@beaglebone:/var/lib/cloud9/$ python LIS2DHTR.py
Acceleration in X-Axis : -17856
Acceleration in Y-Axis : -21568
Acceleration in Z-Axis : 5312

Link

http://www.st.com/resource/en/datasheet/lis2dh.pdf

LEAVE A REPLY

Please enter your comment!
Please enter your name here