このチュートリアルはの一部です: サーボモーター
サーボモーターに関連するすべてのビデオをここに掲載しています。他のビデオへのリンクはこの記事の下にあります。
Arduinoを使ってプッシュボタンでサーボを制御する
このチュートリアルでは、2つのプッシュボタンを使ってサーボモーターを制御する方法を学びます。1つのボタンはサーボを左に動かすため、もう1つは右に動かすためのものです。右ボタンを押すとサーボは右に10度動き、左ボタンを押すと同じ量だけ左に動きます。ボタンを押し続けると、離すまでその方向に動き続けます。

このプロジェクトは、Arduinoを使ってボタンとサーボモーターを連携させる方法を理解するのに最適です。必要なハードウェア部品、配線手順、そしてすべてを動作させるためのコードを説明します。より分かりやすくするために、このチュートリアルに付随するビデオ(動画の01:00)を参照してください。
ハードウェアの解説
このプロジェクトでは、Arduinoボード、サーボモーター、2つのプッシュボタンが必要です。サーボモーターが動作を担当し、プッシュボタンはその動きの方向を制御するための入力として機能します。Arduinoはこれらの入力を処理し、サーボに適切な信号を送ります。
サーボモーターはパルス幅変調(PWM)信号に基づいて動作します。異なるパルス幅を送ることでサーボの角度を制御できます。プッシュボタンはArduinoのデジタル入力ピンに接続され、配線を簡素化するために内部プルアップ抵抗を使用します。
データシートの詳細
| 製造元 | さまざまな |
|---|---|
| 部品番号 | SG90 |
| ロジック/IO電圧 | 5 V |
| 供給電圧 | 4.8~6V |
| 出力電流(チャンネルあたり) | 最大1.5A |
| ピーク電流(チャンネルあたり) | 最大2.5 A |
| PWM周波数に関するガイダンス | 50 Hz |
| 入力論理の閾値 | 0.3VCC0.7Vまでカーボンコピー |
| 電圧降下 / Rドレイン-ソース(オン時)/ 彩度 | 0.4 V |
| 温度制限 | 85℃ |
| パッケージ | プラスチック |
| 備考 / バリエーション | 標準の180°回転 |
- サーボが電源電圧に対応していることを確認してください。
- 高電流で動作させる場合は、適切なヒートシンクを使用してください。
- ボタンをプルアップ構成でグラウンドに接続してください。
- 誤動作が発生している場合は、ボタンにデバウンス処理を行ってください。
- デバッグのためにシリアル出力を監視してください。
配線手順

