Nema 17 Stepper stops moving randomly

March 17, 2024, 17:32

samstrike

Hi guys, Im working on a little project where I need a Nema 17 Stepper. I paired it with a l298n module and control it with the Raspberry Pi 4B. My problem is, that the motor works fine the first few rotations but then stops randomly. After stopping it just rattles and vibrates. I already tried changing the connections and increased my power supply. I tried using code from different tutorials online but nothing helped. This is my code for testing the stepper:
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
ControlPin = [7, 11, 13, 15]

for pin in ControlPin:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, 0)

StepCount = 8
# this two-dimensional array holds the eight-step sequence for half stepping
Seq = [ [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],
        [1, 0, 0, 1] ]


# GPIO.output(enable_pin, 1)
def setStep(w1, w2, w3, w4):
    GPIO.output(ControlPin[0], w1)
    GPIO.output(ControlPin[1], w2)
    GPIO.output(ControlPin[2], w3)
    GPIO.output(ControlPin[3], w4)


def forward(delay, steps):
    for i in range(steps):
        for j in range(StepCount):
            setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
            time.sleep(delay)


def backwards(delay, steps):
    for i in range(steps):
        for j in reversed(range(StepCount)):
            setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
            time.sleep(delay)


if __name__ == '__main__':
    while True:
        delay = input("Delay (ms)?")
        steps = input("How many steps forwards? ")
        forward(int(delay) / 1000.0, int(steps))
        steps = input("How many steps backwards? ")
        backwards(int(delay) / 1000.0, int(steps))


# this cleanup command is needed so that all the pins are reset once the program ends
GPIO.cleanup()