Hack Forums
[Python] Control steppermotor with RPI using python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Control steppermotor with RPI using python (/showthread.php?tid=5674242)



Control steppermotor with RPI using python - SIM-1517404 - Jul 20, 2017

Hi,

I'm working on controlling a bipolar stepper motor with the Raspberry pi using python scripts. I want to rotate the motor a specific set of degrees for a full 360 degree rotation. For example rotate the motor 11,25 degrees at a time a total of 32 times for a full rotation.

I have all the connections set up, but when I rund a script I found online the motor goes crazy, rotating uncontrollably in both directions.

Can someone point me in the right direction? The scripts I used is:

Code
import RPi.GPIO as GPIO
import time

# Variables

delay = 0.0055
steps = 500

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Enable pins for IN1-4 to control step sequence

coil_A_1_pin = 18
coil_A_2_pin = 23
coil_B_1_pin = 24
coil_B_2_pin = 25

# Set pin states

GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)


# Function for step sequence

def setStep(w1, w2, w3, w4):
  GPIO.output(coil_A_1_pin, w1)
  GPIO.output(coil_A_2_pin, w2)
  GPIO.output(coil_B_1_pin, w3)
  GPIO.output(coil_B_2_pin, w4)

# loop through step sequence based on number of steps

for i in range(0, steps):
    setStep(1,0,1,0)
    time.sleep(delay)
    setStep(0,1,1,0)
    time.sleep(delay)
    setStep(0,1,0,1)
    time.sleep(delay)
    setStep(1,0,0,1)
    time.sleep(delay)

# Reverse previous step sequence to reverse motor direction

for i in range(0, steps):
    setStep(1,0,0,1)
    time.sleep(delay)
    setStep(0,1,0,1)
    time.sleep(delay)
    setStep(0,1,1,0)
    time.sleep(delay)
    setStep(1,0,1,0)
    time.sleep(delay)



* - koolkid - Jul 20, 2017

increase the delay to about 0.5 or 1 maybe 2 and post your results


* - SIM-1517404 - Jul 20, 2017

(Jul 20, 2017 01:06 PM)koolkid Wrote:  increase the delay to about 0.5 or 1 maybe 2 and post your results

It goes 1 step forward, pauses for about a second, 1 step backwards, pauses for a second and repeat.


* - koolkid - Jul 20, 2017

change the delay to 0.05 and see if that works


* - SIM-1517404 - Jul 20, 2017

(Jul 20, 2017 01:36 PM)koolkid Wrote:  change the delay to 0.05 and see if that works

Does the same thing, just faster. I think my output pins on the motor are messed up, I'm gonna double check and I'll get back.


* - koolkid - Jul 20, 2017

(Jul 20, 2017 01:42 PM)Swatchi Wrote:  Does the same thing, just faster. I think my output pins on the motor are messed up, I'm gonna double check and I'll get back.

ok do that and test it again and see if it works


* - SIM-1517404 - Jul 21, 2017

(Jul 20, 2017 02:13 PM)koolkid Wrote:  ok do that and test it again and see if it works

Now it's not rotating at all. I have no clue what is wrong, trying to figure it out.


* - koolkid - Jul 21, 2017

(Jul 21, 2017 05:34 AM)Swatchi Wrote:  Now it's not rotating at all. I have no clue what is wrong, trying to figure it out.

did you switch the pins around ??


* - SIM-1517404 - Jul 21, 2017

(Jul 21, 2017 05:45 AM)koolkid Wrote:  did you switch the pins around ??

Yeah I did. The motor is getting voltage because it tightens up, but I don't know if maybe the driver is fried. I don't know what else to check/do at this point.


* - koolkid - Jul 21, 2017

(Jul 21, 2017 05:55 AM)Swatchi Wrote:  Yeah I did. The motor is getting voltage because it tightens up, but I don't know if maybe the driver is fried. I don't know what else to check/do at this point.

try putting back the pins to their original state