Home / Tutorials / Arduino Tutorial / Using the Grove Gesture Sensor
Grove gesture sensor

Using the Grove Gesture Sensor

Wouldn’t it be cool if you could control something by just waving your hands? Well, this is what the grove gesture sensor does. In this post, I will show you how to use this sensor with an Arduino and create a simple gesture control project.

Getting Started

Using modules with arduino is easy but grove sensors make it a lot easier. With the grove shield, you can just plug the grove gesture sensor directly; no need for breadboards and jumper wires!

Grove - Gesture(PAJ7620U2)

[the_ad id="3059"]

The grove gesture sensor communicates via I2C. Thus, it must be connected to one of the I2C sockets on the grove shield.


Next, download the Arduino library for the sensor. You can download it on this link and place the files to Documents/Arduino/Libraries on you computer.

It is also possible to download the library via the library manager. On your Arduino IDE, go to Tools > Manage Libraries. On the search field of the Library Manager, type “PAJ7620U2”. The library should now appear.

[the_ad id="3059"] Click the “Install” button. This should now install the library on your computer.

After attaching the sensor to the shield and downloading the library, the gesture sensor is ready to use!

Using the Example Sketch

Next is to try out one of the examples included in the library. Open the example sketch at File->Example->Gesture_PAJ7620->paj7620_9gestures. Make sure your Arduino is now connected to your computer then open the serial monitor.

[the_ad id="3059"]

As the sketch’s name suggests, the grove gesture sensor can detect 9 different gestures. Those gestures are up, down, left, right, clockwise, counter clockwise, forward, backward, and wave. Try these movements about 5 to 10 cm in front of the sensor and you should see the output on the serial monitor.

PAJ7620U2 TEST DEMO: Recognize 9 gestures.
INIT SENSOR...
Addr0 =20,  Addr1 =76
wake-up finish.
Set up gaming mode.
Paj7620 initialize register finished.
INIT OK
Please input your gestures:

Left
Left
Up
Right
Up
Up
Down
Up
Down
Down
Up
Right
Left

Control a Servo Motor using Gestures

Now we will create a simple project that controls a servo motor using hand gestures. The servo motor arm can move either left or right only so we will be only using two gestures.

For this project, we follow this wiring diagram:

Grove gesture sensor servo motor project

Here is the full sketch:

#include <Wire.h>
#include "paj7620.h"
#include <Servo.h>

Servo servo;

#define GES_REACTION_TIME		500				// You can adjust the reaction time according to the actual circumstance.
#define GES_ENTRY_TIME			800				// When you want to recognize the Forward/Backward gestures, your gestures' reaction time must less than GES_ENTRY_TIME(0.8s). 
#define GES_QUIT_TIME			1000

void setup()
{
  servo.attach(6);
  
	uint8_t error = 0;

	Serial.begin(9600);
	Serial.println("\nControl a servo motor using gestures.");

	error = paj7620Init();			// initialize Paj7620 registers
	if (error) 
	{
		Serial.print("INIT ERROR,CODE:");
		Serial.println(error);
	}
	else
	{
		Serial.println("INIT OK");
	}
	Serial.println("Please input your gestures:\n");
}

void loop()
{
	uint8_t data = 0, data1 = 0, error;
	
	error = paj7620ReadReg(0x43, 1, &data);				// Read Bank_0_Reg_0x43/0x44 for gesture result.
	if (!error) 
	{
		switch (data) 									// When different gestures be detected, the variable 'data' will be set to different values by paj7620ReadReg(0x43, 1, &data).
		{
			case GES_RIGHT_FLAG:
				delay(GES_ENTRY_TIME);
				paj7620ReadReg(0x43, 1, &data);
        if(data == GES_FORWARD_FLAG) 
        {
          Serial.println("Forward");
          delay(GES_QUIT_TIME);
        }
        else if(data == GES_BACKWARD_FLAG) 
        {
          Serial.println("Backward");
          delay(GES_QUIT_TIME);
        }
        else
        {
					Serial.println("Right");
          servo.write(10);
          break;
				}          
				break;
			case GES_LEFT_FLAG: 
				delay(GES_ENTRY_TIME);
				paj7620ReadReg(0x43, 1, &data);
        if(data == GES_FORWARD_FLAG) 
        {
          Serial.println("Forward");
          delay(GES_QUIT_TIME);
        }
        else if(data == GES_BACKWARD_FLAG) 
        {
          Serial.println("Backward");
          delay(GES_QUIT_TIME);
        }
				else 
				{
					Serial.println("Left");
          servo.write(150);
          break;
				}          
				break;
			default:
				paj7620ReadReg(0x44, 1, &data1);
				if (data1 == GES_WAVE_FLAG) 
				{
					Serial.println("wave");
          for(int i=0;i<5;i++){
            servo.write(10);
            delay(100);
            servo.write(150);
            delay(100);
          }
          servo.write(90);
				}
				break;
		}
	}
	delay(100);
}
[the_ad id="3059"]

What project can you think of using the grove gesture sensor? Drop your answer on the comments below!

Check Also

arduino-pressure-sensor-mps20n0040d-front

Arduino Pressure Sensor Tutorial | MPS20N0040D

This site has built up a number of tutorials on how to measure almost anything. …

Leave a Reply

Your email address will not be published. Required fields are marked *