レッスン85:サーボモーター入門 | Arduinoステップバイステップコース
このプロジェクトガイドでは、連続回転サーボモーター(360度サーボとも呼ばれます)の世界を紹介します。特定の角度(0度から180度など)に動く標準的なサーボとは異なり、これらのモーターはどちらの方向にも連続的に回転します。そのため、小型ロボットや戦車のキャタピラ、またはシンプルで手頃な駆動モーターを必要とするあらゆるプロジェクトに最適です。このガイドでは、Arduinoといくつかの押しボタンを使ってサーボを制御し、その方向と速度を手動で制御する方法を紹介します。
360度サーボの実用的な用途をいくつか紹介します:
- 小型二輪ロボットカーを製作する。
- 無限に回転するパン・チルト式カメラマウントを作成する。
- 小型コンベアベルトやウインチを駆動する。
- シンプルなレーダーやセンサー走査プラットフォームを製作する。

ハードウェアと部品
このプロジェクトには、以下の部品が必要です:
- Arduinoボード(例:Uno)
- 連続回転サーボモーター(例:FS90Rまたは類似品)
- 押しボタン3個
- ジャンパーワイヤー
- ブレッドボード(配線を簡単にするための任意部品)
配線ガイド
このプロジェクトの配線は簡単です。サーボモーターには3本のワイヤーがあります:通常、茶色(グランド)、赤(電源)、オレンジまたは黄色(信号)です。押しボタンはシンプルな構成で配線されます。
接続の内訳は以下の通りです:
- サーボモーター:
- 茶色のワイヤー(グランド)はArduinoのGNDピンに接続します。
- 赤いワイヤー(電源)はArduinoの5Vピンに接続します。
- オレンジ/黄色のワイヤー(信号)はArduinoのピン9に接続します。
- 押しボタン:
- 各ボタンの片側はArduinoのGNDピンに接続します。
- 「反時計回り」ボタンのもう片側はピン2に接続します。
- 「停止」ボタンのもう片側はピン3に接続します。
- 「時計回り」ボタンのもう片側はピン4に接続します。
ビデオでは、作成者はサーボの信号ワイヤーをArduinoのPWM対応ピン(多くの場合、チルダ(~)記号で示されます)に接続する必要があることを強調しています。ピン9はこれらのピンの1つです。押しボタンはArduinoの内蔵プルアップ抵抗を使用するため、外部抵抗が不要になり配線が簡素化されます(ビデオの07:02時点)。
コードの説明
このコードは、3つの押しボタンの状態を読み取り、対応するコマンドをサーボモーターに送信するように設計されています。ユーザーが設定可能な部分を詳しく見ていきましょう。
ピンとコマンドの設定
コードの先頭に、ピンとサーボ制御値の定義があります。ここでプロジェクトの動作をカスタマイズできます。
const int servoPin = 9; // サーボ信号用のPWMピン
const int stopPin = 3; // 停止ボタン用のピン
const int cwPin = 4; // 時計回りボタン用のピン
const int ccwPin = 2; // 反時計回りボタン用のピン
これらの定数は、ボタンとサーボが接続されているArduinoのピンを定義します。ボタンに異なるピンを使用する場合は、ここで数値を変更します。servoPinはPWM対応ピンである必要があります。
const int csServoCommand[3] = {106, 52, 0}; // {反時計回り、停止、時計回り}
const String csServoCommandText[3] = {"反時計回り", "停止", "時計回り"};
これは理解して調整する上で最も重要な部分です。csServoCommand配列はサーボに送信される値を保持します。連続サーボの場合、これらの値は角度ではなく速度と方向を表します。
- 106は全速反時計回り回転の値です。
- 52は完全停止の値です。
- 0は全速時計回り回転の値です。
これらの値は普遍的ではありません。製造上のばらつきのため、「停止」値(52)は調整が必要な場合があります。停止ボタンを押したときにサーボがゆっくり動き続ける場合は、この値を微調整する必要があります。ビデオではこのプロセスを実演し、値を54や51に変更するとモーターの動作に影響が出ることを示しています(ビデオの13:45時点)。
回転の値を調整してモーターの速度を変更することもできます。例えば、反時計回りの値を70に設定すると、106の場合よりも遅く回転します(ビデオの12:23時点)。
csServoCommandText配列は、現在のコマンド状態を表示するためにシリアルモニターに出力されるテキスト文字列を保持します。
カスタム関数:servoCommand()
このコードは、コマンドロジックを処理するためのカスタム関数を使用します。この関数は数値(0、1、または2)を引数として受け取り、ステータステキストを更新し、対応するコマンドをサーボに送信します。
void servoCommand(int n) {
statusText = csServoCommandText[n];
myservo.write(csServoCommand[n]);
Serial.println(csServoCommandText[n]);
Serial.println(csServoCommand[n]);
}
この関数は、ボタンが押されたときにメインループから呼び出されます。表示の更新とサーボの制御のロジックを一元化し、コードをよりクリーンで変更しやすくします。
ライブプロジェクトとデモンストレーション
ビデオでは、作成者がプロジェクトの動作を実演しています。「時計回り」ボタンを押すと値0がサーボに送信され、一方向に回転します。「反時計回り」ボタンを押すと値106が送信され、反対方向に回転します。「停止」ボタンは52を送信し、モーターを停止させます。
シリアルモニタは、現在の状態とサーボに送信されている正確な値を表示するために使用されます。これはデバッグや、特定のモーターに最適な「停止」値を見つけるために非常に役立ちます。ビデオで述べられているように、サーボの内部回路により停止コマンドが完璧でない場合があり、ゆっくりとしたドリフトや振動が見られることがあります。これは安価な連続回転サーボによく見られる特性であり、コード内の停止値を調整することで最小限に抑えることができます(ビデオの01:15参照)。
チャプター
- [00:05] 360度サーボモーターの紹介
- [02:02] 部品とハードウェアの概要
- [02:40] 詳細な配線図と接続方法
- [05:07] コードの説明:定数、配列、セットアップ
- [09:56] コードの説明:servoCommand関数
- [10:54] ライブデモとシリアルモニタの出力
- [12:23] サーボ速度と停止値の調整
/*
*
* Demonstration of Controlling Continuous Servo (360 servo)
* This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor.
📚⬇️ Download and resource page https://robojax.com/RJT762
*
* Modified by Ahmad Shamshiri for Robojax.com
* on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
* Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
* Get this code from Robojax.com
*
Original code by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
Modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#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
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("received: ");
Serial.print (incomingByte);
if(incomingByte == 108){
Serial.println(" sent 0 Rotating CW ");
myservo.write(0);
}else if(incomingByte == 114){
Serial.println(" sent 180 Rotating CCW ");
myservo.write(180);
}else if(incomingByte == 60){
Serial.println(" sent Stopped ");
myservo.write(60);
}else{
Serial.println(" moving Randomly");
myservo.write(incomingByte);
}
}
}
/*
*
* Demonstration of Controlling Continuous Servo (360 servo)
360 Servo -2
* This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor.
📚⬇️ Download and resource page https://robojax.com/RJT762
*
* Modified by Ahmad Shamshiri for Robojax.com
* on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
* Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
* Get this code from Robojax.com
*
Original code by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
Modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int servoPin = 9;// this pin must be of those with PWM ~
int pos = 0; // variable to store the servo position
int incomingByte = 0; // for incoming serial data
int CWBS, CCWBS, SBS;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);// set pin for push button STOP
pinMode(3,INPUT_PULLUP);// set pin for push button CCW
pinMode(4,INPUT_PULLUP);// set pin for push button CW
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
}
void loop() {
CCWBS = digitalRead(2);// read status of button CCW
SBS = digitalRead(3);// read status of button STOP
CWBS = digitalRead(4);// read status of button CW
if(CCWBS == LOW){
Serial.println(" sent 0 Rotaing CW ");
myservo.write(0);
}else if(CWBS == LOW){
Serial.println(" sent 180 Rotaing CCW ");
myservo.write(180);
}else if(SBS == LOW){
Serial.println(" sent Stopped ");
myservo.write(60);
}else{
Serial.println(" moving Random");
myservo.write(48);
}
}// loop
必要かもしれないもの
-
アマゾン360 Degree Servo motor on Amazonlink.amazon
-
アマゾン
-
イーベイ360 Degree Servo motor on eBayebay.us
-
アリエクスプレス360 Degree Servo motor on AliExpresss.click.aliexpress.com
-
アリエクスプレス360 Degree Servo motor on AliExpress-2s.click.aliexpress.com
リソースと参考文献
ファイル📁
他のファイル
-
SG90 Seroo モーターのデータシート
robojax-servo-SG90_datasheet.pdf0.12 MB