108640 Views
83860 Views
59555 Views
48723 Views
48311 Views
47806 Views
Build a laser-cut robot
Robots and Lasers
Arduino Plug and Make Kit Review
Pi to Pico W Bluetooth Communication
Two-Way Bluetooth Communication Between Raspberry Pi Picos
Gamepad 2
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
Intermediate level MicroPython
Introduction to FreeCAD for Beginners
KevsRobots Learning Platform
63% Percent Complete
By Kevin McAleer, 2 Minutes
Page last updated August 02, 2024
Create a Python script to control the motors and the servo:
import RPi.GPIO as GPIO import time # GPIO pin setup GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Stepper motor pins motor_pins = [17, 18, 27, 22] # Set all pins as output for pin in motor_pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) # Servo pin servo_pin = 23 GPIO.setup(servo_pin, GPIO.OUT) # Set PWM frequency to 50Hz pwm = GPIO.PWM(servo_pin, 50) pwm.start(0) def set_servo_angle(angle): duty = angle / 18 + 2 GPIO.output(servo_pin, True) pwm.ChangeDutyCycle(duty) time.sleep(1) GPIO.output(servo_pin, False) pwm.ChangeDutyCycle(0) # Step sequence for the stepper motor step_sequence = [ [1, 0, 0, 1], [1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1] ] def move_motor(steps, direction=1): for _ in range(steps): for step in step_sequence[::direction]: for pin in range(len(motor_pins)): GPIO.output(motor_pins[pin], step[pin]) time.sleep(0.001) try: while True: move_motor(512, direction=1) # Move motor forward set_servo_angle(90) # Lower the pen time.sleep(1) set_servo_angle(0) # Raise the pen move_motor(512, direction=-1) # Move motor backward time.sleep(1) except KeyboardInterrupt: GPIO.cleanup() pwm.stop()
< Previous Next >