2 stepper motors simultaneously on rpi pico (micropython)

Thread Starter

Robert_Kabuski

Joined Jan 9, 2023
11
I am trying to get 2 stepper motors to run at 2 seperate (and changing) speeds on a rpi pico. the pico will also be doing other things (like reading inputs) along side this. the only way I know to control the speed of a motor is using the time.sleep (see code below function between pulses, however this does not work for more than one motor. is there any way to control mltiple outputs without stacking them together in the same loop?
any help would be much appreciated
-Rob
from machine import Pin
import time

step = Pin(16, Pin.OUT)
dire = Pin(17, Pin.OUT)
spr = 200

dire.high()

def turn_motor(steps, delay):
while True:
for i in range(steps):
step.high()
time.sleep(delay)
step.low()
time.sleep(delay)
time.sleep(.5)
dire.low()
for i in range(steps):
step.high()
time.sleep(delay)
step.low()
time.sleep(delay)
time.sleep(.5)
dire.high()

rpm = 70
delay = (rpm/60)/spr
steps = spr*8
turn_motor(steps, delay)
 

Jerry-Hat-Trick

Joined Aug 31, 2022
232
I've not used an R Pi, or Python, but I have used an Arduino to drive three stepper motors at different speeds by assigning "delay" values to each motor which are counted down with each program loop so that each motor steps when it's respective delay number reaches zero (or safer when it goes negative), then re-setting the corresponding delay (which in itself may be changed by the program). Typically, stepper motors can't run very fast - a 200 step motor without load can probably rotate once a second - so it's unlikely that your program is so slow or long that the "delay" numbers are small. Hope this helps!
 

Thread Starter

Robert_Kabuski

Joined Jan 9, 2023
11
I've not used an R Pi, or Python, but I have used an Arduino to drive three stepper motors at different speeds by assigning "delay" values to each motor which are counted down with each program loop so that each motor steps when it's respective delay number reaches zero (or safer when it goes negative), then re-setting the corresponding delay (which in itself may be changed by the program). Typically, stepper motors can't run very fast - a 200 step motor without load can probably rotate once a second - so it's unlikely that your program is so slow or long that the "delay" numbers are small. Hope this helps!
this didnt work because python has to finish the delay before doing anything else
 

MrChips

Joined Oct 2, 2009
28,155
Delay functions can be blocking or non-blocking.
You are using a blocking function. You need a non-blocking function.
The usual way to implement non-blocking delay function is with hardware timer module and interrupts.
 

Jerry-Hat-Trick

Joined Aug 31, 2022
232
I probably didn’t explain it properly. You don’t need a delay function at all, this is why I put “delay” in inverted commas. You can simply use the time it takes the program to complete one cycle as your “delay” reference constant. Suppose you program takes 10us to complete and you want a delay between steps of 100ms - you use a value of 100 to count down by 1 each cycle to zero and use an if statement to make a step and reset the value to 100. In practice, your program probably loops faster than that, so you can put a very short delay somewhere in it, maybe just at the beginning, which forces the program to have a longer cycle time and which the rest of the program can tolerate nothing happening in that time.

Using timers and interrupts is certainly more elegant but unless the rest of your program is fairly complex I think you will find this ‘count down’ approach is easy to follow and easy to implement. Effectively you are decrementing a value as if it were a timer and using an if statement as if it were an interrupt.
 
Top