Arduino:SG90伺服馬達實作
今天快速記錄下 SG90 伺服馬達的 arduino 程式碼。
Tinkercad 的模擬程式範例
PIN 腳位定義:
- 橘色:控制訊號
- 紅色:輸入電源 +(3~7.2V)
- 棕色:共同接地
規格:
- Weight: 9g
- Dimension: 23×12.2x29mm
- Stall torque: 1.8kg/cm(4.8v)
- Gear type: POM gear set
- Operating speed: 0.12 sec/60degree(4.8v)
- Operating voltage: 4.8v~7.2V
- Temperature range: 0℃_ 55℃
- Dead band width: 1us
- Power Supply: Through External Adapter
- servo wire length: 25 cm
- Servo Plug: JR (Fits JR and Futaba)
- 轉動角度:最大90°
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> //載入函式庫,這是內建的,不用安裝 | |
Servo myservo; // 建立SERVO物件 | |
void setup() { | |
myservo.attach(9); // 設定要將伺服馬達接到哪一個PIN腳 | |
} | |
void loop() { | |
myservo.write(0); //旋轉到0度,就是一般所說的歸零 | |
delay(1000); | |
myservo.write(90); //旋轉到90度 | |
delay(1000); | |
myservo.write(180); //旋轉到180度 | |
delay(1000); | |
myservo.write(90); | |
delay(1000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sweep | |
by BARRAGAN <http://barraganstudio.com> | |
This example code is in the public domain. | |
modified 8 Nov 2013 by Scott Fitzgerald | |
http://www.arduino.cc/en/Tutorial/Sweep | |
*/ | |
#include <Servo.h> | |
int pos = 0; | |
Servo servo_9; | |
void setup() | |
{ | |
servo_9.attach(9, 500, 2500); | |
} | |
void loop() | |
{ | |
// sweep the servo from 0 to 180 degrees in steps | |
// of 1 degrees | |
for (pos = 0; pos <= 180; pos += 1) { | |
// tell servo to go to position in variable 'pos' | |
servo_9.write(pos); | |
// wait 15 ms for servo to reach the position | |
delay(15); // Wait for 15 millisecond(s) | |
} | |
for (pos = 180; pos >= 0; pos -= 1) { | |
// tell servo to go to position in variable 'pos' | |
servo_9.write(pos); | |
// wait 15 ms for servo to reach the position | |
delay(15); // Wait for 15 millisecond(s) | |
} | |
} |
參考 Tinkercad 的模擬程式
留言
張貼留言