This tutorial is part of: 伺服电机
这里列出了所有与伺服电机相关的视频。其他视频的链接在本文下方。
使用電位器控制Arduino伺服馬達
喺呢個教學入面,我哋會探討點樣用一個電位器同Arduino控制伺服馬達。目標係創建一個簡單嘅電路,根據電位器嘅旋轉調整伺服嘅位置。到呢個項目完成時,你會有一個運行中嘅模型,當你轉動電位器時,伺服角度會平滑地由0度變到180度。呢個係了解Arduino中類比輸入同伺服控制基礎嘅好方法。
為了達成這個目標,我們將使用一塊Arduino板、一個伺服電機和一個電位器。電位器作為可變電阻,為Arduino提供模擬輸入,然後Arduino會根據這個輸入來決定伺服電機的位置。這個項目不僅有趣,還具有教育意義,因為它展示了電子學和編程中的關鍵概念。想要更深入的解釋,記得查看視頻(在視頻的00:00)。
硬件解说
喺呢個項目入面,主要組件係Arduino板、伺服馬達同埋電位器。Arduino係操作嘅大腦,處理輸入同控制輸出。伺服馬達會根據佢收到嘅命令實際移動。電位器可以係1k歐姆到1M歐姆,容許用戶提供一個可變輸入,轉化為伺服運動。
伺服馬達透過接收一個PWM(脈衝寬度調變)信號來運作,這個信號指示所需的位置。當電位器轉動時,會改變其電阻,Arduino會將其讀取為一個類比電壓。伺服馬達以5V運作,唔可以用3.3V。這個電壓然後被映射到伺服馬達的運動範圍,允許從0度到180度的平滑運動。
SG90伺服電機的數據表詳情
| 製造商 | 視差 |
|---|---|
| 零件號碼 | SG90 |
| 邏輯/輸入輸出電壓 | 5 伏 |
| 供應電壓 | 4.8-6 伏 |
| 每個通道的輸出電流 | 約500毫安 |
| 每個通道的峰值電流 | 1 A |
| PWM 頻率指導 | 50 赫兹 |
| 輸入邏輯閾值 | 0.5 伏 (低), 2.5 伏 (高) |
| 電壓降 / RDS(開)/ 飽和度 | 0.5 伏特喺 500 毫安 |
| 熱限制 | 0°C至60°C |
| 包裹 | 標準9克伺服器 |
| 備註 / 變體 | 常用於遙控應用 |
- 確保伺服器唔好超過佢嘅最大電流額定值。
- 如果伺服器在高負載下持續運行,請使用散熱器。
- 將電位器連接到模擬引腳以獲得準確的讀數。
- 對伺服控制嘅PWM信號時序要小心。
- 檢查伺服器的運動範圍,以避免物理障礙。
接線指示


對於這個設置,你需要按以下方式連接各個組件:
將電位器的中間引腳(滑動觸點)連接到Arduino的模擬引腳A0. 另外兩個電位器的引腳應該連接到5V和GND喺Arduino上。對於伺服馬達,將紅色線連接到5V,黑色的線接到GND,黃色或白色的線接到數字引腳9喺Arduino上。呢個安排容許電位器提供一個類比輸入畀Arduino,Arduino會根據呢個輸入控制伺服馬達嘅位置。
確保檢查所有連接,並確保伺服器在其運動範圍內沒有被阻礙。如果使用不同的Arduino型號,請確認模擬輸入和伺服器輸出的引腳分配是兼容的。
代碼示例及步驟說明
以下係初始化伺服器同讀取電位器值嘅代碼摘錄:
#include
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
喺呢段代碼入面,我哋引入咗 Servo 函式庫,並創建咗一個叫做 servo 嘅伺服物件。myservo伺服器連接到針腳9,咁樣可以讓我哋用呢個針腳去控制佢。
void loop() {
int sensorValue = analogRead(A0); // read the potentiometer
pos = map(sensorValue, 0, 1023, 0, 180); // map to servo position
myservo.write(pos); // move servo to position
delay(15); // waits 15ms for the servo to reach the position
}
喺循環函數入面,我哋從引腳讀取電位器嘅值。A0將其範圍(0-1023)映射到伺服器的範圍(0-180度)。myservo.write(pos)命令然後根據電位器的旋轉將伺服器移動到所需的位置。
示範 / 期待什麼
當你運行這個程序時,你應該會看到伺服電機隨著你轉動電位器而平滑地從 0 移動到 180 度。如果伺服電機沒有按預期移動,請檢查接線並確保電位器正常運作。此外,確保 Arduino 正確供電,並且代碼已無錯誤地上傳。你可以期待看到伺服電機實時反映電位器的位置(在視頻的 02:00)。
視頻時間戳
- 00:00- 項目介紹
- 01:30- 硬件設置說明
- 03:45- 接線指示
- 05:00- 代碼走查
- 07:30- 完成項目的示範
This tutorial is part of: 伺服电机
- 用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)
- 用Arduino控制电位器控制舵机
- 用Arduino控制舵机配合电位器同LCD1602
- 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
- 如何使用Arduino控制连续360°舵机
- Arduino Code and Video for PCA9685 16-Channel 12-Bit Servo Controller V1
- 建立一個用按鈕控制嘅Arduino伺服馬達切換開關
/**
*Original source: Arduino IDE
* Arduino Project: Controlling the servo position
* This code is used with a video tutorial for Robojax.com
* Published on April 24, 2017, from Ajax, ON, Canada.
* Written/Edited by Ahmad Shamshiri
* Watch the video on YouTube: https://youtu.be/kZ9b31gVemc
* View tutorial page for this code https://robojax.com/RJT8
* Controlling a servo position using a potentiometer (variable resistor)
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
資源與參考資料
暫時冇資源。
文件📁
数据表 (pdf)
-
sg90_serveo_motor_datasheet
sg90_serveo_motor_datasheet.pdf0.13 MB