Search Code

제85강: 서보 모터 소개 | 아두이노 단계별 강좌

제85강: 서보 모터 소개 | 아두이노 단계별 강좌

이 프로젝트 가이드는 연속 회전 서보 모터, 즉 360도 서보의 세계를 소개합니다. 특정 각도(예: 0~180도)로 움직이는 일반 서보와 달리, 이 모터는 양방향으로 계속 회전합니다. 따라서 소형 로봇, 탱크 트레드, 또는 간단하고 저렴한 구동 모터가 필요한 모든 프로젝트에 이상적입니다. 이 가이드에서는 Arduino와 몇 개의 푸시 버튼을 사용하여 이 모터를 제어하는 방법을 보여드리며, 방향과 속도를 직접 제어할 수 있게 해줍니다.

360도 서보의 실용적인 응용 사례는 다음과 같습니다:

  • 소형 2륜 로봇 자동차 제작.
  • 끝없이 회전하는 팬-틸트 카메라 마운트 만들기.
  • 소형 컨베이어 벨트 또는 윈치 구동.
  • 간단한 레이더 또는 센서 스캐닝 플랫폼 만들기.

하드웨어 및 부품

이 프로젝트에는 다음 부품이 필요합니다:

  • Arduino 보드 (예: Uno)
  • 연속 회전 서보 모터 (예: FS90R 또는 유사 제품)
  • 푸시 버튼 3개
  • 점퍼 와이어
  • 브레드보드 (선택 사항, 배선을 더 쉽게 하기 위해)

배선 가이드

이 프로젝트의 배선은 간단합니다. 서보 모터에는 일반적으로 갈색(접지), 빨간색(전원), 주황색 또는 노란색(신호)의 세 가닥 전선이 있습니다. 푸시 버튼은 간단한 구성으로 배선됩니다.

연결에 대한 자세한 설명은 다음과 같습니다:

  • 서보 모터:
    • 갈색 전선(접지)은 Arduino의 GND 핀에 연결합니다.
    • 빨간색 전선(전원)은 Arduino의 5V 핀에 연결합니다.
    • 주황색/노란색 전선(신호)은 Arduino 핀 9에 연결합니다.
  • 푸시 버튼:
    • 각 버튼의 한쪽은 Arduino의 GND 핀에 연결합니다.
    • "반시계 방향" 버튼의 다른 쪽은 핀 2에 연결합니다.
    • "정지" 버튼의 다른 쪽은 핀 3에 연결합니다.
    • "시계 방향" 버튼의 다른 쪽은 핀 4에 연결합니다.

비디오에서 제작자는 서보의 신호선이 Arduino의 PWM 지원 핀에 연결되어야 하며, 이 핀은 일반적으로 물결표(~) 기호로 표시된다고 강조합니다. 핀 9는 이러한 핀 중 하나입니다. 푸시 버튼은 Arduino의 내부 풀업 저항을 사용하므로 외부 저항이 필요 없어 배선이 간단해집니다 (비디오 07:02 부분).

코드 설명

코드는 세 개의 푸시 버튼 상태를 읽고 해당 명령을 서보 모터로 보내도록 설계되었습니다. 사용자가 구성할 수 있는 부분을 자세히 살펴보겠습니다.

핀 및 명령 구성

코드 상단에는 핀과 서보 제어 값에 대한 정의가 있습니다. 여기서 프로젝트의 동작을 사용자 지정할 수 있습니다.


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] 서보 속도 및 정지 값 조정

이미지

SG90 Mini Servo Motor
SG90 Mini Servo Motor
SG90_servo_motor-1
SG90_servo_motor-1
115-Arduino code to control continuous 360° servo from serial monitor command
언어: C++
/* 
 *  
 *  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); 
                }
                  
                 
        }

  
}
893-Arduino code to control continuous 360° servo from serial monitor command (Code 2)
언어: C++
/* 
 *  
 *  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 

필요할 수 있는 것들

자원 및 참고자료

파일📁

다른 파일들