Example Code for Arduino - Custom Gesture Recognition

Last revision 2026/03/05

The tutorial demonstrates how Arduino can use AI pose and gesture sensor SEN0670 and ESP32 controller DFR0654 for custom gesture recognition. Covering I2C/UART wiring, library installation, and example code to confidently detect learning gestures and unknown gestures, and output keypoint coordinates

Hardware Preparation

Software Preparation

Wiring Diagram

I²C Connection (The following examples all use I²C communication)
SEN0670-I2C wiring

Connection Description:

Sensor Pin: VCC Connect to Main Controller Pin: 5V
Sensor Pin: GND Connect to Main Controller Pin: GND
Sensor Pin: C Connect to Main Controller Pin: 22/SCL
Sensor Pin: D Connect to Main Controller Pin: 21/SDA

UART Connection
SEN0670-UART wiring
Connection Description:

Sensor Pin: VCC Connect to Main Controller Pin: 5V
Sensor Pin: GND Connect to Main Controller Pin: GND
Sensor Pin: R Connect to Main Controller Pin: 26/D3
Sensor Pin: T Connect to Main Controller Pin: 25/D2

Note:

  • The default communication method of the sensor is I²C. If you need to switch to UART, use the on‑board DIP switch to change the communication method.
  • After switching, the sensor must be powered off and on again for the change to take effect.

Sample Code

Before starting this example, make sure the sensor has already learned custom gestures using the host computer software. If no gestures have been learned beforehand, the sensor will recognise them as unknown gestures.

Function: Drive gesture sensor via I2C, output gesture type, confidence and position data.

#include <DFRobot_HumanPose.h>

// I2C communication by default
const uint8_t I2C_ADDR = 0x3A;
DFRobot_HumanPose_I2C humanPose(&Wire, I2C_ADDR);

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

  // 1. Initialise the sensor
  while (!humanPose.begin()) {
    Serial.println("Sensor initialisation failed, please check the wiring!");
    delay(1000);
  }
  Serial.println("Sensor initialisation successful!");

  // 2. Set to "hand detection" mode
  humanPose.setModelType(DFRobot_HumanPose::eHand);
}

void loop()
{
  // 3. Get the detection result
  if (humanPose.getResult() == DFRobot_HumanPose::eOK) {
    // Iterate through all detected hands/gestures
    while (humanPose.availableResult()) {
      Result *result = humanPose.popResult();
      if (result) {
        // Logic: id=0 means not learned, id≠0 means learned
        if (result->id == 0) {
          Serial.println("[Unknown gesture]");
        } else {
          Serial.print("[Learned gesture] Name: ");
          Serial.println(result->name); // Print the name of the learned gesture
        }
        
        // Print common information: confidence, position
        Serial.print("Confidence: "); Serial.println(result->score);
        Serial.print("Position: left="); Serial.print(result->xLeft);
        Serial.print(" top="); Serial.print(result->yTop);
        Serial.print(" width="); Serial.print(result->width);
        Serial.print(" height="); Serial.println(result->height);
        Serial.println("------------------------");
      }
    }
  }

  delay(100); // 100ms delay to avoid spamming the serial monitor
}

Result:

The sensor automatically recognises gestures and can distinguish between learned and unlearned gestures.
SEN0670-Get gesture

Was this article helpful?

TOP