Gestures have become a popular way to interact with devices, especially in applications involving home automation, robotics, and gaming. In this tutorial, we will learn how to interface the PAJ7620 gesture sensor with an Arduino Uno and use it to detect various gestures like up, down, left, right, and more. Let's dive into the details of setting up the hardware, writing the code, and testing the system.

What is the GY-PAJ7620 Gesture Sensor?

The PAJ7620 is a highly capable gesture recognition sensor. It can detect a wide range of gestures such as up, down, left, right, forward, backward, and more. The sensor uses an array of infrared LEDs and a photodiode array to detect motion in its field of view. It communicates with a microcontroller (like Arduino) over the I2C interface, making it a versatile option for many gesture-controlled applications.

Technical Specifications of PAJ7620 Gesture Sensor:

  1. Communication Interface: I2C (Supports 400kHz Fast Mode I2C communication)
  2. Power Supply: 2.4V to 3.6V (typically 3.3V), Current Consumption: 1.6mA (normal mode), up to 4mA (gesture detection)
  3. Gesture Recognition: Detects 9 gestures, including Up, Down, Left, Right, Forward, Backward, Clockwise, Counterclockwise, and Wave
  4. Detection Range: 10 cm to 30 cm (optimal), Field of View: 60° (horizontal) x 60° (vertical)
  5. Interrupt Pin: External interrupt pin (connect to microcontroller interrupt pin)
  6. Operating Temperature Range: -20°C to 70°C
  7. Sensor Technology: Infrared LED Array and Photodiode Array, built-in 3D accelerometer and infrared sensor
  8. Output: I2C Address: 0x73, outputs gesture data to microcontroller
  9. Features: Low Power Consumption, Normal and High-speed gesture detection modes, User-configurable sensitivity
  10. Sensitivity: Adjustable detection range, works well in indoor and moderate outdoor lighting
  11. Response Time: Gesture Recognition Time: < 100ms

Components Required: 

  1. PAJ7620 Gesture Recognition Sensor Module
  2. Arduino Uno
  3. Arduino Uno Programming Cable
  4. Breadboard
  5. Jumper Wires

Pinout and Connections for GY-PAJ7620 :

    • VCC: Connect to the 5V pin on the Arduino.
    • GND: Connect to the GND pin on the Arduino.
    • SCL: Connect to the A5 pin on the Arduino (SCL for I2C communication).
    • SDA: Connect to the A4 pin on the Arduino (SDA for I2C communication).
    • INT: Connect to pin 2 on the Arduino (used for gesture detection interrupts).
GY-PAJ7620 Gesture Sensor Pinout

Arduino GY-PAJ7620 Gesture Sensor Circuit Diagram:

Arduino GY-PAJ7620 Gesture Sensor Circuit Diagram

Code Explanation:

#include <DFRobot_PAJ7620U2.h>
#include <Wire.h>

  • Includes the necessary libraries: DFRobot_PAJ7620U2.h for interacting with the PAJ7620U2 gesture sensor and Wire.h for I2C communication.

#define GESTURE_INT_PIN 2

  • Defines GESTURE_INT_PIN as pin 2, which will be used for the interrupt when a gesture is detected.
DFRobot_PAJ7620U2 paj;
  • Creates an instance of the DFRobot_PAJ7620U2 class called paj, which will allow interaction with the PAJ7620U2 gesture sensor.

volatile bool gestureDetected = false;

  • Declares a volatile boolean variable gestureDetected, which will be used to track whether a gesture has been detected. The volatile keyword ensures that the variable is properly handled within interrupt service routines (ISRs).
void gestureInterrupt() {
  gestureDetected = true;
}
  • Defines the interrupt service routine (ISR) gestureInterrupt(). This function is called whenever an interrupt is triggered. It sets the gestureDetected flag to true.

void setup() {
  Serial.begin(9600);
  delay(300);

  • Initializes serial communication at a baud rate of 9600 for debugging. A short delay of 300ms is added to allow time for the sensor to stabilize.

  while (paj.begin() != 0) {
    delay(500);
  }

  • Tries to initialize the PAJ7620U2 sensor. If initialization fails, it retries every 500ms until successful.

  paj.setGestureHighRate(false);

  • Sets the gesture detection rate to low, which may help reduce false positives.

  pinMode(GESTURE_INT_PIN, INPUT_PULLUP);
  • Configures GESTURE_INT_PIN (pin 2) as an input with an internal pull-up resistor. This ensures the pin reads HIGH unless pulled LOW by the sensor.
 attachInterrupt(digitalPinToInterrupt(GESTURE_INT_PIN), gestureInterrupt, FALLING);
}
  • Attaches an interrupt to pin 2 (the gesture interrupt pin). When the pin goes from HIGH to LOW (falling edge), it triggers the gestureInterrupt() ISR.

