Arduino Tutorials

Recognize gestures with the APDS9960 sensor

recognize gestures with the APDS9960

Being able to recognize gestures with your Arduino gives you the possibility to control it and connected components without touching them. The APDS9960 sensor does a good job in this respect: it detects four directions of movement when you move your hand directly in front of it – up, down, left & right.

What you do with the detected movements is entirely up to you: Switching LEDs on and off or controlling a robot – everything is possible.

You can buy this sensor separately on a breakout board and connect it to your Arduino board or – as we describe it in this tutorial – use the Arduino Nano 33 BLE Sense on which this sensor (and many others) is already installed.

Arduino Nano 33 BLE Sense with Headers [ABX00035]
Uhr 64MHz, Flask 1MB, RAM 256KB.; Zwei 15-polige Steckverbinder eine auf jeder Seite der...
40,88 EUR

The right library

For the sensor APDS9960 there are several libraries that make your life easier. In this tutorial we use the library ArduinoAPDS9960. Open your Library Manager and install the latest version.

Arduino library ArduinoAPDS9960 to recognize gestures.

With the help of this library, it is easy to identify gestures. And not only that, as you know for sure, this sensor can also detect colors and physical proximity – you can use this library to do that too.

Recognizing gestures made easy

When you have installed the library you will find the sketch GestureSensor in the menu under File > Examples > APDS_9960. Open and load it onto your Arduino to start a first test.

In your serial monitor (watch the baud rate) you can now see if everything works. Move your hand directly in front of the sensor in one direction and check if it has detected the direction correctly. If not, rotate it (or the Arduino Nano 33 BLE Sense) so that your gesture matches the detected movement.

You will have to practice your gestures a bit to make the APDS9960 recognize them reliably. In our tests it happened quite often that a natural movement was misinterpreted.

A closer look at the code

Let’s take a closer look at the sketch. Only very few functions are needed to use the gesture sensor. First of all, of course, you include the library mentioned above:

#include <Arduino_APDS9960.h>

Then initialize the sensor in the setup with

APDS.begin();

Within the Loop you first check if the sensor is available and working. Then you check with APDS.gestureAvailable() if it has recognized a gesture. With a switch…case you check which one it was, display it in the serial monitor and leave the request until the next call.

  if (APDS.gestureAvailable()) {
    int gesture = APDS.readGesture();

    switch (gesture) {
      case GESTURE_UP:
        Serial.println("Detected UP gesture");
        break;

      case GESTURE_DOWN:
        Serial.println("Detected DOWN gesture");
        break;

      case GESTURE_LEFT:
        Serial.println("Detected LEFT gesture");
        break;

      case GESTURE_RIGHT:
        Serial.println("Detected RIGHT gesture");
        break;

      default:
        break;
    }
  }

And that was it with the basic functionality of the APDS9960. For all further applications your creativity is needed. 🙂

The fine adjustments

During your tests, you will soon notice that the sensor is not always positioned correctly and that gestures are often misdetected. With the function setGestureSensitivity(sensitivity) you can readjust here.

The argument sensitivity can have a value from 0 to 100. By default – that is, if you do not use this function – the value is 80.

A lower value makes gesture recognition more accurate, but may not always detect that a gesture has been performed. A higher value makes the sensor detect gestures better, but may not always detect them correctly.

As is often the case, a good eye for detail is required until you find the right setting for you.

Control your Arduino with gestures

Of course you are not limited to outputting the recognized gestures to the Serial Monitor. You could use it to control the light in your apartment or just a simple jump’n’run.

This project is interesting for you if you want to recognize more complex gestures using artificial intelligence.

Letzte Aktualisierung am 2021-01-09 / Affiliate Links / Bilder von der Amazon Product Advertising API

You may also like

Comments are closed.