Arduino Biến trở kích hoạt động cơ servo

Trong Arduino - Động cơ servo được điều khiển bằng chiết áp, chúng ta đã học cách quay động cơ servo theo giá trị đầu ra của biến trở. Trong bài hướng dẫn này, chúng ta sẽ học cách:

Phần cứng cần chuẩn bị

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Hoặc) Potentiometer Kit
1×(Hoặc) Potentiometer Module with Knob
1×động cơ servo
1×dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về động cơ servo và biến trở

Nếu bạn chưa biết về động cơ servo và biến trở (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ nối dây động cơ servo với Arduino và chiết áp

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Ngưỡng Analog

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-potentiometer-triggers-servo-motor */ #include <Servo.h> // constants won't change const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin const int ANALOG_THRESHOLD = 500; Servo servo; // create servo object to control a servo void setup() { servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin if(analogValue > ANALOG_THRESHOLD) servo.write(90); // rotate servo motor to 90 degree else servo.write(0); // rotate servo motor to 0 degree }

Hướng dẫn từng bước

  • Kết nối Arduino với PC bằng cáp USB
  • Mở Arduino IDE, chọn board và cổng đúng
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp vào nút Tải lên trong Arduino IDE để tải mã lên Arduino
  • Xoay biến trở
  • Quan sát sự thay đổi của động cơ servo

Mã Arduino - ngưỡng điện áp

Giá trị tương tự của biến trở được chuyển đổi thành giá trị điện áp, sau đó được so sánh với ngưỡng điện áp để kích hoạt động cơ servo.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino/arduino-potentiometer-triggers-servo-motor */ #include <Servo.h> // constants won't change const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin const float VOLTAGE_THRESHOLD = 2.5; // Voltages Servo servo; // create servo object to control a servo void setup() { servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin float voltage = floatMap(analogValue, 0, 1023, 0, 5); // Rescale to potentiometer's voltage if(voltage > VOLTAGE_THRESHOLD) servo.write(90); // rotate servo motor to 90 degree else servo.write(0); // rotate servo motor to 0 degree } float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.