Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Control steppermotor with RPI using python

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)
increase the delay to about 0.5 or 1 maybe 2 and post your results
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(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.
change the delay to 0.05 and see if that works
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(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.
(Jul 20, 2017 01:42 PM)SIM-1517404 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
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(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.
(Jul 21, 2017 05:34 AM)SIM-1517404 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 ??
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(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.
(Jul 21, 2017 05:55 AM)SIM-1517404 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
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 630 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by SIM-1517404Sep 27, 2017 03:05 PM 0 98 Sep 27, 2017 03:05 PM
Last Post: SIM-1517404
[Python] by SIM-1517404Aug 17, 2017 08:24 AM 7 338 Aug 17, 2017 08:24 AM
Last Post: SIM-1517404