Search Code

Czujnik gestów APDS-9960 z Arduino

Czujnik gestów APDS-9960 z Arduino

Czujnik gestów APDS-9960 umożliwia wykrywanie gestów dłoni, takich jak przesunięcia i bliskość. W tym samouczku pokażemy, jak podłączyć APDS-9960 do Arduino i zaprogramować go, aby rozpoznawał gesty. Efektem będzie prosta konfiguracja, która może wykrywać ruchy i zgłaszać je za pośrednictwem monitora szeregowego. Ten przewodnik pomoże Ci zrozumieć okablowanie i kod potrzebny do rozpoczęcia pracy z czujnikiem gestów. (w wideo o 00:30)

APDS-9960 gesture sensor module -1

Wyjaśnienie sprzętu

APDS-9960 to wszechstronny czujnik, który łączy w sobie funkcje wykrywania koloru RGB, wykrywania światła otoczenia, wykrywania bliskości i rozpoznawania gestów. Komunikuje się z Arduino za pośrednictwem I2C, co umożliwia łatwą integrację przy minimalnym okablowaniu. Czujnik działa tylko na 3,3V, co sprawia, że niezbędne jest zapewnienie odpowiednich poziomów napięcia, aby uniknąć uszkodzenia urządzenia. W tej budowie użyjemy Arduino do odczytu danych dotyczących gestów. Czujnik przekazuje informacje o gestach za pośrednictwem przerwań, co pozwala Arduino na szybką reakcję na ruchy ręki. Integracja APDS-9960 z Arduino otwiera wiele możliwości dla interaktywnych projektów, w tym urządzeń sterowanych gestami.

Szczegóły karty danych

ProducentBroadcom
Numer katalogowyAPDS-9960
Napięcie logiczne/IO2,7 - 3,6 V
Napięcie zasilające3,3 V
Prąd wyjściowy (na kanał)1 mA
Prąd szczytowy (na kanał)10 mA
Wskazówki dotyczące częstotliwości PWM100 Hz
Progi logiki wejściowej0,3 x VDD(niski), 0,7 x VDD(wysoki)
Spadek napięcia / RDS(on)/ nasycenie0,5 V max
Granice termalne-40 do +85 °C
Paczka6-pin LGA
Uwagi / wariantyObsługuje komunikację I2C

  • Upewnij się, że napięcie zasilania nie przekracza 3,6 V, aby zapobiec uszkodzeniom.
  • Użyj rezystorów podciągających na liniach I2C, aby zapewnić niezawodną komunikację.
  • Trzymaj czujnik z dala od bezpośredniego światła słonecznego dla dokładnych pomiarów.
  • Upewnij się, że sensor jest poprawnie zainicjowany w kodzie.
  • Sprawdź połączenia okablowania, aby uniknąć pływających sygnałów wejściowych.

Instrukcje podłączenia

Arduino wiring for APDS9960 Gesture sensor
Arduino wiring for APDS9960 Gesture sensor

Aby podłączyć APDS-9960 do Arduino, najpierw podłącz zasilanie. Użyj czerwonego przewodu, aby połączyć pin VCC APDS-9960 z pinem 3.3V na Arduino. Następnie podłącz pin GND czujnika do pinu masy na Arduino, używając czarnego przewodu. Teraz, aby nawiązać komunikację I2C, podłącz pin SDA APDS-9960 do pinu A4 na Arduino. Podobnie, podłącz pin SCL do pinu A5 na Arduino. Na koniec, podłącz pin INT czujnika do pinu 2 na Arduino. Dzięki temu Arduino będzie mogło reagować na przerwania generowane przez czujnik.

Przykłady kodu i przewodnik

Podany kod inicjalizuje sensor APDS-9960 i konfiguruje Arduino do odczytu gestów. Oto krótki fragment funkcji ustawiającej:

void setup() {
  pinMode(APDS9960_INT, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, interruptRoutine, FALLING);
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  }
}

W tym fragmencie inicjalizujemy monitor szeregowy i ustawiamy pin przerwania. Czujnik jest inicjalizowany, a komunikat potwierdzający jest drukowany na konsoli. Następnie sprawdzamy gesty w funkcji pętli:

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

Tutaj pętla nieprzerwanie sprawdza gesty. Gdy zostanie wykryty gest, przerwanie jest wyłączane, a funkcja `handleGesture` jest wywoływana w celu przetworzenia gestu. Na koniec funkcja `handleGesture` przetwarza wykryte gesty:

