Search Code

APDS-9960 手勢感測器配合 Arduino

APDS-9960 手勢感測器配合 Arduino

APDS-9960手勢感測器可以偵測到手部手勢,例如揮動同近接感應。喺呢個教學入面,我哋會示範點樣將APDS-9960連接到Arduino,並編寫程式去辨識手勢。最後嘅結果會係一個簡單嘅設置,可以偵測到動作並透過序列監視器報告出嚟。呢個指南會幫你了解接線同程式碼,等你可以開始使用呢個手勢感測器。(喺影片00:30)
APDS-9960 gesture sensor module -1

硬件解說

APDS-9960係一個多功能感測器,結合咗RGB顏色感應、環境光感應、近接感應同手勢偵測功能。佢透過I2C同Arduino通訊,令到整合變得簡單,接線亦都好少。呢個感測器只係喺3.3V下運作,所以一定要確保電壓水平正確,避免損壞裝置。 喺呢個製作入面,我哋會用Arduino去讀取手勢數據。感測器透過中斷輸出到手勢資訊,令到Arduino可以即時對手部動作作出反應。將APDS-9960同Arduino整合,可以開啟好多互動項目嘅可能性,包括手勢控制裝置。

數據表詳細資料

製造商Broadcom
零件編號APDS-9960
邏輯/IO電壓2.7 - 3.6 V
供電電壓3.3 V
輸出電流(每通道)1 mA
峰值電流(每通道)10 mA
PWM頻率指引100 Hz
輸入邏輯閾值0.3 x VDD(低),0.7 x VDD(高)
電壓降 / RDS(on) / 飽和最大0.5 V
熱限制-40至+85 °C
封裝6針LGA
備註 / 變體支援I2C通訊

  • 確保供電電壓唔好超過3.6 V,以免損壞。
  • 喺I2C線路上使用上拉電阻,確保通訊可靠。
  • 將感測器遠離直接陽光,以獲得準確讀數。
  • 確保喺程式碼入面正確初始化感測器。
  • 檢查接線連接,避免浮動輸入。

接線指示

Arduino wiring for APDS9960 Gesture sensor
Arduino wiring for APDS9960 Gesture sensor
要將APDS-9960連接到Arduino,首先連接電源。用一條紅色線將APDS-9960嘅VCC針連接到Arduino嘅3.3V針。跟住,用一條黑色線將感測器嘅GND針連接到Arduino嘅接地針。 而家,對於I2C通訊,將APDS-9960嘅SDA針連接到Arduino嘅A4針。同樣,將SCL針連接到Arduino嘅A5針。最後,將感測器嘅INT針連接到Arduino嘅針2。咁樣就可以令到Arduino對感測器產生嘅中斷作出反應。

程式碼示例同講解

提供嘅程式碼初始化APDS-9960感測器,並設置Arduino去讀取手勢。以下係setup函數嘅簡短摘錄:
void setup() {
  pinMode(APDS9960_INT, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, interruptRoutine, FALLING);
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  }
}
喺呢段程式碼入面,我哋初始化序列監視器並設置中斷針。感測器被初始化,並喺控制台打印確認訊息。 跟住,我哋喺loop函數入面檢查手勢:
void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}
喺呢度,loop會持續檢查手勢。當偵測到手勢時,中斷會被停用,並呼叫`handleGesture`函數去處理手勢。 最後,`handleGesture`函數會處理偵測到手勢:
void handleGesture() {
  if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      // 其他情況...
    }
  }
}
喺呢個函數入面,手勢會被讀取,並根據偵測到嘅方向採取相應動作。咁樣就可以根據手部動作進行互動控制。 如果想完整了解程式碼,請參考文章下面載入嘅完整程式。

示範 / 預期效果

當設置完成後,你可以預期Arduino會讀取手勢,並喺序列監視器上顯示偵測到嘅方向。簡單測試包括將手喺感測器前面向上、向下、向左或向右移動。如果接線正確,你應該會喺序列監視器上見到相應輸出,確認手勢辨識成功。要小心浮動輸入,因為佢哋可能導致讀數唔一致。(喺影片05:20)

影片時間戳

  • 00:00 - 介紹
  • 01:15 - 硬件概覽
  • 03:00 - 接線指示
  • 04:30 - 程式碼講解
  • 06:15 - 示範
APDS-9960 gesture sensor -3

圖片

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

你可能需要嘅嘢

資源與參考資料

暫時冇資源。

文件📁

其他文件

  • APDS9960 数据表
    呢個係APDS9960晶片嘅數據表。睇下呢個文件了解多啲。
    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