Double Tap Recognition with BerryIMUv3

The BerryIMUv3 makes it easy to detect double taps, which can be used to control the GPIO of an a Raspberry Pi.

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.

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

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.

GPIO18 (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

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

Line 38, 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 39 , LSM6DSL_TAP_THS_6D is used to set the tap thresholds. a lower value will result in softer taps being detected.

Line 40 , 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 41, LSM6DSL_WAKE_UP_THS. Set the left most bit to enable double tap recognition.

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


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)

Source: Double Tap Recognition with BerryIMUv3

About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Scroll to Top