私の入門記録であって、入門者向け解説サイトではありません。

サーボモーター

  • 投稿日:
  • Category:

サーボモーターはラジコン等やる方にはお馴染みのパーツだろうが、私は経験が無いので今回が初めての購入となった。回路は簡単で電源と信号線だけ、スケッチも標準サンプルに入っていると教科書には書いてあったが、探すのに少し手間取った(結局、メニューをスクロールして行った一番下の方で発見)

[ スケッチ:Sweep]

#include <Servo.h>
Servo myservo;  // create servo object
// 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 'pos'
    delay(15);                       // waits 15ms for the servo to reach pos
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to 'pos'
    delay(15);                       // waits 15ms for the servo to reach pos
  }
}

サンプル動作はゼロから180度まで、1度づつ15ミリ秒おきに信号を送り往復するというもの。サーボモーターには何種類か回転軸に付けるパーツがついているが、一番シンプルなレバーを嵌め込んで動作確認を行なった。