Servo Motor(SG90) JP: 두 판 사이의 차이

아두위키 : Arduwiki
(새 문서: {{#seo:|title=ArduWiki : Arduino サーボモーター (SG90) ガイド|title_mode=append|keywords=Arduino, 情報科学, メイカー学習, パフォーマンス評価, サーボモーター (SG90), Arduino作品, キャップストーン作品, Arduinoサンプルコード|description=Arduinoでサーボモーター(SG90)を制御する方法(基本情報、配線、サンプルコード)を紹介します。情報科学やメイカー授業に活用できます。}}[...)
(차이 없음)

2025년 3월 27일 (목) 15:02 판

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

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

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

仕様

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

必要なハードウェア

接続方法

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

ライブラリ

  • 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);
      }
    }
  }
}

実行結果