Example Code for Arduino-Gesture Detection

Last revision 2026/01/15

In this section, we will use a simple experiment to teach you how to use our gesture sensor. Our goal is to get the sensor detect the gesture UP, DOWN, left and right.

Hardware Preparation

  • Hardware
    • UNO x1
    • RGB and Gesture Sensor x1

Software Preparation

Wiring Diagram

SEN0187 RGB Color and Gesture Sensor For Arduino Connection Diagram
Sensor Pin UNO Pin
VCC 5V
GND GND
SCL SCL
SDA SDA
INT 2

Other Preparation Work

Download and install the RGB and Gesture Sensor Library:

Click to download library files
Arduino Library Installation Tutorial
Next, we need to open the ARDUINO IDE, and copy the following simple code to the IDE window. Then select the right serial port and board (Arduino UNO). Wave your hand in front of the sensor, see what happen on the serial port.

Sample Code

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));

  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }

  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if( isr_flag == 1 ) {
    handleGesture();
      if(digitalRead(APDS9960_INT) == 0){
      apds.init();
      apds.enableGestureSensor(true);
    }

    isr_flag = 0;
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

Result

Wave your hand in front of the sensor, see what happen on the serial port.

Additional Information

This sensor not only support gesture recognition, but also the range, ambient light and RGB color detecting. The specific examples can be found in the library EXAMPLES folder. so you can also develop other application according to your requirement.

Was this article helpful?

TOP