void handleGesture() {
  if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      // Additional cases...
    }
  }
}

W tej funkcji odczytywany jest gest, a następnie podejmowana jest odpowiednia akcja w zależności od wykrytego kierunku. Umożliwia to interaktywną kontrolę na podstawie ruchów rąk. Aby w pełni zrozumieć kod, proszę zapoznać się z pełnym programem załadowanym poniżej artykułu.

APDS-9960 gesture sensor -3

Demonstracja / Czego się spodziewać

Gdy konfiguracja jest zakończona, można oczekiwać, że Arduino zarejestruje gesty i wyświetli wykryty kierunek na monitorze szeregowym. Proste testy obejmują poruszanie ręką w górę, w dół, w lewo lub w prawo przed czujnikiem. Jeśli wszystko jest poprawnie podłączone, powinieneś zobaczyć odpowiadające wyjście na monitorze szeregowym, potwierdzające rozpoznawanie gestów. Uważaj na pływające sygnały, ponieważ mogą prowadzić do niejednoznacznych odczytów (wideo o 05:20).

Zamki czasowe wideo

  • 00:00- Wprowadzenie
  • 01:15- Przegląd sprzętu
  • 03:00- Instrukcje dotyczące okablowania
  • 04:30- Przegląd kodu
  • 06:15- Demonstracja

Obrazy

APDS-9960 gesture sensor module -2
APDS-9960 gesture sensor module -2
APDS-9960 gesture sensor module -1
APDS-9960 gesture sensor module -1
APDS-9960 gesture sensor -3
APDS-9960 gesture sensor -3
Arduino wiring for APDS9960 Gesture sensor
Arduino wiring for APDS9960 Gesture sensor
1-Code for an APDS-9960 gesture sensor for Arduino
Język: C++
/*
 * Code for APDS-9960 Gesture sensor for Arduino
 * This code is to detect gestures of your hand, either moving it up, down, left, or right; other gestures.
 * can be detected and used to control something else.

 * Watch the APDS-9960 Gesture sensor video https://youtu.be/qzSgZV_fbxI
 * Get this code from https://robojax.com


 * Code used in video by Ahmad Shamshiri for Robojax.com
 * on December 31, 2016, at 6:53 AM in Ajax, Ontario, Canada
  * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purposes only.

GestureTest.ino
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

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 then move it
away.

To perform a FAR gesture, hold your hand within 2 inches of the
sensor for at least 1 second and then move it away (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

Resources:
Include Wire.h and SparkFun_APDS-9960.h

Development environment specifics:
Written in Arduino 1.0.5
Tested with SparkFun Arduino Pro Mini 3.3V

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 <SparkFun_APDS9960.h>
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() {

  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  
  // 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 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}
2-Source code for controlling a servo with your hand
Język: C++
/****************************************************************
Modified for RoboJax by A.B.S. on May 09, 2017
in Ajax, Ontario, Canada. www.RoboJax.com


Original Source:
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor


Distributed as-is; no warranty is given.
****************************************************************/

#include <Wire.h>

// added by RoboJax
#include <Servo.h>

#include <SparkFun_APDS9960.h>


Servo myservo;  // create servo object to control a servo // added by RoboJax

// 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(9);  // attaches the servo on pin 9 to the servo object
  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  
  // 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 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        myservo.write(180);  // added by RoboJax
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        myservo.write(0);   // added by RoboJax
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

Rzeczy, których możesz potrzebować

Zasoby i odniesienia

Brak zasobów.

Pliki📁

Inne pliki

  • APDS9960 Karta katalogowa
    to jest karta katalogowa układu APDS9960. zapoznaj się z tym plikiem, aby dowiedzieć się więcej na jego temat.
    robojax-gesture-APDS9960_datasheet.pdf 0.89 MB
  • APDS9960 Gesture Library
    Arduino library for the APDS9960 gesture, proximity, and color sensor. Provides functions to initialize the sensor, read gesture data, and configure detection thresholds. Supports I2C interface and operates at 3.3V.
    robojax-APDS9960-gesture-library.zip 0.30 MB
  • APDS9960 Gesture Datasheet
    Datasheet for the APDS9960 digital proximity, ambient light, RGB and gesture sensor. Features I2C interface, gesture detection with directional output, and operates at 2.4-3.6V.
    robojax-APDS9960-gesture-datasheet.pdf 0.89 MB
  • APDS-9960 Gesture sensor Library
    from Robojax.com
    robojax-gesture-APDS9960-Library.zip 0.30 MB