在本Arduino伺服電機(jī)教程中,您將學(xué)習(xí)如何使用Arduino UNO板和電位器控制伺服電機(jī)。
一旦你啟動(dòng)并運(yùn)行了這個(gè)示例項(xiàng)目,你就掌握了從Arduino控制伺服電機(jī)的技能。這可以成為更先進(jìn)的機(jī)器人項(xiàng)目的基礎(chǔ)。
您需要的組件:
Arduino的板
5V伺服電機(jī)(如SG90)
電位器(1k及以上的任意值)
面包板和跳線
電路設(shè)置:
將舵機(jī)的電源線(通常為紅色)連接到 Arduino 上的 5V 輸出。
將舵機(jī)的接地電纜(通常為棕色或黑色)連接到 Arduino 上的一個(gè) GND 引腳。
將舵機(jī)的信號(hào)線(通常為橙色或黃色)連接到 Arduino 上的數(shù)字引腳 9。
將電位器插入試驗(yàn)板,并將其一個(gè)外部引腳連接到 5V,另一個(gè)外部引腳連接到 Arduino 上的 GND。
將電位計(jì)的中間引腳連接到Arduino上的模擬引腳A0。
示例代碼 1:自動(dòng)伺服運(yùn)動(dòng)
首先,要測(cè)試伺服電機(jī)是否工作,請(qǐng)上傳以下測(cè)試代碼。上傳后,伺服電機(jī)應(yīng)開始在 0 到 180 度之間來(lái)回移動(dòng)。確保您的舵機(jī)連接到引腳
9,如電路圖所示。
#include < Servo.h > // Include the Servo library
Servo myservo; // Create a servo object to control the servo motor
void setup() {
myservo.attach(9); // Attaches the servo on pin 9 to the servo object
}
void loop() {
// Goes from 0 degrees to 180 degrees in steps of 1 degree
for (int pos = 0; pos <= 180; pos += 1) {
myservo.write(pos); // Tell servo to go to position in variable 'pos'
delay(15); // Waits for the servo to reach the position
}
// Goes from 180 degrees to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos); // Tell servo to go to position in variable 'pos'
delay(15); // Waits for the servo to reach the position
}
}
上傳代碼:
通過(guò) USB 數(shù)據(jù)線將 Arduino UNO 連接到計(jì)算機(jī)。
在計(jì)算機(jī)上打開Arduino IDE。
將提供的代碼復(fù)制到新草圖中。
在“工具”菜單下選擇正確的主板和端口。
單擊“上傳”按鈕將代碼傳輸?shù)紸rduino。
示例代碼 2:使用電位計(jì)控制伺服
此示例草圖使用電位計(jì)來(lái)控制伺服電機(jī)的位置。當(dāng)您轉(zhuǎn)動(dòng)電位器時(shí),伺服電機(jī)應(yīng)相應(yīng)地移動(dòng)到電位器電阻所指示的位置。
此代碼基于基本的Arduino電位器示例。
#include < Servo.h >
Servo myservo; // Servo object to control the motor
int potpin = A0; // Where the potentiometer is connected
int val; // Variable to read the potentiometer value
void setup() {
// Tell the servo object which pin to use
myservo.attach(9);
}
void loop() {
// Read the value of the potentiometer (value between 0 and 1023)
val = analogRead(potpin);
// Scale it to use it with the servo (value between 0 and 180)
val = map(val, 0, 1023, 0, 180);
// Set the servo position according to the scaled value
myservo.write(val);
// Wait for the servo to reach the position
delay(15);
}
上傳代碼后,轉(zhuǎn)動(dòng)電位器將改變電阻和Arduino讀取的模擬值,從而控制伺服電機(jī)的位置。
現(xiàn)在,您可以對(duì)位置進(jìn)行試驗(yàn),并了解伺服電機(jī)如何響應(yīng)電位計(jì)的調(diào)整。
Arduino伺服電機(jī)故障排除
如果伺服電機(jī)沒(méi)有響應(yīng):
根據(jù)原理圖檢查所有連接。
確保在IDE中正確選擇了Arduino板。
確保試驗(yàn)板上沒(méi)有短路。
確保您的舵機(jī)在 5V 下工作
審核編輯:陳陳
-
電位器
+關(guān)注
關(guān)注
14文章
1015瀏覽量
67333 -
伺服電機(jī)
+關(guān)注
關(guān)注
85文章
2079瀏覽量
58700 -
Arduino
+關(guān)注
關(guān)注
188文章
6481瀏覽量
189046
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
如何使用Arduino的藍(lán)牙控制伺服電機(jī)

三電阻FOC電機(jī)控制板電位器調(diào)速是如何移植的?
如何使用Arduino開發(fā)板通過(guò)藍(lán)牙方式控制伺服電機(jī)
Arduino UNO利用電位器模擬輸量輸入控制步進(jìn)電機(jī)調(diào)速 相關(guān)資料下載
如何在arduino中利用電位器控制舵機(jī)
請(qǐng)問(wèn)Arduino UNO如何利用電位器模擬輸量輸入控制步進(jìn)電機(jī)調(diào)速?
基于Arduino UNO和HC-05藍(lán)牙模塊控制伺服電機(jī)

用于Arduino UNO兼容板的3個(gè)步進(jìn)電機(jī)控制器載板

使用Arduino Uno和POT控制伺服電機(jī)

基于Arduino UNO的手勢(shì)控制伺服電機(jī)
用Arduino和電位器控制伺服電機(jī)的位置

使用電位器和Arduino實(shí)現(xiàn)步進(jìn)電機(jī)控制

使用Arduino控制伺服電機(jī)

使用電位器和Arduino控制LED亮度

評(píng)論