-
Notifications
You must be signed in to change notification settings - Fork 5
/
stepper_control.py
34 lines (27 loc) · 1.01 KB
/
stepper_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import RPi.GPIO as GPIO
import time
class Stepper():
def __init__(self, step_pin, direction_pin, enable_pin, sleep_time=0.0005):
GPIO.setmode(GPIO.BOARD)
self.direction_pin = direction_pin
self.step_pin = step_pin
self.sleep_time = sleep_time
self.enable_pin = enable_pin
def setup(self):
GPIO.setup(self.step_pin, GPIO.OUT)
GPIO.setup(self.direction_pin, GPIO.OUT)
GPIO.setup(self.enable_pin, GPIO.OUT)
GPIO.output(self.enable_pin, GPIO.LOW)
def step(self, direction, steps=200):
GPIO.output(self.direction_pin, direction)
for _ in range(steps):
GPIO.output(self.step_pin, GPIO.HIGH)
time.sleep(self.sleep_time)
GPIO.output(self.step_pin, GPIO.LOW)
time.sleep(self.sleep_time)
def cleanup(self):
GPIO.cleanup()
if __name__ == "__main__":
stepper = Stepper(step_pin=12, direction_pin=16, enable_pin=8)
stepper.setup()
stepper.step(direction=1, steps=200)