Back to Step by Step Course by Robojax

Lesson 100: Control Servo Motor with your hand gesture

Part 9: Servo Motors

In This lesson we learn how to control a servo motor using hand gesture. For each gesture we could take action to move servo motor to a sertain angle or a full motion. We've used APDS-9960 Gesture sensor and a library is needed to be installed on Arduino IDE. Just copy this code below, open new project by clicking on File->New and then save and download it your Arduion. You need computer only once to send this program to Arduino board. then you don't need it. Just power up using a battery, power adapter or USB cable and it will work. for Servo motor if you are using the small SG90 servo, you can power it up using Arduino. But for Larger motors, make sure to provide power from independant battery or power source as Arduino can't supply enough power to larger Servo Motors.

Gestures detected

  • Waving hand to the right from top of sensor
  • Waving hand to the left from top of sensor
  • Moving hund forward from top of sensor
  • Moving hand backwards from top of sensor
  • Moving hand away from top of sensor
  • Brining hand from far to top of sensor

 * Arduio Step by Step Course by Robojax 
 * Lesson 100: Control Servo Motor with your hand gesture 
 * Using hand gesture RIGHT, LEFT, UP, DOWN, FAR, NEAR we control motor in any way we want
 * video details:
 * Code written based on sampel from this code
 * on Feb 13, 2022 by Ahmad Shamshiri.
 * This code is part of Arduino Step by Step Course which starts here:
 * for library of this code visit
If you found this tutorial helpful, please support me so I can continue creating 
content like this. Make a donation using PayPal by credit card
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <>.
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014

Tests the gesture sensing abilities of the APDS-9960. Configures
APDS-9960 over I2C and waits for gesture events. Calculates the
direction of the swipe (up, down, left, right) and displays it
on a serial console. 

To perform a NEAR gesture, hold your hand
far above the sensor and move it close to the sensor (within 2
inches). Hold your hand there for at least 1 second and move it

To perform a FAR gesture, hold your hand within 2 inches of the
sensor for at least 1 second and then move it above (out of
range) of the sensor.

Hardware Connections:

IMPORTANT: The APDS-9960 can only accept 3.3V!
 Arduino Pin  APDS-9960 Board  Function
 3.3V         VCC              Power
 GND          GND              Ground
 A4           SDA              I2C Data
 A5           SCL              I2C Clock
 2            INT              Interrupt

Include Wire.h and SparkFun_APDS-9960.h

This code is beerware; if you see me (or any other SparkFun 
employee) at the local, and you've found our code helpful, please
buy us a round!

Distributed as-is; no warranty is given.

#include <Wire.h>
#include <Servo.h>
#include <SparkFun_APDS9960.h>
//see details in this video:
int servoPin  =9;
int downAngle =135;
int upAngle = 45;
int rightAngle = 0;
int leftAngle = 180;

int angleStep =2;
int angle =90; //initial Angle

const int minAngle = 0;
const int maxAngle = 180;
Servo myservo;  // create servo object to control a servo

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {
 myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));

void loop() {
  if( isr_flag == 1 ) {
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);

void interruptRoutine() {
  isr_flag = 1;

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
      case DIR_DOWN:
      case DIR_LEFT:
      case DIR_RIGHT:
      case DIR_NEAR:
      case DIR_FAR:

void nearAction()

  for (angle = minAngle; angle <= maxAngle; angle += angleStep) { 
    // in steps of angleStep degree
  for (angle = maxAngle; angle >= minAngle; angle -= angleStep) { 
}//nearAction() ends

 void farAction()

 //do something here
}//farAction() ends

The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side