레슨 88: 두 개의 푸시 버튼 스위치를 사용하여 서보 모터 제어하기
이 프로젝트는 Arduino와 두 개의 푸시 버튼을 사용하여 서보 모터를 제어하는 방법을 보여줍니다. 가변 저항이나 시리얼 명령에 의존하는 대신, 간단한 택트 스위치를 사용하여 서보를 수동으로 왼쪽 또는 오른쪽으로 움직일 수 있습니다. 이는 로봇 팔 구축, 카메라 짐벌 조정, 또는 원격 제어 팬-틸트 시스템 생성과 같이 메커니즘에 대한 직접적이고 물리적인 제어가 필요한 응용 분야에서 매우 유용합니다. 버튼을 사용하면 서보의 각도를 점진적으로 조정할 수 있으며, 버튼을 누르고 있으면 전체 작동 범위를 스윕할 수 있습니다.
이 설정으로 구축할 수 있는 몇 가지 실용적인 프로젝트는 다음과 같습니다:
- 로봇 팔 또는 그리퍼용 수동 제어 패널 구축.
- 원격 제어 카메라 슬라이더 또는 팬-틸트 헤드 생성.
- 모형 비행기 또는 보트 방향타용 물리적 제어 인터페이스 개발.
- CNC 기계 또는 3D 프린터용 위치 기록을 위한 간단한 "티치 펜던트" 만들기.

하드웨어 및 부품
이 프로젝트는 몇 가지 기본 부품만 필요하므로 Arduino로 입력 및 출력 장치를 모두 배우기에 훌륭한 시작점이 됩니다.
- Arduino 보드 (Uno, Nano, Mega 등)
- 서보 모터 (예: SG90 또는 MG995)
- 두 개의 순간 푸시 버튼
- 점퍼 와이어
- 브레드보드 (선택 사항이지만 권장)
배선 가이드
비디오에서 배선은 간단합니다. 서보 모터의 신호 핀은 Arduino의 PWM 지원 핀에 연결되며, 이 예에서는 핀 9입니다. 서보의 전원과 접지는 Arduino의 5V 및 GND 핀에 연결됩니다. 두 개의 푸시 버튼은 서로 유사한 방식으로 배선됩니다. 각 버튼의 한 다리는 접지에 연결되고, 다른 다리는 Arduino의 디지털 입력 핀(왼쪽 버튼은 핀 2, 오른쪽 버튼은 핀 3)에 연결됩니다. 코드는 내부 풀업 저항을 사용하므로 버튼에 외부 저항이 필요하지 않습니다. (비디오 00:57에서)

코드 설명
코드는 쉽게 구성할 수 있도록 설계되었습니다. 주요 사용자 구성 가능 부분은 스케치 상단에 있습니다. 다음은 자신의 프로젝트에 맞게 조정할 수 있는 중요한 변수와 정의에 대한 설명입니다.
#include <Servo.h>
Servo myservo; // 서보를 제어할 서보 객체 생성
int servoPin = 9;// 이 핀은 PWM ~이 있는 핀이어야 합니다
int angle =90; // 서보의 초기 각도
int angleStep =5;
#define LEFT 2 // 핀 2는 왼쪽 버튼에 연결됨
#define RIGHT 3 // 핀 3은 오른쪽 버튼에 연결됨
servoPin: 이 변수는 서보의 신호 와이어가 연결된 Arduino 핀을 정의합니다. 보드에서 일반적으로~기호로 표시되는 펄스 폭 변조(PWM)를 지원하는 핀을 사용하는 것이 중요합니다.angle: 이는 Arduino가 시작될 때 서보의 초기 각도를 설정합니다. 기본값은 90도이며, 일반적으로 서보를 중앙에 위치시킵니다.angleStep: 이는 버튼을 누를 때마다 서보 각도가 변경되는 증분입니다. 값이 5이면 각 누름마다 서보가 5도씩 이동합니다. 더 빠른 이동을 위해 이 값을 늘리거나 더 세밀한 제어를 위해 줄일 수 있습니다.LEFT및RIGHT: 이#define지시문은 푸시 버튼이 연결된 디지털 핀을 할당합니다. 버튼을 다른 핀에 배선한 경우 설정에 맞게 이 값을 변경해야 합니다.
loop() 함수의 핵심 로직은 버튼이 눌렸는지 확인합니다(풀업 구성으로 인해 LOW 신호를 읽음). "오른쪽" 버튼이 눌리면 angle 변수를 angleStep만큼 감소시킵니다. "왼쪽" 버튼이 눌리면 angle을 증가시킵니다. 코드에는 각도가 0~180도 범위 내에 유지되도록 경계 검사가 포함되어 있습니다. 그런 다음 myservo.write(angle) 함수를 사용하여 서보가 새 위치로 이동하도록 명령합니다.
라이브 프로젝트 시연
비디오 시연에서 기능이 명확하게 표시됩니다. "왼쪽" 버튼을 누르고 있으면 서보가 180도에 도달하여 멈출 때까지 각도가 한 번에 5도씩 증가하면서 왼쪽으로 부드럽게 스윕합니다. 마찬가지로 "오른쪽" 버튼을 누르고 있으면 각도가 0도에 도달할 때까지 감소하면서 서보가 오른쪽으로 다시 스윕합니다. 현재 각도는 시리얼 모니터에 출력되어 실시간으로 서보의 정확한 위치를 확인할 수 있습니다. (비디오 05:20에서)
비디오 챕터
- [00:00] 버튼으로 서보 제어 소개
- [00:57] 서보 및 푸시 버튼 배선 가이드
- [02:12] 코드 설명, 1부: 설정 및 구성
- [03:25] 코드 설명, 2부: 메인 루프 로직
- [05:20] 프로젝트 라이브 시연
- [06:23] 결론 및 잠재적 응용 분야
/*
* S05-05 Servo control with 2 push buttons
* Download and resource page https://robojax.com/RJT764
Watch video on YouTube https://www.youtube.com/watch?v=J_kbyAY1rLM
*
* Written by Ahmad Shamshiri for Robojax.com
* on Jan 15, 2019 at 19:15 in Ajax, Ontario, Canada
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Controlling a servo with two Push buttons with Arduino
when Left push button is pressed, the servo start moving to the left until it reaches 180( or zero) degrees
when Right push button is pressed, the servo start moving to the right until it reaches 180( or zero) degrees
At any instance if the button is released, servo stops
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int angle =90; // initial angle for servo
int angleStep =5;
#define LEFT 2 // pin 2 is connected to left button
#define RIGHT 3 // pin 3 is connected to right button
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assing pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println("Robojax Servo 2 Buttons ");
}
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
}//
}
필요할 수 있는 것들
-
아마존
-
아마존
-
아마존
-
아마존Servo motor on Amazonamzn.to
-
이베이
파일📁
다른 파일들
-
SG90 Seroo 모터 데이터시트
robojax-servo-SG90_datasheet.pdf0.12 MB