Skip to Content

PAJ7620U2 Gesture Sensor with Arduino

PAJ7620U2 Gesture Sensor with Arduino

In this tutorial you will learn how to use the PAJ7620U2 Gesture Sensor with Arduino or other common microcontrollers such as the ESP32 or ESP8266.

We will be using three different Arduino libraries to recognize gestures with the PAJ7620U2 sensor. And we will use the gesture detection of the PAJ7620U2 to control the position of a servo.

For other gesture sensors have a look at our tutorials for the APDS-9930 and the APDS-9960 Gesture Sensor.

Required Parts

For this project you will need an PAJ7620U2 sensor and a microcontroller. I used an Arduino Uno but any other Arduino or any ESP32/ESP8266 will work as well, as long as it provides a 3.3V power output. One of the examples also uses a servo, but this is not an essential part for this project.

PAJ7620U2 Sensor

Arduino

Arduino Uno

USB Data Sync cable Arduino

USB Cable for Arduino UNO

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

Features of the PAJ7620U2 Gesture Sensor

The PAJ7620U2 is a very small (5.2x3x1.88 mm) gesture detection sensor. It can recognize 13 human
hand gesticulations such as moving up, down, left, right, forward, backward, circle-clockwise, circle-counter, clockwise, waving and combinations of the some of the gestures, e.g. up-down.

Note that there is also a PAJ7620 sensor that is very similar to the PAJ7620U2 but recognizes only 9 gestures.

The PAJ7620U2 has two detection modes: fast and slow. Gesture speed is 60°/s to 600°/s in slow (normal) and 60°/s to 1200°/s in fast (gaming) mode.

The chip is designed to operate from 2.8V to 3.6V, with a current consumption of just 2.82 mA, and it communicates via an I2C interface with up to 400 kbit/s.

For more detailed information have a look at the datasheet for the PAJ7620U2 linked below:

Breakout board for PAJ7620U2

The PAJ7620U2 chip is too small to connect it to an Arduino directly. Typically, you will need a breakout board as shown below:

Front and Back of Breakout board for PAJ7620U2
Front and Back of Breakout board for PAJ7620U2

Most breakout boards for the PAJ7620U2 have the following five pins:

  • VIN: Power supply (2.8 – 3.6V)
  • GND: Ground
  • SDA: I2C data signal
  • SCL: I2C clock signal
  • INT: Interrupt pin

In the next section I show you how to connect a PAJ7620U2 breakout board to an Arduino.

Connecting the PAJ7620U2 to Arduino

Thanks to the I2C interface of the PAJ7620U2, connecting it to an Arduino is easy. First, connect the SCL pin of the PAJ7620U2 breakout board to A5 of the Arduino. Similarly, connect SDA to A4 of the Arduino. Next, connect GND to GND and 3.3V to VCC of the PAJ7620U2.

Connecting PAJ7620U2 to Arduino
Connecting PAJ7620U2 to Arduino

Make sure that you use 3.3V as power supply. The PAJ7620U2 sensor is not rated for 5V and breakout boards for the PAJ7620U2 typically do not have a voltage regulator.

Code to detect Gestures with PAJ7620U2

Before we can write any code to detect gestures with the PAJ7620U2 we need to install a library. I went with the PAJ7620U2 Library by Seeed-Studio. To install this library, open the Library Manager, search for “PAJ7620”, find the library “Gesture PAJ7620” by Seeed-Studio and click on INSTALL

Installing PAJ7620U2 Library by Seeed-Studio via Library Manager
Installing PAJ7620 Library by Seeed-Studio via Library Manager

With the library installed, let’s write some test code to try the sensor out. Upload the following code, open the Serial Monitor and wave your hand in front of the sensor in a distance of about 5cm.

#include "Wire.h"
#include "paj7620.h"

void setup() {
  Serial.begin(9600);
  if (paj7620Init()) {
    Serial.println("Could not initalize Sensor!");
  }
}

