July 11, 2024, 03:51
python Raspberry Pi GPIO A4988/DRV8825 Pins Stepper Motor Coils External Power Supply ------------------ ------------------ ------------------ --------------------- 3.3V (Pin 1) -> VDD GND (Pin 6) -> GND GPIO 17 (Pin 11) -> STEP GPIO 27 (Pin 13) -> DIR GND (Pin 6) -> EN VMOT -> VMOT -> Red (1A) GND -> GND -> Blue (1B) -> Black (2A) -> Green (2B)I triple checked my connections.
import RPi.GPIO as GPIO import time # GPIO setup GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Stepper Motor GPIO pins STEP_PIN = 17 DIR_PIN = 27 GPIO.setup(STEP_PIN, GPIO.OUT) GPIO.setup(DIR_PIN, GPIO.OUT) # Function to control stepper motor def move_stepper_motor_continuous(step_pin, dir_pin, delay): GPIO.output(dir_pin, GPIO.LOW) # Set direction to counter-clockwise while True: GPIO.output(step_pin, GPIO.HIGH) time.sleep(delay) GPIO.output(step_pin, GPIO.LOW) time.sleep(delay) # Main function if __name__ == "__main__": try: # Medium speed: adjust delay as needed delay = 0.005 move_stepper_motor_continuous(STEP_PIN, DIR_PIN, delay) except KeyboardInterrupt: # Cleanup GPIO settings on exit GPIO.cleanup()
Components Raspberry Pi (3.3V logic) A4988 Stepper Motor Driver NEMA 17 Stepper Motor 12v External Power Supply Connections A4988 Driver to Raspberry Pi: VDD -> Raspberry Pi 3.3V (Pin 1) GND -> Raspberry Pi GND (Pin 6) STEP -> Raspberry Pi GPIO 17 (Pin 11) DIR -> Raspberry Pi GPIO 27 (Pin 13) EN -> Raspberry Pi GND (Pin 6) A4988 Driver to Stepper Motor: 1A -> Red (Pin 1 on the motor) 1B -> Blue (Pin 4 on the motor) 2A -> Black (Pin 3 on the motor) 2B -> Green (Pin 6 on the motor) A4988 Driver to External Power Supply: VMOT -> Positive terminal of the external power supply GND -> Negative terminal of the external power supply
python import RPi.GPIO as GPIO from RpiMotorLib import RpiMotorLib import time import sys # Redirect stdout to a file log_file = '/home/halloween_clock/motor_control.log' sys.stdout = open(log_file, 'w') try: ################################ # RPi and Motor Pre-allocations ################################ # # Define GPIO pins direction = 22 # Direction (DIR) GPIO Pin step = 23 # Step GPIO Pin EN_pin = 24 # Enable pin (LOW to enable) # Declare an instance of class pass GPIO pins numbers and the motor type mymotortest = RpiMotorLib.A4988Nema(direction, step, (21, 21, 21), "A4988") GPIO.setup(EN_pin, GPIO.OUT) # Set enable pin as output ########################### # Actual motor control ########################### # GPIO.output(EN_pin, GPIO.LOW) # Pull enable to low to enable motor mymotortest.motor_go(False, # True=Clockwise, False=Counter-Clockwise "Full", # Step type (Full,Half,1/4,1/8,1/16) 200, # Number of steps 0.0005, # Step delay [sec] True, # True = print verbose output 0.05) # Initial delay [sec] except Exception as e: print(f"An error occurred: {e}") finally: GPIO.cleanup() # Clear GPIO allocations after run sys.stdout.close()