Page 1 of 1

PyBoard and stepper motor

Posted: Thu Oct 23, 2014 10:03 pm
by adamboho
Hi everyone. This is just to show, how I use pyboard to control stepper motor.
Maybe someone will need it.

See it in action. https://www.youtube.com/watch?v=4XYBexi ... e=youtu.be

Code: Select all

import pyb

accel = pyb.Accel()

speed_x = 0

angle_x = 0

tim_x = pyb.Timer(1, freq=1)


def stepper_x(speed, angle):
    global speed_x
    global angle_x

    if angle_x != angle:

        if angle_x < angle:
            
            angle_x += 1

            pyb.Pin('X2', pyb.Pin.OUT_PP).low()
        
            pyb.Pin('X1', pyb.Pin.OUT_PP).high()
            pyb.Pin('X1', pyb.Pin.OUT_PP).low()

        if angle_x > angle:
            
            angle_x -= 1

            pyb.Pin('X2', pyb.Pin.OUT_PP).high()
        
            pyb.Pin('X1', pyb.Pin.OUT_PP).high()
            pyb.Pin('X1', pyb.Pin.OUT_PP).low()

        
    
    if speed_x != speed:
        tim_x.init(freq = speed)
        speed_x = speed




        
while True:
    x = accel.x()
    x = x * 3
    tim_x.callback(lambda t:stepper_x(200, x))
    
    pyb.delay(100)
    
    pyb.LED(3).on()

Any suggestions welcome.

Re: PyBoard and stepper motor

Posted: Fri Oct 24, 2014 6:35 am
by riklaunim
I used slightly different solution for ULN2003 stepper motor controller, without the Timer class.

Re: PyBoard and stepper motor

Posted: Fri Oct 24, 2014 8:05 pm
by adamboho
Yes, that is fine but I want use USB_VCP() to receive commands to control the stepper motor, so pyb.delay() is no good, at least for me.

Re: PyBoard and stepper motor

Posted: Wed May 27, 2015 7:33 am
by yllumi
I am using ULD2003 and trying the code from @riklaunim. It works but the rotation is too slow, even after I change the delay to 2ms. How can I raise the speed?