Raspberry Pi Điều Khiển Servo Motor bằng Potentiometer

Hướng dẫn này sẽ chỉ cho bạn cách sử dụng Raspberry Pi để điều khiển góc quay của servo motor dựa trên giá trị đầu vào từ potentiometer. Cụ thể, chúng ta sẽ học:

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×động cơ servo
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Tùy chọn) Potentiometer Kit
1×(Tùy chọn) Potentiometer Module với Núm Xoay
1×mô-đun ADC ADS1115
1×breadboard
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Giới Thiệu Servo Motor và Potentiometer

Nếu bạn chưa quen thuộc với servo motor và potentiometer (sơ đồ chân, cách hoạt động, cách lập trình, v.v.), các hướng dẫn sau có thể hữu ích:

Sơ Đồ Đấu Nối

Raspberry Pi động cơ servo chiết áp sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

Các Bước Nhanh

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích khác trên Raspberry Pi.
  • Đảm bảo Raspberry Pi của bạn được kết nối với cùng mạng cục bộ với PC.
  • Đảm bảo Raspberry Pi của bạn có kết nối internet nếu cần cài đặt thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH bằng SSH client tích hợp sẵn trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Cài đặt thư viện Adafruit_ADS1x15 bằng cách chạy các lệnh sau trên terminal Raspberry Pi:
sudo pip install Adafruit-ADS1x15
  • Tạo file script Python potentiometer_servo.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi 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/raspberry-pi/raspberry-pi-potentiometer-servo-motor import time import RPi.GPIO as GPIO import Adafruit_ADS1x15 # Constants SERVO_PIN = 26 # Raspberry Pi GPIO pin connected to the servo motor ADC_CHANNEL = 0 # Analog channel on ADS1015 GAIN = 1 # Gain (1, 2/3, 1, 2, 4, 8, 16) # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(SERVO_PIN, GPIO.OUT) # Create PWM instance for servo servo_pwm = GPIO.PWM(SERVO_PIN, 50) # 50 Hz frequency servo_pwm.start(0) # Initialize servo position # Create ADS1x15 instance ads = Adafruit_ADS1x15.ADS1015() def map_value(value, in_min, in_max, out_min, out_max): # Map the input value from one range to another return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min try: while True: # Read the raw ADC value from the potentiometer pot_value = ads.read_adc(ADC_CHANNEL, gain=GAIN) # Map the ADC value to the servo angle (0 to 180 degrees) angle = int(map_value(pot_value, 0, 32767, 0, 180)) # Control the servo motor according to the angle duty_cycle = (angle / 18) + 2.5 # Convert angle to duty cycle servo_pwm.ChangeDutyCycle(duty_cycle) print(f"Potentiometer Value: {pot_value}, Servo Angle: {angle}") time.sleep(0.1) except KeyboardInterrupt: servo_pwm.stop() GPIO.cleanup()
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 potentiometer_servo.py

Script chạy trong vòng lặp vô hạn liên tục cho đến khi bạn nhấn Ctrl + C trong terminal.

  • Xoay potentiometer
  • Quan sát sự quay của servo motor
  • Xem kết quả trong Serial Monitor
PuTTY - Raspberry Pi
Potentiometer Value: 100, Servo Angle: 3 Potentiometer Value: 200, Servo Angle: 6 Potentiometer Value: 300, Servo Angle: 9 Potentiometer Value: 400, Servo Angle: 13 Potentiometer Value: 500, Servo Angle: 16 Potentiometer Value: 600, Servo Angle: 19 Potentiometer Value: 700, Servo Angle: 23 Potentiometer Value: 800, Servo Angle: 26 Potentiometer Value: 900, Servo Angle: 29 Potentiometer Value: 1000, Servo Angle: 33

Giải Thích Code

Hãy xem phần giải thích từng dòng được chứa trong các comment của source code!

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.