# Arduino筆記 04. 伺服馬達(Servo)測試 ## 實驗項目: 測試如何控制Servo ## 學習重點: ## 實驗電路: ## 程式碼: ```cpp= #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 } } ``` > https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep > ## 延伸學習: * N/A ## 參考資料: [Arduino 官網Reference > Libraries > Servo](https://www.arduino.cc/reference/en/libraries/servo/) ![](https://i.imgur.com/aTv1g0w.png)