Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Gesture-Controlled Servo Motor Using Arduino Nano and APDS-9960 Sensor

Image of Gesture-Controlled Servo Motor Using Arduino Nano and APDS-9960 Sensor

Circuit Documentation

Summary

This circuit consists of an Arduino Nano microcontroller, an APDS-9960 RGB and Gesture Sensor, and a Micro Servo 9G. The Arduino Nano reads gesture inputs from the APDS-9960 sensor and controls the position of the Micro Servo based on specific gesture patterns.

Component List

APDS-9960 RGB and Gesture Sensor

  • Description: A sensor capable of detecting RGB color, ambient light, and gestures.
  • Pins: VL, GND, VCC, SDA, SCL, INT

Arduino Nano

  • Description: A small, complete, and breadboard-friendly microcontroller board based on the ATmega328P.
  • Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK

Micro Servo 9G

  • Description: A small servo motor used for precise control of angular position.
  • Pins: GND, +5V, PWM

Wiring Details

APDS-9960 RGB and Gesture Sensor

  • GND connected to Arduino Nano GND
  • VCC connected to Arduino Nano VIN
  • SDA connected to Arduino Nano A6
  • SCL connected to Arduino Nano A5

Arduino Nano

  • GND connected to APDS-9960 GND
  • VIN connected to APDS-9960 VCC
  • A6 connected to APDS-9960 SDA
  • A5 connected to APDS-9960 SCL
  • GND connected to Micro Servo 9G GND
  • VIN connected to Micro Servo 9G +5V
  • D9 connected to Micro Servo 9G PWM

Micro Servo 9G

  • GND connected to Arduino Nano GND
  • +5V connected to Arduino Nano VIN
  • PWM connected to Arduino Nano D9

Code Documentation

#include <Wire.h>
#include <Adafruit_APDS9960.h>
#include <Servo.h>

Adafruit_APDS9960 apds;
Servo myservo;

const int servoPin = 9;
int upCount = 0;
int leftCount = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  myservo.attach(servoPin);
  myservo.write(0); // Initialize servo to 0°

  if (!apds.begin()) {
    Serial.println("Failed to initialize APDS-9960 sensor!");
    while (1);
  }
  apds.enableGesture(true);
}

void loop() {
  if (apds.gestureValid()) {
    int gesture = apds.readGesture();
    switch (gesture) {
      case APDS9960_UP:
        upCount++;
        break;
      case APDS9960_DOWN:
        myservo.write(0); // Reset servo to 0°
        upCount = 0;
        leftCount = 0;
        break;
      case APDS9960_LEFT:
        leftCount++;
        break;
      case APDS9960_RIGHT:
        // No action for RIGHT gesture
        break;
    }
    if (upCount == 2 && leftCount == 1) {
      myservo.write(90); // Rotate servo to 90°
      upCount = 0;
      leftCount = 0;
    }
  }
}

Code Explanation

  • Libraries Included:

    • Wire.h: For I2C communication.
    • Adafruit_APDS9960.h: For interfacing with the APDS-9960 sensor.
    • Servo.h: For controlling the servo motor.
  • Global Variables:

    • Adafruit_APDS9960 apds;: Object for the APDS-9960 sensor.
    • Servo myservo;: Object for the servo motor.
    • const int servoPin = 9;: Pin number for the servo motor.
    • int upCount = 0;: Counter for UP gestures.
    • int leftCount = 0;: Counter for LEFT gestures.
  • Setup Function:

    • Initializes serial communication at 9600 baud.
    • Initializes I2C communication.
    • Attaches the servo motor to pin 9 and sets its initial position to 0°.
    • Initializes the APDS-9960 sensor and enables gesture detection.
  • Loop Function:

    • Checks if a valid gesture is detected.
    • Reads the gesture and updates the counters based on the gesture type.
    • If two UP gestures followed by one LEFT gesture are detected, the servo rotates to 90°.