Arduino UNO R4 Cảm Biến Ánh Sáng Điều Khiển Servo Motor

Trong hướng dẫn này, chúng ta sẽ học cách sử dụng Arduino UNO R4 để điều khiển servo motor dựa trên giá trị đọc được từ cảm biến ánh sáng hoặc module cảm biến ánh sáng. Cụ thể, Arduino UNO R4 sẽ đọc giá trị từ cảm biến ánh sáng:

Arduino UNO R4 cảm biến ánh sáng controls động cơ servo

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Cảm Biến Ánh Sáng
1×Điện trở 10 kΩ
1×động cơ servo
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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

Cảm biến ánh sáng LDR có giá thành rất phải chăng, nhưng nó cần thêm điện trở khi đấu nối, điều này có thể làm cho việc kết nối phức tạp hơn. Để đơn giản hóa việc đấu nối, bạn có thể sử dụng module cảm biến ánh sáng LDR như một giải pháp thay thế.

Giới Thiệu Về Servo Motor và Cảm Biến Ánh Sáng

Nếu bạn chưa quen thuộc với servo motor và cảm biến ánh sáng (cách kết nối, chức năng và lập trình), vui lòng tham khảo các hướng dẫn bên dưới để tìm hiểu thêm:

Sơ Đồ Đấu Nối

  • Sơ đồ đấu nối giữa Arduino Uno R4, servo motor và cảm biến ánh sáng (Analog)
Arduino UNO R4 cảm biến ánh sáng động cơ servo sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

  • Sơ đồ đấu nối giữa Arduino Uno R4, servo motor và module cảm biến ánh sáng (digital)
Arduino UNO R4 cảm biến ánh sáng module động cơ servo sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4 - Ngưỡng Analog

  • Code Arduino Uno R4 để đọc giá trị từ cảm biến ánh sáng LDR và điều khiển servo motor
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-light-sensor-controls-servo-motor */ #include <Servo.h> #define LIGHT_SENSOR_PIN A0 // Arduino Uno R4 pin connected to light sensor's pin #define SERVO_PIN 9 // Arduino Uno R4 pin connected to Servo Motor's pin #define 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(LIGHT_SENSOR_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 }
  • Code Arduino Uno R4 để đọc giá trị từ module cảm biến ánh sáng LDR và điều khiển servo motor
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-light-sensor-controls-servo-motor */ #include <Servo.h> #define LIGHT_SENSOR_PIN 11 // Arduino Uno R4 pin connected to light sensor's pin #define SERVO_PIN 9 // Arduino Uno R4 pin connected to Servo Motor's pin Servo servo; // create servo object to control a servo void setup() { Serial.begin(9600); // initialize serial communication pinMode(LIGHT_SENSOR_PIN, INPUT); // initialize the Arduino Uno R4's pin as an input servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { int light_state = digitalRead(LIGHT_SENSOR_PIN); if (light_state == HIGH) { Serial.println("The light is NOT present"); servo.write(90); // rotate servo motor to 90 degree } else { Serial.println("The light is present"); servo.write(0); // rotate servo motor to 0 degree } }

Các Bước Thực Hiện

Thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Đấu nối các linh kiện theo sơ đồ được cung cấp.
  • Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Sao chép code phía trên và dán vào Arduino IDE.
  • Nhấp vào nút Upload trong Arduino IDE để truyền code lên Arduino UNO R4.
  • Xoay cảm biến ánh sáng.
  • Quan sát chuyển động của servo motor.

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.

Bình Luận