stepper_limit_switch.py라는 이름의 Python 스크립트 파일을 생성하고 다음 코드를 추가하세요:
/* * 이 라즈베리 파이 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch */import RPi.GPIOasGPIOimport time# Define GPIO pins for L298N driver and limit switchIN1 = 12IN2 = 16IN3 = 20IN4 = 21LIMIT_SWITCH_PIN = 27# Set GPIO mode and configure pinsGPIO.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 constantsDEG_PER_STEP = 1.8STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP)# Function to move the stepper motor one step forwarddef 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 statusdef is_limit_switch_pressed():returnGPIO.input(LIMIT_SWITCH_PIN) == GPIO.LOWtry:# Set the delay between steps delay = 0.001whilenot is_limit_switch_pressed():# Move the stepper motor forward endlessly step_forward(delay)exceptKeyboardInterrupt:print("\nExiting the script.")finally:# Clean up GPIO settingsGPIO.cleanup()
파일을 저장하고 터미널에서 다음 명령어를 실행하여 Python 스크립트를 실행하세요:
python3 stepper_limit_switch.py
배선이 올바르면 모터는 시계 방향으로 회전해야 합니다.
리미트 스위치가 접촉되면 모터는 즉시 멈춰야 합니다.
그런 다음 리미트 스위치가 해제되면 모터가 다시 회전합니다.
스크립트는 터미널에서 Ctrl + C를 누를 때까지 무한 루프로 계속 실행됩니다.
코드에서 delay 변수의 값을 변경하여 스테퍼 모터의 속도를 변경할 수 있습니다.
코드 설명
소스 코드 주석에 포함된 줄별 설명을 확인하세요!
라즈베리 파이 코드 - 리미트 스위치로 스텝 모터 방향 변경
스테퍼 모터는 계속해서 작동되며, 리미트 스위치가 작동될 때 방향이 변경됩니다.
자세한 사용 방법
Python 스크립트 파일 stepper_direction.py을 만들고 다음 코드를 추가하세요:
/* * 이 라즈베리 파이 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch */import RPi.GPIOasGPIOimport time# Define GPIO pins for L298N driver and limit switchIN1 = 12IN2 = 16IN3 = 20IN4 = 21LIMIT_SWITCH_PIN = 27# Set GPIO mode and configure pinsGPIO.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 constantsDEG_PER_STEP = 1.8STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP)# Global variable to store the previous state of the limit switchprev_limit_switch_state = GPIO.HIGH# Function to move the stepper motor one step forwarddef 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 backwarddef 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'whileTrue:# 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 LOWif limit_switch_state == GPIO.LOWand prev_limit_switch_state == GPIO.HIGH: direction = 'backward'if direction == 'forward'else'forward'# Move the stepper motor based on the directionif 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_stateexceptKeyboardInterrupt:print("\nExiting the script.")finally:# Clean up GPIO settingsGPIO.cleanup()
파일을 저장하고 터미널에서 다음 명령어를 실행하여 Python 스크립트를 실행하세요:
python3 stepper_direction.py
배선이 올바르면 모터는 시계 방향으로 회전해야 합니다.
리미트 스위치를 터치하면 스테퍼 모터의 방향이 반시계 방향으로 바뀝니다.
리미트 스위치를 다시 터치하면 스테퍼 모터의 방향이 시계 방향으로 돌아갑니다.
라즈베리 파이 코드 - 두 개의 리미트 스위치로 스테퍼 모터 방향 변경
스테퍼 모터가 계속 회전하고 두 개의 제한 스위치 중 하나가 눌렸을 때 모터의 방향을 전환하는 코드를 살펴봅시다.
자세한 사용 방법
Python 스크립트 파일 stepper_two_limit_switches.py을(를) 생성하고 다음 코드를 추가하세요:
/* * 이 라즈베리 파이 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pi/raspberry-pi-stepper-motor-limit-switch */import RPi.GPIOasGPIOimport time# Define GPIO pins for L298N driver and limit switchesIN1 = 12IN2 = 16IN3 = 20IN4 = 21LIMIT_SWITCH1_PIN = 27LIMIT_SWITCH2_PIN = 19# Set GPIO mode and configure pinsGPIO.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 constantsDEG_PER_STEP = 1.8STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP)# Global variables to store the previous state of the limit switchesprev_limit_switch_1 = GPIO.HIGHprev_limit_switch_2 = GPIO.HIGH# Function to move the stepper motor one step forwarddef 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 backwarddef 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'whileTrue:# Check if limit switch 1 state changes limit_switch_1 = GPIO.input(LIMIT_SWITCH1_PIN)if limit_switch_1 == GPIO.LOWand 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.LOWand prev_limit_switch_2 == GPIO.HIGH: direction = 'forward'# Move the stepper motor based on the directionif 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_2exceptKeyboardInterrupt:print("\nExiting the script.")finally:# Clean up GPIO settingsGPIO.cleanup()