아두이노 - 모션 센서 - 서보 모터 | Arduino - Motion Sensor - Servo Motor

우리는 배울 것입니다:

준비물

1×Arduino Uno Amazon
1×USB 2.0 cable type A/B 쿠팡 | Amazon
1×HC-SR501 Motion Sensor Amazon
1×Servo Motor Amazon
1×Jumper Wires Amazon
1×(Optional) 9V Power Adapter for Arduino Amazon
1×(Recommended) Screw Terminal Block Shield for Arduino Uno 쿠팡 | Amazon
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

서보 모터 및 모션 센서에 관하여

서보 모터와 모션 센서(핀 배치, 작동 방식, 프로그래밍 방법 등)에 대해 잘 모르신다면, 다음 튜토리얼에서 배워보세요:

선연결

Arduino Motion Sensor Servo Motor Wiring Diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

초기 설정

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

아두이노 코드 - 모션 센서가 서보 모터를 제어합니다.

/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-motion-sensor-servo-motor */ #include <Servo.h> // 상수는 변경되지 않음 const int MOTION_SENSOR_PIN = 7; // 모션 센서의 핀에 연결된 아두이노 핀 const int SERVO_PIN = 9; // 서보 모터의 핀에 연결된 아두이노 핀 Servo servo; // 서보를 제어하기 위한 서보 객체 생성 // 변수는 변경될 것임: int angle = 0; // 서보 모터의 현재 각도 int lastMotionState; // 모션 센서의 이전 상태 int currentMotionState; // 모션 센서의 현재 상태 void setup() { Serial.begin(9600); // 시리얼 초기화 pinMode(MOTION_SENSOR_PIN, INPUT); // 아두이노 핀을 입력 모드로 설정 servo.attach(SERVO_PIN); // 서보 객체에 9번 핀의 서보를 첨부 servo.write(angle); currentMotionState = digitalRead(MOTION_SENSOR_PIN); } void loop() { lastMotionState = currentMotionState; // 마지막 상태 저장 currentMotionState = digitalRead(MOTION_SENSOR_PIN); // 새로운 상태 읽기 if (currentMotionState == LOW && lastMotionState == HIGH) { // 핀 상태 변경: LOW -> HIGH Serial.println("Motion detected!"); servo.write(90); } else if (currentMotionState == HIGH && lastMotionState == LOW) { // 핀 상태 변경: HIGH -> LOW Serial.println("Motion stopped!"); servo.write(0); } }

사용 방법

  • 아두이노를 USB 케이블을 이용해 PC에 연결하세요
  • 아두이노 IDE를 열고, 올바른 보드와 포트를 선택하세요
  • 위의 코드를 복사하고 아두이노 IDE로 엽니다
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 코드를 아두이노에 업로드하세요
  • 센서 앞에서 손을 움직이세요
  • 서보 모터의 변화를 확인하세요

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!