cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2HH12- Tilt detection with interrupt

Zeinchehab
Associate

Hello everyone,

I'm stuck and I need some help. I have the LIS2HH12 accelerometer connected to my raspberry pi via SPI. So far, I have been able to collect real-time accelerometer data . But my goal is to only get data once the accelerometer is titled or moved(I've noticed that z goes to less than 0.7g when tilted). I'm trying to program the INT1 pin to send a high or low signal to my gpio once movement is detected. But so far, I've failed.

 

Here's my code, if anyone can please look at it and help a desperate PhD student I would be very thankful!

import spidev
import RPi.GPIO as GPIO
import time

# SPI Configuration
spi = spidev.SpiDev()
spi.open(0, 0) # Bus 0, Device 0
spi.max_speed_hz = 10000000 # 10 MHz
spi.mode = 0b11

# GPIO Configuration
INT_PIN = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(INT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Register addresses
CTRL1 = 0x20
CTRL2 = 0x21
CTRL3 = 0x22
CTRL4 = 0x23
CTRL5 = 0x24
CTRL7 = 0x26
IG_THS_X1 = 0x32
IG_THS_Y1 = 0x33
IG_THS_Z1 = 0x34
IG_DUR1 = 0x35
IG_CFG1 = 0x30
IG_SRC1 = 0x31
XL_REFERENCE = 0x3A

def write_register(reg, value):
spi.xfer2([reg & 0x7F, value])

def read_register(reg):
return spi.xfer2([reg | 0x80, 0x00])[1]

# Initialization sequence
write_register(CTRL1, 0x3F) # ODR 100Hz, XYZ enabled, BDU
write_register(CTRL2, 0x02) # HP filter for interrupt
write_register(CTRL3, 0x08) # INT1 from IG1
write_register(CTRL4, 0x04) # FS=2g, auto-increment
write_register(CTRL5, 0x00) # Active high, push-pull
write_register(CTRL7, 0x04) # Latched interrupt

# Set thresholds (250 mg)
write_register(IG_THS_X1, 0x20)
write_register(IG_THS_Y1, 0x20)
write_register(IG_THS_Z1, 0x20)

write_register(IG_DUR1, 0x00) # No duration

# Dummy read to settle HP filter
read_register(XL_REFERENCE)

# Configure interrupt sources
write_register(IG_CFG1, 0x2A) # Enable XHIE, YHIE, ZHIE

print("Waiting for wake-up event...")
try:
while True:
if GPIO.input(INT_PIN) == GPIO.LOW:
# Read interrupt source to clear INT pin
src=read_register(IG_SRC1)
print(f"Interrupt triggered! Source: {bin(src)}")
# Add your event handling code here

except KeyboardInterrupt:
spi.close()
GPIO.cleanup()

1 REPLY 1
Federica Bossi
ST Employee

Hi @Zeinchehab ,

I see that you followed the example in section 6.3.3 of the application note so your writings are correct.

If you need to detect when the Z-axis goes below 0.7g, calculate the corresponding threshold value based on the sensor's resolution and set it in IG_THS_Z1.

If you want to detect low events (e.g., when Z-axis goes below a certain value), adjust IG_CFG1 to enable low event detection.

In addition, you could print statements to verify that the register values are being set correctly and that the interrupt is being triggered as expected and ensure that the wiring and connections between the accelerometer and Raspberry Pi are correct, particularly the connection to the INT1 pin.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.