0
0

More than 3 years have passed since last update.

ArduinoにServoモータを繋いで動かす

Last updated at Posted at 2019-02-12

Servoモータを接続して動かした手順を記載します。

準備

Arduino UNO R3 互換品
マイクロサーボ SG90
・ジャンパワイヤ3個(オスーオス)

届きました。

回路

Arduino Servoモータ
5V <--> Vcc
GND <--> GND
PIN9 <--> 制御ピン

servo.png

コード

Servoライブラリの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

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

テスト

サーボモータが180度回転すればOKです。

参考

組み込みエンジニアでなくても週末にArduinoを使って遊ぶ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0