Raspberry Pi Stepper Motor với Limit Switch

Hướng dẫn này sẽ chỉ bạn cách sử dụng Raspberry Pi để điều khiển động cơ bước thông qua limit switch và driver L298N. Cụ thể, chúng ta sẽ tìm hiểu:

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Limit Switch (KW12-3)
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×Stepper Motor Nema 17
1×L298N Motor Driver Module
1×Adapter 12V
1×DC Power Jack
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)

Về Stepper Motor và Limit Switch

Nếu bạn chưa quen thuộc với stepper motor và limit switch (bao gồm sơ đồ chân, chức năng, lập trình, v.v.), các hướng dẫn sau có thể giúp bạn:

Sơ Đồ Kết Nối

Hướng dẫn này cung cấp sơ đồ kết nối cho hai trường hợp: Một stepper motor + một limit switch, Một stepper motor + hai limit switch.

  • Sơ đồ kết nối giữa Raspberry Pi, stepper motor và một limit switch
Raspberry Pi động cơ bước and limit switch sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

  • Sơ đồ kết nối giữa Raspberry Pi, stepper motor và hai limit switch
Raspberry Pi động cơ bước and two limit switches sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

※ Lưu ý:

Kết nối dây giữa stepper motor và L298N có thể khác nhau tùy thuộc vào loại stepper motor. Hãy xem kỹ hướng dẫn Raspberry Pi - Stepper Motor để học cách kết nối đúng stepper motor với driver motor L298N.

Code Raspberry Pi - Dừng Stepper Motor bằng Limit Switch

Stepper motor được lập trình để quay liên tục với code sau đây, và sẽ dừng ngay lập tức khi limit switch được chạm, và tiếp tục chuyển động nếu limit switch được nhả ra.

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

  • Đảm bảo bạn đã cài đặt Raspbian hoặc bất kỳ hệ điều hành tương thích Raspberry Pi nào khác trên Pi của bạn.
  • Đả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 của bạn.
  • Đảm bảo Raspberry Pi của bạn được kết nối internet nếu bạn cần cài đặt một số 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
  • Tạo file Python script stepper_limit_switch.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-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # Define GPIO pins for L298N driver and limit switch IN1 = 12 IN2 = 16 IN3 = 20 IN4 = 21 LIMIT_SWITCH_PIN = 27 # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(IN1, GPIO.OUT) GPIO.setup(IN2, GPIO.OUT) GPIO.setup(IN3, GPIO.OUT) GPIO.setup(IN4, GPIO.OUT) GPIO.setup(LIMIT_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Function to move the stepper motor one step forward def step_forward(delay): GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.LOW) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.HIGH) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) # Function to check the limit switch status def is_limit_switch_pressed(): return GPIO.input(LIMIT_SWITCH_PIN) == GPIO.LOW try: # Set the delay between steps delay = 0.001 while not is_limit_switch_pressed(): # Move the stepper motor forward endlessly step_forward(delay) except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Lưu file và chạy Python script bằng cách thực thi lệnh sau trong terminal:
python3 stepper_limit_switch.py
  • Nếu kết nối đúng, motor sẽ quay theo chiều kim đồng hồ.
  • Khi limit switch được chạm, motor sẽ dừng ngay lập tức.
  • Sau đó nếu limit switch được nhả ra, motor sẽ quay trở lại.

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.

Bằng cách thay đổi giá trị của biến delay trong code, bạn có thể thay đổi tốc độ của stepper motor.

Giải Thích Code

Hãy xem giải thích từng dòng có trong các comment của source code!

Code Raspberry Pi - Thay Đổi Hướng Stepper Motor bằng Limit Switch

