Gesture Event Sample

This sample demonstrates detecting hand gestures using GPIO interrupt.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:


Sample Code
#!/usr/bin/env python3

#This sample demonstrates detecting hand gestures using GPIO interrupt.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
import RPi.GPIO as GPIO
from turta_iothat3 import Turta_Light
from turta_iothat3 import Turta_Buzzer

#Gesture Event
def gesture_event(ch):
    g = light.read_gesture()
    if g is not None:
        print("Gesture Detected: " + str(g))

#Interrupt Pin
gesture_int = 4

light = Turta_Light.LightSensor(Turta_Light.MODES.PROX_GES, True)
buzzer = Turta_Buzzer.BuzzerDriver()
GPIO.setup(gesture_int, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(gesture_int, GPIO.FALLING, callback=gesture_event, bouncetime=1)

#Add your configuration code here

    while True:
        #Add your code here


#Exit on CTRL+C
except KeyboardInterrupt:


When you run the sample, it waits for an interrupt signal from the light sensor. When interrupt is detected, the program reads the buffer from the light sensor and searches for gesture directions. If it finds a valid gesture, prints the direction. The example output should be like this:

Gesture Detected: GESTURES.LEFT
Gesture Detected: GESTURES.RIGHT
Gesture Detected: GESTURES.UP
Gesture Detected: GESTURES.DOWN

To stop the Python script, just press CTRL + C.

Last updated