Simple test

Ensure your device works with this simple test.

examples/lis331_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_lis331

i2c = board.I2C()  # uses board.SCL and board.SDA
lis = adafruit_lis331.LIS331HH(i2c)

while True:
    print("Acceleration : X: %.2f, Y:%.2f, Z:%.2f ms^2" % lis.acceleration)
    time.sleep(0.1)

Low Pass Filter

Example showing a low pass filter example

examples/lis331_low_pass_filter.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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
from adafruit_lis331 import LIS331HH, Rate, Frequency

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

# un-comment the sensor you are using
# lis = H3LIS331(i2c)
lis = LIS331HH(i2c)

# `data_rate` must be a `LOWPOWER` rate to use the low-pass filter
lis.data_rate = Rate.RATE_LOWPOWER_10_HZ
# next set the cutoff frequency. Anything changing faster than
# the specified frequency will be filtered out
lis.lpf_cutoff = Frequency.FREQ_74_HZ

# Once you've seen the filter do its thing, you can comment out the
# lines above to use the default data rate without the low pass filter
# and see the difference it makes

while True:
    print(lis.acceleration)  # plotter friendly printing
    time.sleep(0.002)

High Pass Filter

Example showing a high pass filter example

examples/lis331_high_pass_filter.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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_lis331

i2c = board.I2C()  # uses board.SCL and board.SDA
# un-comment the sensor you are using
# lis = H3LIS331(i2c)
lis = adafruit_lis331.LIS331HH(i2c)

# use a nice fast data rate to for maximum resolution
lis.data_rate = adafruit_lis331.Rate.RATE_1000_HZ

# enable the high pass filter without a reference or offset
lis.enable_hpf(
    True, cutoff=adafruit_lis331.RateDivisor.ODR_DIV_100, use_reference=False
)

# you can also uncomment this section to set and use a reference to offset the measurements
# lis.hpf_reference = 50
# lis.enable_hpf(True, cutoff=RateDivisor.ODR_DIV_100, use_reference=True)


# watch in the serial plotter with the sensor still and you will see the
# z-axis value go from the normal around 9.8 with the filter off to near zero with it
# enabled. If you have a reference enabled and set, that will determind the center point.

# If you shake the sensor, you'll still see the acceleration values change! This is the
# Filter removing slow or non-changing values and letting through ones that move more quickly

while True:
    print(lis.acceleration)  # plotter friendly printing
    time.sleep(0.02)