void loop() {
  uint8_t data = 0;

  if (!paj7620ReadReg(0x43, 1, &data)) {
    if (data == GES_RIGHT_FLAG) Serial.println("Right");
    if (data == GES_LEFT_FLAG) Serial.println("Left");
    if (data == GES_UP_FLAG) Serial.println("Up");
    if (data == GES_DOWN_FLAG) Serial.println("Down");
  }
  delay(100);
}

You should see the following output on your Serial Monitor after passing your hand across the sensor in all four directions:

Gesture Detection of PAJ7620U2 on Serial Monitor
Gesture Detection of PAJ7620U2 on Serial Monitor

The picture below shows the orientation of the sensor and the directions you have to move your hand across the sensor to get the corresponding gesture readings:

Gesture Orientation of PAJ7620U2 Sensor
Gesture Orientation of PAJ7620U2 Sensor

Install updated PAJ7620U2 Library

As of Oct 2024 the PAJ7620U2 Library by Seeed-Studio that can be installed via the Library Manager is limited to version 1.0.0. While Seeed-Studio has released an updated version 2.0.0, it can’t be installed via the Library Manager. In this section I show you how to install and use the updated library.

Before installing version 2.0.0 make sure that you uninstall version 1.0.0 first! You can do this by opening the Library Manager, search for ” PAJ7620″, and press “UNINSTALL” for the PAJ7620U2 Library by Seeed-Studio.

Then go to the Grove_Gesture repo, click the green “Code” button and then press “Download ZIP”, which downloads a file named “Grove_Gesture-master.zip” to your computer.

Download updated PAJ7620U2 Library from repo

You can then install this ZIP library via Sketch -> Include Library -> Add .ZIP Library ...

Below you will find a simple example that uses the updated PAJ7620U2 Library to recognize four gestures (Up, Down, Left, Right):

#include "Gesture.h"

paj7620 Gesture;

void setup() {
  Serial.begin(9600);
  if (!Gesture.init()) {
    Serial.println("PAJ7620U2 initialization failed");
  }
}

void loop() {
  paj7620_gesture_t result;
  if (Gesture.getResult(result)) {
    if (result == UP) Serial.println("Up");
    if (result == DOWN) Serial.println("Down");
    if (result == LEFT) Serial.println("Left");
    if (result == RIGHT) Serial.println("Right");
  }
  delay(100);
}

It is essentially the same example as the one shown for old, version 1.0.0 library. But you can see that the new code is a bit more elegant, since there is now a Gesture and a result object.

The Grove_Gesture repo has other examples that demonstrate how to recognize 9 gestures or 15 gestures. However, if you look at the code, it is rather complex to and honestly quite ugly. In the next section, I therefore show you another library, which is much nicer to use, if you want to detect more complex gestures.

Install RevEng_PAJ7620 Library

In this example we are going to use the RevEng_PAJ7620 Library to detect 9 gestures. To install this library, open the Library Manager, search for “RevEng PAJ7620”, find the library “RevEng PAJ7620” by Aaron S. Crandall and click on INSTALL:

Installing RevEng PAJ7620 Library via Library Manager
Installing RevEng PAJ7620 Library via Library Manager

And here is the example code that uses the RevEng PAJ7620 Library to recognize 9 different gestures:

#include "RevEng_PAJ7620.h"

RevEng_PAJ7620 sensor = RevEng_PAJ7620();

void setup() {
  Serial.begin(9600);
  if (!sensor.begin()) {
    Serial.print("PAJ7620U2 initialization failed");
  }
}

void loop() {
  Gesture gesture = sensor.readGesture();

  switch (gesture) {
    case GES_FORWARD:
      Serial.println("FORWARD");
      break;
    case GES_BACKWARD:
      Serial.println("BACKWARD");
      break;
    case GES_LEFT:
      Serial.println("LEFT");
      break;
    case GES_RIGHT:
      Serial.println("RIGHT");
      break;
    case GES_UP:
      Serial.println("UP");
      break;
    case GES_DOWN:
      Serial.println("DOWN");
      break;
    case GES_CLOCKWISE:
      Serial.println("CLOCKWISE");
      break;
    case GES_ANTICLOCKWISE:
      Serial.println("ANTICLOCKWISE");
      break;
    case GES_WAVE:
      Serial.println("WAVE");
      break;
  }
}