void loop() {
  if (gestureDetected) {
    gestureDetected = false;
    DFRobot_PAJ7620U2::eGesture_t gesture = paj.getGesture();

    if (gesture != paj.eGestureNone) {
      switch (gesture) {
        case 1:
          Serial.println("RIGHT");
          break;
        case 2:
          Serial.println("LEFT");
          break;
        case 4:
          Serial.println("UP");
          break;
        case 8:
          Serial.println("DOWN");
          break;
        case 16:
          Serial.println("FORWARD");
          break;
        case 32:
          Serial.println("BACKWARD");
          break;
        case 64:
          Serial.println("CLOCKWISE");
          break;
        case 128:
          Serial.println("ANTI-CLOCKWISE");
          break;
        case 256:
          Serial.println("WAVE");
          break;
        case 512:
          Serial.println("SLOW WAVE - DISORDER");
          break;
        case 3:
          Serial.println("SLOW WAVE - LEFT-RIGHT");
          break;
        case 12:
          Serial.println("SLOW WAVE - UP-DOWN");
          break;
        case 48:
          Serial.println("SLOW WAVE - FORWARD-BACKWARD");
          break;
        default:
          Serial.println("New Gesture");
          break;
      }
    }
  }
}

  • if (gestureDetected): This condition checks if the gestureDetected flag is true, indicating that a gesture has been detected by the sensor. If a gesture is detected, the code inside the block is executed. If not, it simply skips to the next iteration of the loop.

  • gestureDetected = false;: After detecting a gesture, this line resets the gestureDetected flag back to false so the program is ready to detect the next gesture. This ensures that gestures are only processed once.

  • DFRobot_PAJ7620U2::eGesture_t gesture = paj.getGesture();: This line calls the getGesture() method from the paj object to retrieve the type of gesture detected. The result is stored in the gesture variable, which can be one of several predefined gestures.

  • if (gesture != paj.eGestureNone): This checks if the gesture is not eGestureNone, which means a valid gesture was detected. If no gesture was detected, it skips the switch statement.

  • switch (gesture): The switch statement evaluates the value of the gesture variable, which contains a number corresponding to a specific gesture. Based on the gesture detected, a different case is executed.

  • case 1: through case 48:: Each case corresponds to a specific gesture. For example:

      • case 1: is for a "RIGHT" gesture.
      • case 2: is for a "LEFT" gesture.
      • And so on for all possible gestures like "UP", "DOWN", "FORWARD", "BACKWARD", and more.
    • For each recognized gesture, the program prints the corresponding gesture description (e.g., "RIGHT", "LEFT", etc.) to the Serial Monitor.

     

  • default:: If the detected gesture doesn't match any of the predefined cases, the default case is executed. It prints "New Gesture" to the Serial Monitor, indicating that an unrecognized gesture was detected.

 

Code For Arduino GY-PAJ7620 Gesture Sensor Interfacing:

#include <DFRobot_PAJ7620U2.h>
#include <Wire.h>

#define GESTURE_INT_PIN 2

DFRobot_PAJ7620U2 paj;
volatile bool gestureDetected = false;

void gestureInterrupt() {
  gestureDetected = true;
}

void setup() {
  Serial.begin(9600);
  delay(300);

  while (paj.begin() != 0) {
    delay(500);
  }

  paj.setGestureHighRate(false);

  pinMode(GESTURE_INT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(GESTURE_INT_PIN), gestureInterrupt, FALLING);
}

void loop() {
  if (gestureDetected) {
    gestureDetected = false;
    DFRobot_PAJ7620U2::eGesture_t gesture = paj.getGesture();

    if (gesture != paj.eGestureNone) {
      switch (gesture) {
        case 1:
          Serial.println("RIGHT");
          break;
        case 2:
          Serial.println("LEFT");
          break;
        case 4:
          Serial.println("UP");
          break;
        case 8:
          Serial.println("DOWN");
          break;
        case 16:
          Serial.println("FORWARD");
          break;
        case 32:
          Serial.println("BACKWARD");
          break;
        case 64:
          Serial.println("CLOCKWISE");
          break;
        case 128:
          Serial.println("ANTI-CLOCKWISE");
          break;
        case 256:
          Serial.println("WAVE");
          break;
        case 512:
          Serial.println("SLOW WAVE - DISORDER");
          break;
        case 3:
          Serial.println("SLOW WAVE - LEFT-RIGHT");
          break;
        case 12:
          Serial.println("SLOW WAVE - UP-DOWN");
          break;
        case 48:
          Serial.println("SLOW WAVE - FORWARD-BACKWARD");
          break;
        default:
          Serial.println("New Gesture");
          break;
      }
    }
  }
}

Video

Leave a comment

Please note, comments must be approved before they are published

Your cart

×