2

I am building a sorting machine for a project where I want to sort the balls based on their color. For the actual sorting mechanism I am using a stepping motor (NEMA 17 with Driver DRV8825). I followed the steps from here to set up the connections between the PI and the motor. For my use case, I want to rotate the motor in either direction depending on the ball color. So, I am rotating my motor say 20 steps in one direction and then halt it for a few microseconds and then return it to the middle position (kind of pendulum but controlled). However, the actual behaviour is very different. Sometimes the motor works fine and then all of sudden it just rotates in one direction and the motion is also jerky (even if I set the motor to rotate in the other direction by changing the DIRECTION pin value). I have also added the code here which I am using

import RPi.GPIO as GPIO
from RpiMotorLib import RpiMotorLib
import time

GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) direction= 10 # Direction (DIR) GPIO Pin step = 25 # Step GPIO Pin EN_pin = 8 # enable pin (LOW to enable)

Declare a instance of class pass GPIO pins numbers and the motor type

mymotortest = RpiMotorLib.A4988Nema(direction, step, (21,21,21), "DRV8825") GPIO.setup(EN_pin,GPIO.OUT) # set enable pin as output

GPIO.setup(direction, GPIO.OUT) GPIO.setup(step, GPIO.OUT)

step_type = "Full" steps = 20

step_delay = 0.0008 initial_delay = 0

def halt_step(): GPIO.output(step, GPIO.HIGH) time.sleep(0.5) GPIO.output(step, GPIO.LOW)

def middle(ccw=True): #GPIO.output(EN_pin,GPIO.LOW) # pull enable to low to enable motor mymotortest.motor_go(ccw, # True=Clockwise, False=Counter-Clockwise step_type, # Step type (Full,Half,1/4,1/8,1/16,1/32) steps, # number of steps step_delay, # step delay [sec] False, # True = print verbose output initial_delay) GPIO.output(EN_pin,GPIO.HIGH)

def left(): GPIO.output(EN_pin,GPIO.LOW) # pull enable to low to enable motor mymotortest.motor_go(False, # True=Clockwise, False=Counter-Clockwise step_type, # Step type (Full,Half,1/4,1/8,1/16,1/32) steps, # number of steps step_delay, # step delay [sec] False, # True = print verbose output initial_delay) halt_step() middle(True)

def right(): GPIO.output(EN_pin,GPIO.LOW) # pull enable to low to enable motor mymotortest.motor_go(True, # True=Clockwise, False=Counter-Clockwise step_type, # Step type (Full,Half,1/4,1/8,1/16,1/32) steps, # number of steps step_delay, # step delay [sec] False, # True = print verbose output initial_delay) halt_step() middle(False)

if name == "main": import random for i in range(10): flag = random.random() #print(flag) if flag < 0.5: print(f"{flag}:Left") left() else: print(f"{flag}:Right") right()

    time.sleep(1)

GPIO.output(EN_pin,GPIO.HIGH)
GPIO.cleanup()

Any kind of help is appreciated, as I am kind of stuck here. I am new to the stepper motors, so I have no idea if something is wrong there.

  • Rather than reinventing the wheel look for existing solutions e.g. https://raspberrypi.stackexchange.com/a/105920/8697 – Milliways Apr 21 '22 at 22:54
  • I am using DRV8825 not L298N. So it might not work for me. Like I said it works sometimes but then maybe after a few minutes/hour it just jerks – Rajat Sharma Apr 21 '22 at 23:03
  • If this is occasional the most likely reason is your Python script is being preempted by the Linux kernel so throwing the pulse timing way out. If that's the case you will have to look to using something like pigpio waves. – joan Apr 22 '22 at 21:31

1 Answers1

0

I'd use the pigpio library from joan. You can easily look for a full stepper motor control guide made by rototron (search stepper motor rototron) where he demonstrates all the way you can use a stepper with rpi including joan's.