Search Code

APDS-9960 senzor gestova sa Arduinom

APDS-9960 senzor gestova sa Arduinom

Senzor gesta APDS-9960 omogućava detekciju pokreta ruke, kao što su prevlačenja i približavanje. U ovom vodiču pokazat ćemo kako povezati APDS-9960 s Arduinom i programirati ga da prepoznaje geste. Rezultat će biti jednostavno postavljanje koje može otkriti pokrete i prijaviti ih putem serijskog monitora. Ovaj vodič pomoći će vam da razumijete ožičenje i kod potreban za početak rada sa senzorom gesta. (u videu na 00:30)

APDS-9960 gesture sensor module -1

Hardver objašnjen

APDS-9960 je svestran senzor koji kombinuje detekciju RGB boja, mjerenje ambijentalnog svjetla, detekciju blizine i mogućnost prepoznavanja gesti. Komunicira s Arduinom putem I2C, što omogućava laku integraciju uz minimalno ožičenje. Senzor radi samo na 3.3V, stoga je neophodno osigurati odgovarajuće razine napona kako biste izbjegli oštećenje uređaja. U ovom projektu koristit ćemo Arduino za čitanje podataka o gestama. Senzor šalje informacije o gestama putem prekida, što omogućava Arduinu da brzo odgovori na pokrete ruke. Integracija APDS-9960 s Arduinom otvara mnoge mogućnosti za interaktivne projekte, uključujući uređaje kojima se upravlja gestama.

Detalji tehničkog lista

ProizvođačBroadcom
Broj dijelaAPDS-9960
Napon logike/IO2.7 - 3.6 V
Napon napajanja3.3 V
Izlazna struja (po kanalu)1 mA
Vršna struja (po kanalu)10 mA
Smjernice za frekvenciju PWM-a100 Hz
Ulazni logički pragovi0.3 x VDD(nizak), 0.7 x VDD(visok)
Naponski pad / RDS(on)/ zasićenost0.5 V maks.
Toplotna ograničenja-40 do +85 °C
PaketLGA sa 6 pinova
Bilješke / varijantePodržava I2C komunikaciju

  • Osigurajte da napon napajanja ne prelazi 3.6 V kako biste spriječili oštećenje.
  • Koristite pull-up otpornike na I2C linijama za pouzdanu komunikaciju.
  • Držite senzor dalje od direktne sunčeve svjetlosti za tačna očitanja.
  • Pobrinite se da pravilno inicijalizirate senzor u kodu.
  • Provjerite spojeve ožičenja kako biste izbjegli plutajuće ulaze.

Upute za ožičenje

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

Da biste povezali APDS-9960 sa Arduinom, počnite povezivanjem napajanja. Koristite crvenu žicu da povežete VCC pin APDS-9960 sa 3.3V pinom na Arduinu. Zatim povežite GND pin senzora sa GND pinom na Arduinu koristeći crnu žicu. Sada, za I2C komunikaciju, povežite SDA pin APDS-9960 sa A4 pinom na Arduinu. Slično, povežite SCL pin sa A5 pinom na Arduinu. Na kraju, povežite INT pin senzora sa pinom 2 na Arduinu. Ovo će omogućiti Arduinu da reaguje na prekide koje generiše senzor.

Primjeri koda i vodič korak po korak

Priloženi kod inicijalizira APDS-9960 senzor i podešava Arduino da čita geste. Evo kratkog isječka funkcije setup:

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

U ovom isječku inicijaliziramo serijski monitor i postavljamo prekidni pin. Senzor je inicijaliziran, a poruka potvrde se ispisuje na konzolu. Zatim provjeravamo geste u funkciji loop:

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

Ovdje petlja kontinuirano provjerava geste. Kada je gesta otkrivena, prekid se onemogućava i poziva se funkcija handleGesture da obradi gestu. Na kraju, funkcija handleGesture obrađuje otkrivene geste:

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

U ovoj funkciji gesta se očitava, te se na osnovu detektovanog smjera preduzima odgovarajuća akcija. To omogućava interaktivnu kontrolu zasnovanu na pokretima ruke. Za potpuno razumijevanje koda, pogledajte kompletan program učitan ispod članka.

APDS-9960 gesture sensor -3

Demonstracija / Šta očekivati

Kada je postavljanje završeno, možete očekivati da će Arduino čitati geste i prikazivati otkriveni smjer na serijskom monitoru. Jednostavni testovi uključuju pomicanje ruke gore, dolje, lijevo ili desno ispred senzora. Ako je sve ispravno ožičeno, trebali biste vidjeti odgovarajući izlaz na serijskom monitoru, što potvrđuje prepoznavanje geste. Budite oprezni s plutajućim ulazima, jer oni mogu dovesti do nedosljednih očitavanja (u videu na 05:20).

Vremenske oznake videozapisa

  • 00:00- Uvod
  • 01:15- Pregled hardvera
  • 03:00- Upute za ožičenje
  • 04:30- Pregled koda
  • 06:15- Demonstracija

Images

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
Language: 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
Language: 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");
    }
  }
}

Resources & references

No resources yet.

Files📁

Other files

  • APDS9960 Datasheet
    this is the datasheet for APDS9960 chip. have a look at this file to learn more about it.
    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