検索コード

Arduinoを使ってプッシュボタンでサーボを制御する

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 wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons

部品を配線するには、まずサーボモーターを接続します。サーボの赤い線を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参照)。

よくある落とし穴には配線の問題(接続ミスやフローティング入力など)が含まれます。接続が確実であることと、コードで指定された正しいピンを使用していることを常に確認してください。

画像

Arduinoを使って、単一の押しボタンでサーボを制御する
Arduinoを使って、単一の押しボタンでサーボを制御する
SG90_servo_motor-1
SG90_servo_motor-1
SG90_servo_motor-0
SG90_servo_motor-0
Arduino wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons
98-Control a servo with a single push button using Arduino
言語: C++
/*
 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

  
}
99-Control a servo with two push buttons using Arduino
言語: C++
/*
 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
  }// 

  
}

必要かもしれないもの

リソースと参考文献

まだリソースはありません。

ファイル📁

ファイルは利用できません。