Search Code

Cảm biến cử chỉ APDS-9960 với Arduino

Cảm biến cử chỉ APDS-9960 với Arduino

Cảm biến cử chỉ APDS-9960 cho phép phát hiện các cử chỉ tay như vuốt và tiệm cận. Trong hướng dẫn này, chúng tôi sẽ trình bày cách kết nối APDS-9960 với Arduino và lập trình để nhận diện cử chỉ. Kết quả sẽ là một thiết lập đơn giản có thể phát hiện chuyển động và báo cáo chúng qua màn hình nối tiếp. Hướng dẫn này sẽ giúp bạn hiểu cách đấu dây và mã cần thiết để bắt đầu với cảm biến cử chỉ. (trong video tại 00:30)
APDS-9960 gesture sensor module -1

Giải Thích Phần Cứng

APDS-9960 là một cảm biến đa năng kết hợp khả năng cảm biến màu RGB, cảm biến ánh sáng xung quanh, cảm biến tiệm cận và phát hiện cử chỉ. Nó giao tiếp với Arduino qua I2C, cho phép tích hợp dễ dàng với tối thiểu dây nối. Cảm biến chỉ hoạt động ở 3.3V, vì vậy cần đảm bảo mức điện áp phù hợp để tránh làm hỏng thiết bị. Trong thiết kế này, chúng ta sẽ sử dụng Arduino để đọc dữ liệu cử chỉ. Cảm biến xuất thông tin cử chỉ qua ngắt, cho phép Arduino phản hồi nhanh chóng với chuyển động của tay. Việc tích hợp APDS-9960 với Arduino mở ra nhiều khả năng cho các dự án tương tác, bao gồm các thiết bị điều khiển bằng cử chỉ.

Chi Tiết Từ Bảng Dữ Liệu

Nhà sản xuấtBroadcom
Số bộ phậnAPDS-9960
Điện áp logic/IO2.7 - 3.6 V
Điện áp cung cấp3.3 V
Dòng điện đầu ra (mỗi kênh)1 mA
Dòng điện đỉnh (mỗi kênh)10 mA
Hướng dẫn tần số PWM100 Hz
Ngưỡng logic đầu vào0.3 x VDD (thấp), 0.7 x VDD (cao)
Sụt áp / RDS(on) / bão hòa0.5 V tối đa
Giới hạn nhiệt-40 đến +85 °C
Đóng góiLGA 6 chân
Ghi chú / biến thểHỗ trợ giao tiếp I2C

  • Đảm bảo điện áp cung cấp không vượt quá 3.6 V để tránh hư hỏng.
  • Sử dụng điện trở kéo lên trên các đường I2C để giao tiếp đáng tin cậy.
  • Giữ cảm biến tránh ánh sáng mặt trời trực tiếp để có kết quả đọc chính xác.
  • Đảm bảo khởi tạo cảm biến đúng cách trong mã.
  • Kiểm tra các kết nối dây để tránh đầu vào trôi nổi.

Hướng Dẫn Đấu Dây

Arduino wiring for APDS9960 Gesture sensor
Arduino wiring for APDS9960 Gesture sensor
Để đấu dây APDS-9960 với Arduino, bắt đầu bằng cách kết nối nguồn. Sử dụng dây màu đỏ để kết nối chân VCC của APDS-9960 với chân 3.3V trên Arduino. Tiếp theo, kết nối chân GND trên cảm biến với chân nối đất trên Arduino bằng dây màu đen. Bây giờ, cho giao tiếp I2C, kết nối chân SDA của APDS-9960 với chân A4 của Arduino. Tương tự, kết nối chân SCL với chân A5 trên Arduino. Cuối cùng, kết nối chân INT của cảm biến với chân 2 trên Arduino. Điều này sẽ cho phép Arduino phản hồi các ngắt được tạo bởi cảm biến.

Ví Dụ Mã & Hướng Dẫn Chi Tiết

Mã được cung cấp khởi tạo cảm biến APDS-9960 và thiết lập Arduino để đọc cử chỉ. Dưới đây là một đoạn trích ngắn của hàm thiết lập:
void setup() {
  pinMode(APDS9960_INT, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, interruptRoutine, FALLING);
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  }
}
Trong đoạn mã này, chúng ta khởi tạo màn hình nối tiếp và đặt chân ngắt. Cảm biến được khởi tạo và một thông báo xác nhận được in ra bảng điều khiển. Tiếp theo, chúng ta kiểm tra cử chỉ trong hàm vòng lặp:
void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}
Ở đây, vòng lặp liên tục kiểm tra cử chỉ. Khi một cử chỉ được phát hiện, ngắt bị vô hiệu hóa và hàm `handleGesture` được gọi để xử lý cử chỉ. Cuối cùng, hàm `handleGesture` xử lý các cử chỉ được phát hiện:
void handleGesture() {
  if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      // Các trường hợp bổ sung...
    }
  }
}
Trong hàm này, cử chỉ được đọc và hành động tương ứng được thực hiện dựa trên hướng được phát hiện. Điều này cho phép điều khiển tương tác dựa trên chuyển động của tay. Để hiểu đầy đủ về mã, vui lòng tham khảo chương trình hoàn chỉnh được tải bên dưới bài viết.

Trình Diễn / Điều Mong Đợi

Khi thiết lập hoàn tất, bạn có thể mong đợi Arduino đọc cử chỉ và hiển thị hướng được phát hiện trên màn hình nối tiếp. Các bài kiểm tra đơn giản bao gồm di chuyển tay lên, xuống, trái hoặc phải trước cảm biến. Nếu mọi thứ được đấu dây đúng cách, bạn sẽ thấy đầu ra tương ứng trên màn hình nối tiếp, xác nhận nhận diện cử chỉ. Hãy cẩn thận với các đầu vào trôi nổi, vì chúng có thể dẫn đến kết quả đọc không nhất quán (trong video tại 05:20).

Mốc Thời Gian Video

  • 00:00 - Giới thiệu
  • 01:15 - Tổng quan phần cứng
  • 03:00 - Hướng dẫn đấu dây
  • 04:30 - Hướng dẫn mã
  • 06:15 - Trình diễn
APDS-9960 gesture sensor -3

Hình ảnh

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
Ngôn ngữ: 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
Ngôn ngữ: 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");
    }
  }
}

Tài nguyên & tài liệu tham khảo

Chưa có tài nguyên nào.

Tập tin📁

Các tập tin khác

  • Tài liệu dữ liệu APDS9960
    đây là bảng dữ liệu cho chip APDS9960. hãy xem file này để tìm hiểu thêm về nó.
    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