This tutorial is part of: 伺服电机
这里列出了所有与伺服电机相关的视频。其他视频的链接在本文下方。
用手勢控制Arduino的伺服器位置
喺呢個教學入面,我哋會學點樣用手勢控制伺服馬達。透過將手向左或向右移動,你可以令伺服馬達作出相應嘅反應。呢個項目利用APDS-9960手勢傳感器來檢測手部動作,並將佢哋轉換成伺服位置。想要詳細嘅視覺解釋,記得去睇下視頻(喺視頻入面喺00:30)。

我們將使用的核心組件包括伺服電機和 APDS-9960 手勢傳感器。伺服電機會根據檢測到的手勢旋轉到特定角度,而 APDS-9960 傳感器會解讀手部動作。這個設置讓人可以直觀地控制伺服電機,而無需實體接觸。
硬件解说
伺服馬達係一個細小嘅裝置,可以轉動到特定角度,適合用於需要精確定位嘅應用。佢通常有三條線:電源(紅色)、接地(黑色)同信號(橙色)。信號線接收命令,令伺服馬達移動到指定角度。
APDS-9960 係一個多功能嘅感應器,可以檢測手勢、環境光同顏色。佢透過 I2C 同 Arduino 通訊,需要幾個引腳來供電同數據傳輸。呢個感應器可以檢測到上、下、左、右等動作,我哋會用嚟控制伺服器。
數據表詳情
| 製造商 | 博通 |
|---|---|
| 零件编号 | APDS-9960 |
| 邏輯/輸入輸出電壓 | 2.4 - 3.6 V |
| 供應電壓 | 2.4 - 3.6 V |
| 每個通道的輸出電流 | 最大20毫安 |
| PWM 頻率指導 | 不適用 |
| 輸入邏輯閾值 | 0.3 伏 (低), 0.7 伏 (高) |
| 電壓降 / RDS(通)/ 飽和度 | 不適用 |
| 熱限制 | -40至85°C |
| 包裹 | 6.0 x 3.0 毫米 |
| 備註 / 變體 | 手勢感應,RGB燈光感應 |
- 確保伺服器有足夠的電壓供應(通常是5V)。
- 將 APDS-9960 透過 I2C 通訊連接到 Arduino。
- 留意手勢感應器嘅中斷引腳。
- 如有需要,使用上拉電阻以確保信號檢測穩定。
- 確保喺唔同嘅光線條件下校準感應器,以提高準確性。

要接線部件,首先從伺服馬達開始。將紅線連接到Arduino的5V引腳,黑線連接到地(GND),橙色信號線連接到引腳。9呢個會令Arduino可以控制伺服器嘅位置。
接住,接線APDS-9960手勢感應器。連接個V_N釘到Arduino的3.3V,然後GND釘住地面。該INT引腳應該連接到引腳2在Arduino上,當...SDA和SCL針去針A4和A5,分别。这个配置让传感器可以有效地同Arduino沟通。
代碼示例及步驟講解
喺設置函數入面,我哋初始化伺服器同手勢感應器。嗰行myservo.attach(9);將伺服器綁定到我們之前連接的9號針腳。這樣可以讓伺服器接收來自Arduino的指令。

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);
}接住,我哋定義系統點樣喺循環函數入面處理手勢。個方法handleGesture();檢查可用嘅手勢並執行相應嘅動作。例如,如果檢測到嘅手勢係左邊,伺服器就會移動到180度。
void handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_LEFT:
myservo.write(180); // added by RoboJax
break;
case DIR_RIGHT:
myservo.write(0); // added by RoboJax
break;
}
}
}最後,循環不斷檢查手勢,並根據檢測到的手勢更新伺服器位置。確保在不同的光照條件下測試設置,以獲得準確的手勢識別。
示範 / 期待什麼
一旦所有嘢都接好線同埋代碼上載完,你應該可以透過將手向左或向右移動來控制伺服器。伺服器會因為左邊手勢而轉動到180度,因為右邊手勢而返回0度。如果冇檢測到手勢,檢查下感應器嘅位置同埋周圍嘅光線條件(喺視頻05:30)。
章節
- 介紹 - :00
- 硬件概述 - 01:30
- 接線指示 - 03:00
- 代碼解釋 - 04:30
- 示範 - 06:00
This tutorial is part of: 伺服电机
- Controlling a Servo with Push Buttons Using Arduino
- Control a Servo Motor with a Push Button: Move Servo and Return SPB-1
- Control a Servo Motor with a Push Button: Move Servo in One Direction SPB-2
- Controlling a Servo Motor with a Push Button: Move Servo While Button Is Pressed (SPB-3)
- Controlling a Servo with a Potentiometer Using Arduino
- Controlling a Servo with Potentiometer and LCD1602 using Arduino
- Controlling Servo Motors Using an Infrared Remote with Arduino
- 使用電位器控制Arduino伺服馬達
- Controlling Two or More Servos with Potentiometers Using an Arduino
- How to Control a 360° Servo with Three Push-Button Switches
- How to Use Continuous 360° Servo with Arduino
- Arduino Code and Video for PCA9685 16-Channel 12-Bit Servo Controller V1
- Build an Arduino Servo Toggle Switch with a Push Button
++
/****************************************************************
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");
}
}
}
資源與參考資料
暫時冇資源。
文件📁
冇文件可用。