部品を配線するには、まずサーボモーターを接続します。サーボの赤い線をArduinoの5Vピンに、黒い線をGNDピンに、黄色または白い信号線をArduinoの9番ピンに接続します。このピンがサーボの位置を制御します。
次に、プッシュボタンを配線します。右側のボタンの一方の端子をピン2に、もう一方の端子をグランドに接続します。左側のボタンは一方の端子をピン12に、もう一方の端子もグランドに接続します。この設定により、ボタンが押されていないときに内部プルアップ抵抗がピンを高い状態に保ちます。
コード例とウォークスルー
まずは、単一のプッシュボタンでサーボを制御するためのコードから始めましょう。コードはサーボオブジェクトを初期化し、初期角度を設定します。以下はその短い抜粋です:
int angle = 90; // initial angle for servo
int angleStep = 10; // step size for movement
このスニペットでは、angleサーボの現在の位置を表し、一方でangleStep各ボタン押下ごとにサーボがどれだけ動くかを定義します。このコードはサーボの位置を追跡するために不可欠です。
次に、サーボを取り付けて入力ピンを設定する setup() 関数があります:
void setup() {
Serial.begin(9600); // setup serial
myservo.attach(9); // attaches the servo on pin 9
pinMode(2, INPUT_PULLUP); // configure button pin
}
setup()ではシリアルモニタを初期化し、サーボをピン9に接続します。ピン2のボタンはプルアップ抵抗を使った入力に設定されており、配線が簡素化され外部抵抗が不要になります。
最後に、ボタンの押下を処理する loop 関数を見てみましょう:
while(digitalRead(2) == LOW) {
angle = angle + angleStep; // increment angle
myservo.write(angle); // move servo
}
このループはボタンが押されているかを継続的に確認します。ボタンが押されると角度は定義されたステップ分増加し、それに応じてサーボが動きます。サーボの損傷を防ぐため、角度が0〜180度の範囲内に収まるようにすることが重要です。
デモンストレーション / 何が期待できるか
右のボタンを押すと、サーボは10度ずつ右に動き、最大の180度に達するまで移動します。同様に、左のボタンを押すと左に動きます。ボタンを押し続けると、離すまでその方向に動き続けます(ビデオの01:00参照)。
よくある落とし穴には配線の問題(接続ミスやフローティング入力など)が含まれます。接続が確実であることと、コードで指定された正しいピンを使用していることを常に確認してください。
このチュートリアルはの一部です: サーボモーター
- Control a Servo Motor with a Push Button: Move Servo and Return SPB-1
- Control a Servo Motor with a Push Button: Move Servo in One Direction SPB-2
- Controlling a Servo Motor with a Push Button: Move Servo While Button Is Pressed (SPB-3)
- Arduinoを使ってポテンショメータでサーボを制御する
- Arduinoを使ってポテンショメータとLCD1602でサーボを制御する
- Arduinoを使った赤外線リモコンによるサーボモーターの制御
- ポテンショメーターを使用したArduinoサーボモーター制御
- Arduino用の手のジェスチャーによるサーボ位置の制御
- Controlling Two or More Servos with Potentiometers Using an Arduino
- How to Control a 360° Servo with Three Push-Button Switches
- How to Use Continuous 360° Servo with Arduino
- PCA9685 16チャンネル 12ビット サーボコントローラ V1 用の Arduino コードとビデオ
- Build an Arduino Servo Toggle Switch with a Push Button
/*
Controlling a servo with a push button with Arduino
When a push button is pressed, the servo starts moving to the right or left until
it reaches 180 and then returns to 0 degrees.
May 22, 2018 at 01:00
Written by Ahmad S. for Robojax.com in Ajax, Ontario, Canada
Watch a video for this code at https://youtu.be/7woqNH_qby4
This code is taken from http://robojax.com/learn/arduino
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle =90; // initial angle for servo
int angleStep =10;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(2,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
}
void loop() {
while(digitalRead(2) == LOW){
// change the angle for the next time through the loop:
angle = angle + angleStep;
// reverse the direction of the movement at the ends of the angle:
if (angle <= 0 || angle >= 180) {
angleStep = -angleStep;
}
myservo.write(angle); // move the servo to the desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degrees");
delay(100); // waits for the servo to get there
}// while
}
/*
Controlling a servo with two push buttons with Arduino
When the left push button is pressed, the servo starts moving to the left until it reaches 180 (or zero) degrees.
When the right push button is pressed, the servo starts moving to the right until it reaches 180 (or zero) degrees.
At any instance, if the button is released, the servo stops.
May 22, 2018, at 01:00
Written by Ahmad S. for Robojax.com in Ajax, Ontario, Canada
Watch a video for this code at https://youtu.be/7woqNH_qby4
This code is taken from http://robojax.com/learn/arduino
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle =90; // initial angle for servo
int angleStep =5;
#define LEFT 12 // pin 12 is connected to left button
#define RIGHT 2 // pin 2 is connected to right button
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 as input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assign pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println("Robojax Servo Button ");
}
void loop() {
// Servo button demo by Robojax.com
while(digitalRead(RIGHT) == LOW){
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 0;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}// while
// Servo button demo by Robojax.com
while(digitalRead(LEFT) == LOW){
// Servo button demo by Robojax.com
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if(angle >180){
angle =180;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}//
}
必要かもしれないもの
-
アマゾンアマゾンのサーボモーターamzn.to
-
アリエクスプレスAliExpressからSG90サーボモーター180または360を購入してください。s.click.aliexpress.com
リソースと参考文献
まだリソースはありません。
ファイル📁
ファイルは利用できません。