隔空挥手翻页这种操作,靠的就是一颗指甲盖大小的芯片——APDS-9960。它把环境光、RGB颜色、接近检测和上下左右远近六种手势识别装进了同一个3.3V的小封装里,I2C两根线就能驱动。这篇把这颗芯片一次拆透:每种功能靠什么原理实现、库怎么装、示例代码怎么跑、手势识别的坑都在哪。

一颗芯片怎么装下四种功能
板子正面这颗黑色的小元件是核心:内部集成了IR LED和四组光电二极管阵列。手势识别就是靠这四组二极管测量反射红外光到达的时间差和强度差,判断手往哪个方向挥;接近检测看反射红外强度;RGB则直接对反射光做四通道分色采样。芯片信号处理全在片内完成,主控只管通过I2C读结果:

引脚一共六个:VL可以外接红外补光(一般不用)、GND、VCC、SDA、SCL、INT中断输出。特别提醒:这颗芯片只能吃3.3V,接5V会把I2C总线一起拖挂,UNO用户要么用3.3V板,要么上电平转换。
接线与库
按官方hookup guide的接法,UNO用A4/A5做I2C数据与时钟,INT接D2中断脚:

库装SparkFun_APDS-9960_RGB_and_Gesture_Sensor这一份,GitHub上下载解压丢进libraries目录即可。它同时覆盖手势、接近、RGB和环境光四套API。
跑官方GestureTest示例
库自带GestureTest示例,逻辑是INT中断触发后进入handleGesture(),逐个识别挥手方向:
language:c
/****************************************************************
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 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 above (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"
// 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() {
// 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");
}
}
}
烧进去打开串口监视器(9600),在传感器上方10到20厘米处慢慢挥手,LEFT、UP、DOWN、RIGHT这些词就会一行行蹦出来:

在传感器上方悬停一秒再靠近,识别NEAR;贴近后停留一秒再移远,识别FAR。这套逻辑全靠库内部的状态机,自己一行都不用写。
手势识别的实战心得
这颗芯片好玩但娇气。挥手要慢,快了四组二极管来不及采样;距离别贴太近,5厘米内反射太强反而乱判;环境光强的白天窗口边,误触发率明显升高,加深色外壳只留个窗口效果立竿见影。RGB和接近模式用readProximity()、readAmbientLight()、readRedLight()这些接口逐个读就行,和手势引擎分时使能不冲突。拿它做个隔空控制的音乐盒、翻页器或者无接触开关,都是经典项目,一颗芯片基本够你折腾一个月。











挥手要慢这条是精髓,我第一次玩就是挥太快,出来的方向全靠猜。放慢到半秒一次基本次次准,深色纸盒开窗那招确实管用。
5V的坑替大家踩过了,直接怼5V上去I2C再也扫不到设备,换3.3V板子立马复活。这芯片真的娇贵,新手务必看清供电。
NEAR和FAR那两个动作描述写得清楚,悬停一秒再靠近就算NEAR,之前看库文档一直没看懂触发条件,照这个试一次就通。
拿它做了个隔空翻页的PPT遥控器,PY串口发给电脑就完事,会议室效果拔群。RGB颜色读取还没玩,回头补上。