As you can see, it is very simple. First, we include the library and create the sensor object. In the setup function we initialize serial communication and the sensor. And in the loop function, we read a gesture and the use a switch statement to print out, what gestures was detected.

I found that up, down, left, right, forward and backward gestures are detected reliably. However, the sensor had difficulties detecting the clockwise, anti-clockwise and wave gestures. They may take some practice to get working.

In the next section we are going to use the PAJ7620U2 to control a servo with gestures.

Control servo with PAJ7620U2 Gesture Sensor

First we need to connect the servo to the Arduino. If you have one of those little SG90 Micro Servos then you can connect them directly to an Arduino (no need for a separate power supply). Just connect the red wire (middle pin) of the servo to 5V, the brown wire to ground (GND) and the orange/yellow wire to pin 13, as shown below.

Connecting Servo to Arduino
Connecting Servo to Arduino

If you need more info on how to connect and use these servos have a look at the How to control servo motors with Arduino tutorial.

The following code uses the updated PAJ7620U2 Library by Seeed-Studio (V2.0.0). It detects four gestures and positions the servo accordingly.

#include "Gesture.h"
#include "Servo.h"

paj7620 sensor;
Servo servo;

const int servoPin = 13;

void setup() {
  sensor.init();
  servo.attach(servoPin);
  servo.write(90);
}

void loop() {
  paj7620_gesture_t gesture;
  if (sensor.getResult(gesture)) {
    if (gesture == UP) servo.write(90);
    if (gesture == DOWN) servo.write(90);
    if (gesture == LEFT) servo.write(170);
    if (gesture == RIGHT) servo.write(10);
  }
}

First, we include the standard Servo library, (no need to install a library). Then we create the servo object and define the pin the servo is connected to (servoPin).

In the setup function we attach the servo to the servo pin and initially orient the servo to 90°. In the loop function we replace the print function calls by write commands to the servo. For an up or down gesture, we move the servo to the 90° position. A left gesture moves the servo to a 10° position and a right gestures moves it to a 170° position. The short video clip below demonstrates the code in action:

Controlling Servo with Gestures and PAJ7620U2

And here is the same code using the RevEng_PAJ7620 Library. It operates in same way but I found it to be a little bit less responsive, thought that might be just an impression.

#include "RevEng_PAJ7620.h"
#include "Servo.h"

RevEng_PAJ7620 sensor = RevEng_PAJ7620();
Servo servo;

const int servoPin = 13;

void setup() {
  sensor.begin();
  servo.attach(servoPin);
  servo.write(90);
}

void loop() {
  Gesture gesture = sensor.readGesture();
  if (gesture == GES_UP) servo.write(90);
  if (gesture == GES_DOWN) servo.write(90);
  if (gesture == GES_LEFT) servo.write(170);
  if (gesture == GES_RIGHT) servo.write(10);
}

With that you now have a simple gesture control for a small servo, which you could use, for instance, to open or close a box without touch.

Conclusions

In this tutorial you learned how to use the PAJ7620U2 Gesture Sensor with an Arduino to detect gestures using different libraries.

In comparison to the APDS-9930 or the APDS-9960 Gesture Sensor, the PAJ7620U2 can detect many more gestures (up to 15) compared to the four gestures of the APDS-9960 or the single gesture of the APDS-9930.

The range of Gesture Sensors is typically limited to about 10 cm. If you want to accurately measure distances to far objects, e.g. for robotics applications, you should use infrared distance sensors such as the GP2Y0A710K0F that use triangulation to determine the distance to an object, instead. Even better are Time-of-Flight (ToF) distance sensors such as the TOF10120 or the VL53L1X library, that can measure distances up to several meters.

If you have any questions feel free to leave them in the comment section.

Happy Tinkering ; )