Home

Awesome

pypi version shield pypi downloads per month shield

Description

A micropython library to use stepper motors in a tidy way.

Your steppers should be connected to a stepper driver that is controlled using a step, a dir and optional an enable pin.

This library uses one timer per stepper to achive controlled speeds for multiple steppers in a non blocking way.

The steppers can be controlled by angle if the number of steps per rotation is supplied.

Multiple steppers can share a common dir pin, allowing for N steppers controlled by N+1 output pins.

Examples

Two steppers

from stepper import Stepper
import time

s1 = Stepper(18,19,steps_per_rev=200,speed_sps=50)
s2 = Stepper(20,21,steps_per_rev=200,speed_sps=50)
# some boards might require a different timer_id for each stepper:
# s1 = Stepper(18,19,steps_per_rev=200,speed_sps=50,timer_id=0)
# s2 = Stepper(20,21,steps_per_rev=200,speed_sps=50,timer_id=1)

s1.target_deg(90)
s2.target_deg(45)
time.sleep(5.0)
s1.target_deg(0)
s2.target_deg(5)
time.sleep(5.0)

Calibrate absolute position using an endswitch

import machine
import time
from stepper import Stepper

s1 = Stepper(18,19,steps_per_rev=200)
#create an input pin for the end switch (switch connects pin to GND)
endswitch = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)

s1.speed(20) #use low speed for the calibration
s1.free_run(-1) #move backwards
while endswitch.value(): #wait till the switch is triggered
    pass
s1.stop() #stop as soon as the switch is triggered
s1.overwrite_pos(0) #set position as 0 point
s1.target(0) #set the target to the same value to avoid unwanted movement
s1.speed(100) #return to default speed
s1.track_target() #start stepper again

#calibration finished. Do something else below.
s1.target_deg(45)
time.sleep(5.0)

Two steppers sharing a common dir pin

import machine
from stepper import Stepper

dir_pin = machine.Pin(19,machine.Pin.OUT)
s1 = Stepper(18,dir_pin,steps_per_rev=200,speed_sps=50)
s2 = Stepper(20,dir_pin,steps_per_rev=200,speed_sps=50)

API

class Stepper(step_pin,dir_pin,en_pin=None,steps_per_rev=200,speed_sps=10,invert_dir=False,timer_id=-1)

speed(sps)

speed_rps(rps)

target(t)

target_deg(deg)

target_rad(rad)

get_pos()

get_pos_deg()

get_pos_rad()

overwrite_pos(p)

overwrite_pos_deg(deg)

overwrite_pos_rad(rad)

step(d)

free_run(d)

track_target()

stop()

enable(e)

is_enabled()