Stepper motor sẽ được thiết lập chuyển động liên tục và hướng của nó sẽ được thay đổi khi limit switch được chạm.

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

  • Tạo file Python script stepper_direction.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-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # Define GPIO pins for L298N driver and limit switch IN1 = 12 IN2 = 16 IN3 = 20 IN4 = 21 LIMIT_SWITCH_PIN = 27 # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(IN1, GPIO.OUT) GPIO.setup(IN2, GPIO.OUT) GPIO.setup(IN3, GPIO.OUT) GPIO.setup(IN4, GPIO.OUT) GPIO.setup(LIMIT_SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Global variable to store the previous state of the limit switch prev_limit_switch_state = GPIO.HIGH # Function to move the stepper motor one step forward def step_forward(delay): GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.LOW) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.HIGH) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) # Function to move the stepper motor one step backward def step_backward(delay): GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.LOW) GPIO.output(IN3, GPIO.HIGH) GPIO.output(IN4, GPIO.HIGH) time.sleep(delay) GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.LOW) GPIO.output(IN3, GPIO.LOW) GPIO.output(IN4, GPIO.HIGH) time.sleep(delay) try: # Set the delay between steps delay = 0.001 # Set the initial direction direction = 'forward' while True: # Check if the limit switch state changes limit_switch_state = GPIO.input(LIMIT_SWITCH_PIN) # Change direction if the limit switch changes from HIGH to LOW if limit_switch_state == GPIO.LOW and prev_limit_switch_state == GPIO.HIGH: direction = 'backward' if direction == 'forward' else 'forward' # Move the stepper motor based on the direction if direction == 'forward': step_forward(delay) elif direction == 'backward': step_backward(delay) # Update the previous state of the limit switch prev_limit_switch_state = limit_switch_state except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Lưu file và chạy Python script bằng cách thực thi lệnh sau trong terminal:
python3 stepper_direction.py
  • Nếu kết nối đúng, motor sẽ quay theo chiều kim đồng hồ.
  • Khi bạn chạm vào limit switch, hướng của stepper motor sẽ thay đổi thành ngược chiều kim đồng hồ.
  • Chạm limit switch lần nữa và hướng của stepper motor sẽ quay trở lại chiều kim đồng hồ.

Code Raspberry Pi - Thay Đổi Hướng Stepper Motor bằng hai Limit Switch

Hãy xem code làm cho stepper motor quay liên tục, và khi một trong hai limit switch được chạm, sẽ chuyển đổi hướng của motor.

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

  • Tạo file Python script stepper_two_limit_switches.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-stepper-motor-limit-switch import RPi.GPIO as GPIO import time # Define GPIO pins for L298N driver and limit switches IN1 = 12 IN2 = 16 IN3 = 20 IN4 = 21 LIMIT_SWITCH1_PIN = 27 LIMIT_SWITCH2_PIN = 19 # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(IN1, GPIO.OUT) GPIO.setup(IN2, GPIO.OUT) GPIO.setup(IN3, GPIO.OUT) GPIO.setup(IN4, GPIO.OUT) GPIO.setup(LIMIT_SWITCH1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(LIMIT_SWITCH2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Define constants DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Global variables to store the previous state of the limit switches prev_limit_switch_1 = GPIO.HIGH prev_limit_switch_2 = GPIO.HIGH # Function to move the stepper motor one step forward def step_forward(delay): GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.LOW) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.HIGH) GPIO.output(IN3, GPIO.HIGH) GPIO.output(IN4, GPIO.LOW) time.sleep(delay) # Function to move the stepper motor one step backward def step_backward(delay): GPIO.output(IN1, GPIO.LOW) GPIO.output(IN2, GPIO.LOW) GPIO.output(IN3, GPIO.HIGH) GPIO.output(IN4, GPIO.HIGH) time.sleep(delay) GPIO.output(IN1, GPIO.HIGH) GPIO.output(IN2, GPIO.LOW) GPIO.output(IN3, GPIO.LOW) GPIO.output(IN4, GPIO.HIGH) time.sleep(delay) try: # Set the delay between steps delay = 0.001 # Set the initial direction direction = 'forward' while True: # Check if limit switch 1 state changes limit_switch_1 = GPIO.input(LIMIT_SWITCH1_PIN) if limit_switch_1 == GPIO.LOW and prev_limit_switch_1 == GPIO.HIGH: direction = 'backward' # Check if limit switch 2 state changes limit_switch_2 = GPIO.input(LIMIT_SWITCH2_PIN) if limit_switch_2 == GPIO.LOW and prev_limit_switch_2 == GPIO.HIGH: direction = 'forward' # Move the stepper motor based on the direction if direction == 'forward': step_forward(delay) elif direction == 'backward': step_backward(delay) # Update the previous states of the limit switches prev_limit_switch_1 = limit_switch_1 prev_limit_switch_2 = limit_switch_2 except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Lưu file và chạy Python script bằng cách thực thi lệnh sau trong terminal:
python3 stepper_two_limit_switches.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.

  • Nếu kết nối đúng, motor sẽ quay theo chiều kim đồng hồ.
  • Khi bạn chạm limit switch 1, hướng của stepper motor sẽ được đảo ngược thành ngược chiều kim đồng hồ.
  • Chạm limit switch 2 sẽ làm cho stepper motor quay theo chiều kim đồng hồ trở lại.