Servo Motor(SG90) JP

아두위키 : Arduwiki

가운데|class=coders100

信号に応じて回転角度を調整できる部品です。

正確な位置制御が可能で、ロボットの関節などに使用されます。

回転角度の範囲は 0 度以上、180 度以下です。

仕様

  • 動作電圧:5V
  • 消費電流:0.2A ~ 0.7A
  • 角度範囲:0° ~ 180°

必要なハードウェア

  • SG90 サーボモーター
  • Arduino
  • ジャンパーワイヤー

接続方法

サーボモーター Arduino
S D7
+ 5V
- GND

가운데|class=coders100

ライブラリ

  • Servo(標準ライブラリ)

サンプルコード

// サーボライブラリの使用
#include <Servo.h>
// 7番ピンを使用
#define pin 7

// サーボオブジェクトの宣言
Servo servo;

// 角度変数の宣言
int pos = 60;

void setup() {
  Serial.begin(9600);
  // サーボの制御ピンを設定
  servo.attach(pin);
  // サーボの初期角度を設定
  servo.write(pos);
  Serial.println("Serial start");
  Serial.print("default degree : ");
  Serial.println(pos);
}

void loop() {
  if (Serial.available()) {
    char contrl = Serial.read();
    if (contrl == 'a') { // シリアルモニターに a を入力すると 60 度増加
      if (pos == 180)
        Serial.println("max angle");
      else {
        pos = pos + 60;
        Serial.print("degree : ");
        Serial.println(pos);
        servo.write(pos);
      }
    } else if (contrl == 'b') { // シリアルモニターに b を入力すると 60 度減少
      if (pos == 0)
        Serial.println("min angle");
      else {
        pos = pos - 60;
        Serial.print("degree : ");
        Serial.println(pos);
        servo.write(pos);
      }
    }
  }
}

実行結果

가운데|class=coders100