Arduino 伺服馬達

tags: Arduino

目錄

library

#include <Servo.h>

圖片

電路

  • 電源線:通常為紅色,應連接到 Arduino 板上的 5V 腳位
  • 接地線:常為黑色或棕色,應連接到 Arduino 板上的接地腳位(GND)
  • 信號線:通常為黃色、橙色或白色,應連接到 Arduino 板上的數字腳位

順便一提的,單顆伺服馬達在運作時,所需電流大約是 300mA。如果你需要 2 顆以上的伺服馬達同時運作,就要外接高電流的變壓器來供電,不然就會經常出現抖動的情況。

Servo - attach()

Attach the Servo variable to a pin.

servo.attach(pin); servo.attach(pin, min, max);
  • min:最小 0 度
  • max:最大 180 度
  • pin:腳位

Servo - write()

Writes a value to the servo, controlling the shaft(軸) accordingly. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement.

  • 多搭配迴圈讓伺服馬達進行轉動
  • 和 delay() 控制轉速
servo.write(angle);
  • \(0 <\) angle \(< 180\)

example:

#include <Servo.h> Servo myservo; // 宣告馬達 void setup() { myservo.attach(9); myservo.write(90); } void loop() {}

Servo - writeMicroseconds()

Writes a value in microseconds (us) to the servo, controlling the shaft accordingly. On standard servos a parameter(參數) value of 1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.

  • 較常使用 write()
servo.writeMicroseconds(us)
  • \(1000 <\) us \(< 2000\)

Servo - read()

回傳馬達當前的角度

servo.read()
  • \(0 <\) 回傳角度 \(< 180\)

Servo - attached()

回傳馬達是否 attach 到腳位(布林值),如果有就回傳 true,否則回傳 false

servo.attached()

Servo - detach()

將馬達從腳位分離

servo.detach()

參考資料

Select a repo