Double tap detection with BerryIMUv3

The accelerometer(LSM6DSL) on the BerryIMUv3 has built in double tap detection, which makes it very easy to detect double taps without the need for any fancy code.

double tap IMU

When the LSM6DSL detects a double tap, it can fire an interrupt pin on the BerryIMUv3.  We will use a Raspberry Pi to monitor the interrupt pin and turn a LED off and on when a double-tap is detected.


Double-Tap event recognition has special registers which control tap recognition functionality, these are the tap threshold and the Shock, Quiet and Duration time windows

Double-tap event recognition
Double-tap event recognition


The Raspberry Pi will configure the BerryIMUv3 for double tap recognition. It will also monitor for double taps, which will be used to turn a LED on and off.


INT1 On the BerryIMUv3 will go high when a double tap is detected.

(physical pin 12) on the Raspberry Pi will be used to monitor INT1 , using an interrupt.

GPIO20 (physical pin 28) will be used to drive the LED.

The resister below is 330 Ohms

Here is the hock up diagrams

BerryIMU double-tap using QWIIC cable
BerryIMU double-tap using QWIIC cable
BerryIMU double-tap
BerryIMU double-tap
import signal
from LSM6DSL import *
import sys
import RPi.GPIO as GPIO 
import smbus

bus = smbus.SMBus(1)

LED_ON = 0                                                           #Used to track of the current state of the LED
INTERRUPT_PIN = 12                                                   #The interrupt pin which will be connected to the IMU
LED_PIN = 38                                                         #The pin which will be driving the LED

#Used to clean up when Ctrl-c is pressed
def signal_handler(sig, frame):

#Used to write to the IMU
def writeByte(device_address,register,value):
    bus.write_byte_data(device_address, register, value)

def LEDnotification(channel):
	global LED_ON
	if LED_ON:
		LED_ON = 0
		LED_ON = 1

writeByte(LSM6DSL_ADDRESS,LSM6DSL_CTRL1_XL,0b01100000)                 #ODR_XL = 416 Hz, FS_XL = +/- 2 g
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_CFG,0b10001110)                  #Enable interrupts and tap detection on X, Y, Z-axis
writeByte(LSM6DSL_ADDRESS,LSM6DSL_TAP_THS_6D,0b10001100)               #Set tap threshold
writeByte(LSM6DSL_ADDRESS,LSM6DSL_INT_DUR2,0b01111111)                 #Set Duration, Quiet and Shock time windows
writeByte(LSM6DSL_ADDRESS,LSM6DSL_WAKE_UP_THS,0b10000000)              #Double-tap enabled 
writeByte(LSM6DSL_ADDRESS,LSM6DSL_MD1_CFG,0b00001000)                  #Double-tap interrupt driven to INT1 pin


GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.output(LED_PIN, 0)
GPIO.add_event_detect(INTERRUPT_PIN, GPIO.RISING, callback=LEDnotification, bouncetime=300)

while True:
    signal.signal(signal.SIGINT, signal_handler)

We will cover specific code which relates to double-tap recognition.

Line 37 , LSM6DSL_TAP_CFG is used to enable tap recognition on the X, Y, Z directions.  It is also used to enable the interrupt function for double-tap recognition.

Line 38 , LSM6DSL_TAP_THS_6D is used to set the tap thresholds. a lower value will result in softer taps being detected.

Line 39 , LSM6DS_INT_DUR2 is used to set the duration, quiet and shock time window.  A larger duration will result in a longer time between 1st and 2nd tap.

Line 40, LSM6DSL_WAKE_UP_THS. Set the left most bit to enable double tap recognition.

Line 41,  LSM6DSL_MD1_CFG is used to set which interrupt pin ont he BerryIMUv3 is used. In this instance, it is set to INT1.








Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.