'Stepper motor continues turning after code is finished

I am playing around with a Nema17 stepper motor on an Raspberry Pi 4 using Python 3.7.3 and a A4988 stepper motor driver. I run the code below and it works fine however after the code is finished, the stepper motor continues to turn in a somewhat sporatic fashion. How do I get it to just stop after it finishes the code? I have included the code I am using below.


# import the library
from RpiMotorLib import RpiMotorLib

#define GPIO pins
GPIO_pins = (17, 27, 22) # Microstep Resolution MS1-MS3 -> GPIO Pin
direction= 24       # Direction -> GPIO Pin
step = 23      # Step -> GPIO Pin

# Declare an named instance of class pass GPIO pins numbers
mymotortest = RpiMotorLib.A4988Nema(direction, step, GPIO_pins, "A4988")


# call the function, pass the arguments
mymotortest.motor_go(False, "Half" , 500, .001, False, .05)

# good practise to cleanup GPIO at some point before exit
GPIO.cleanup()```

P.S I was wondering what would be the best library or way to control a stepper motor? As in, is this the best library to be using or are there better and more precise methods in doing so.

Thank you


Solution 1:[1]

Maybe GPIO.cleanup puts the gpio outputs in an undefined, floating state. Try put the A4988 in sleep mode before GPIO.cleanup. A4988 pdf

Solution 2:[2]

I confirm the problem with A4988 on Pi Model 4B v1.4. Mace is right, the problem comes from GPIO.cleanup(). For example, removing the GPIO.cleanup() line in the A4988_Nema_Test.py script found on the github solved this issue in my case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mace
Solution 2 JohnB