adafruit_lis3dh

CircuitPython driver for the LIS3DH accelerometer.

See examples in the examples directory.

  • Author(s): Tony DiCola

Implementation Notes

Hardware:

Software and Dependencies:

adafruit_lis3dh.AccelerationTuple

alias of acceleration

class adafruit_lis3dh.LIS3DH(int1: DigitalInOut | None = None, int2: DigitalInOut | None = None)[source]

Driver base for the LIS3DH accelerometer.

Parameters:
property acceleration: acceleration

The x, y, z acceleration values returned in a 3-tuple and are in \(m / s ^ 2\)

property data_rate: int

The data rate of the accelerometer.

Could have the following values:

  • DATARATE_400_HZ

  • DATARATE_200_HZ

  • DATARATE_100_HZ

  • DATARATE_50_HZ

  • DATARATE_25_HZ

  • DATARATE_10_HZ

  • DATARATE_1_HZ

  • DATARATE_POWERDOWN

  • DATARATE_LOWPOWER_1K6HZ

  • DATARATE_LOWPOWER_5KHZ.

property range: Literal[0, 1, 2, 3]

The range of the accelerometer.

Could have the following values:

  • RANGE_2_G

  • RANGE_4_G

  • RANGE_8_G

  • RANGE_16_G.

read_adc_mV(adc: Literal[1, 2, 3]) float[source]

Read the specified analog to digital converter value in millivolts. ADC must be a value 1, 2, or 3. NOTE the ADC can only measure voltages in the range of ~900-1200mV!

read_adc_raw(adc: Literal[1, 2, 3]) int[source]

Retrieve the raw analog to digital converter value. ADC must be a value 1, 2, or 3.

set_tap(tap: Literal[0, 1, 2], threshold: int, *, time_limit: int = 10, time_latency: int = 20, time_window: int = 255, click_cfg: int | None = None) None[source]

The tap detection parameters.

Note

Tap related registers are called CLICK_ in the datasheet.

Parameters:
  • tap (int) – 0 to disable tap detection, 1 to detect only single taps, and 2 to detect only double taps.

  • threshold (int) – A threshold for the tap detection. The higher the value the less sensitive the detection. This changes based on the accelerometer range. Good values are 5-10 for 16G, 10-20 for 8G, 20-40 for 4G, and 40-80 for 2G.

  • time_limit (int) – TIME_LIMIT register value. Defaults to 10

  • time_latency (int) – TIME_LATENCY register value. Defaults to 20

  • time_window (int) – TIME_WINDOW register value. Defaults to 255

  • click_cfg (int) – CLICK_CFG register value.

shake(shake_threshold: int = 30, avg_count: int = 10, total_delay: float = 0.1) bool[source]

Detect when the accelerometer is shaken. Optional parameters:

Parameters:
  • shake_threshold (int) – Increase or decrease to change shake sensitivity. This requires a minimum value of 10. 10 is the total acceleration if the board is not moving, therefore anything less than 10 will erroneously report a constant shake detected. Defaults to 30

  • avg_count (int) – The number of readings taken and used for the average acceleration. Default to 10

  • total_delay (float) – The total time in seconds it takes to obtain avg_count readings from acceleration. Defaults to 0.1

property tapped: int

True if a tap was detected recently. Whether its a single tap or double tap is determined by the tap param on set_tap. tapped may be True over multiple reads even if only a single tap or single double tap occurred if the interrupt (int) pin is not specified.

The following example uses board.I2C and specifies the interrupt pin:

import adafruit_lis3dh
import digitalio
import board

i2c = board.I2C()  # uses board.SCL and board.SDA
int1 = digitalio.DigitalInOut(board.D11) # pin connected to interrupt
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
lis3dh.range = adafruit_lis3dh.RANGE_8_G
class adafruit_lis3dh.LIS3DH_I2C(i2c: I2C, *, address: int = 24, int1: DigitalInOut | None = None, int2: DigitalInOut | None = None)[source]

Driver for the LIS3DH accelerometer connected over I2C.

Parameters:
  • i2c (I2C) – The I2C bus the LIS3DH is connected to.

  • address – The I2C device address. Defaults to 0x18

Quickstart: Importing and using the device

Here is an example of using the LIS3DH_I2C class. First you will need to import the libraries to use the sensor

import board
import adafruit_lis3dh

Once this is done you can define your board.I2C object and define your sensor object

i2c = board.I2C()  # uses board.SCL and board.SDA
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)

Now you have access to the acceleration attribute

acc_x, acc_y, acc_z = lis3dh.acceleration
class adafruit_lis3dh.LIS3DH_SPI(spi: SPI, cs: DigitalInOut, *, baudrate: int = 100000, int1: DigitalInOut | None = None, int2: DigitalInOut | None = None)[source]

Driver for the LIS3DH accelerometer connected over SPI.

Parameters:
  • spi (SPI) – The SPI bus the LIS3DH is connected to.

  • cs (digitalio.DigitalInOut) – The pin connected to the LIS3DH CS pin

Quickstart: Importing and using the device

Here is an example of using the LIS3DH_SPI class. First you will need to import the libraries to use the sensor

import board
import adafruit_lis3dh

Once this is done you can define your board.SPI object and define your sensor object

spi = board.SPI()
cs = digitalio.DigitalInOut(board.D5)  # Set to correct CS pin!
lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)

Now you have access to the acceleration attribute

acc_x, acc_y, acc_z = lis3dh.acceleration