Home Code Raspberry Pi and LIS2DH three-axis linear accelerometer example

Raspberry Pi and LIS2DH three-axis linear accelerometer example

by shedboy71

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




LIS2DH Raspberry Pi
VCC 5V / 3V3
SDA SDA – Pin 3
SCL SCL – Pin 5




[codesyntax lang=”python”]

# 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.
# 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(1)

# 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)


# 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






You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More