Example Code for Arduino-Gesture and Touch Detection

Last revision 2026/01/15

In this tutorial, we will show you the basic usages of the sensor. First of all, connect all parts together and download the codes to Arduino UNO. Open the serial monitor, when we do any one of the seven gestures or touch any touch pad on the sensor, there should be a corresponding reaction appearing on the serial monitor.

Hardware Preparation

  • DFRduino UNO R3 x 1
  • IO Sensor Expansion Shield V7.1 x 1
  • Gesture&Touch Sensor (SEN0285) x 1
  • PC x 1
  • Connectors

Software Preparation

Wiring Diagram

SEN0285 Gravity: Gesture & Touch Sensor Connection Diagram

Other Preparation Work

Since the gesture & touch sensor is a serial device and there is only 1 hardware serial port on Arduino, we recommend using the sensor with software serial or you can use device with multiple serial ports such as Arduino Leonardo, Arduino Mega2560 and so on. Here we choose Arduino UNO as the controller and define D10 and D11 as software serial port.

Sample Code

/*!
 * @file simpleGesture.ino
 * @brief Sensor event will print on your serial monitor
 * @n     for esp32, rx_pin = D5, tx_pin = D6
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license     The MIT License (MIT)
 * @author      PengKaixing([email protected])
 * @version  V1.0.0
 * @date  2022-03-25
 * @url https://github.com/DFRobot/DFRobot_Gesture_Touch
 */

#include "DFRobot_Gesture_Touch.h"

#ifdef __AVR__
  SoftwareSerial    mySerial(/*RX*/10, /*TX*/11); 
#elif defined ESP_PLATFORM
// ESP32:IO16 <--> TX:sensor
// ESP32:IO17 <--> RX:sensor
HardwareSerial mySerial(1);
#endif

// init sensor object, request write and read function
DFRobot_Gesture_Touch   DFGT(&mySerial);    

void setup()
{
  Serial.begin(115200);

  // suggest default value
  DFGT.setGestureDistance(20);

  // enable all functions
  DFGT.enableFunction(DFGT_FUN_ALL);

  // disable function test
  //DFGT.disableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT);

  // enable function test
  // DFGT.enableFunction(DFGT_FUN_RIGHT | DFGT_FUN_LEFT);

  // set auto sleep time out, in sleep mode, something approach will wake it up
  // DFGT.setSleep(4);

  Serial.println("simple Gesture!");
}

void loop()
{
  // get an event that data saved in serial buffer
  int8_t rslt = DFGT.getAnEvent();  

  if(rslt != DF_ERR) 
  {
    // disable auto sleep
    // DFGT.setSleep(DFGT_SLEEP_DISABLE);
    switch(rslt) 
    {
      case DFGT_EVT_BACK: 
        Serial.println("get event back");
        break;
      case DFGT_EVT_FORWARD: 
        Serial.println("get event forward");
        break;
      case DFGT_EVT_RIGHT: 
        Serial.println("get event right");
        break;
      case DFGT_EVT_LEFT: 
        Serial.println("get event left");
        break;
      case DFGT_EVT_PULLUP: 
        Serial.println("get event pull up");
        break;
      case DFGT_EVT_PULLDOWN: 
        Serial.println("get event pull down");
        break;
      case DFGT_EVT_PULLREMOVE: 
        Serial.println("get event pull and remove");
        break;
      case DFGT_EVT_TOUCH1: 
        Serial.println("get event touch1");
        break;
      case DFGT_EVT_TOUCH2: 
        Serial.println("get event touch2");
        break;
      case DFGT_EVT_TOUCH3:  
        Serial.println("get event touch3");
        break;
      case DFGT_EVT_TOUCH4: 
        Serial.println("get event touch4");
        break;
      case DFGT_EVT_TOUCH5: 
        Serial.println("get event touch5");
        break;
    }
  }
}

Result

When you perform any of the seven gestures or touch any touch pad on the sensor, the corresponding event message (e.g., "get event back", "get event touch1") will be displayed on the Arduino serial monitor.

Additional Information

How to use gestures recognization (The maxmium height the sensor can detect is 30cm)

SEN0285 Gravity: Gesture & Touch Sensor How to use gestures recognization SEN0285 Gravity: Gesture & Touch Sensor How to use gestures recognization

Was this article helpful?

TOP