commit b0d7349b4e99b1e8241db457f27f8ddcf5674247 Author: zhcong <1011.0011@163.com> Date: Tue Dec 18 15:50:06 2018 +0800 upload v1.0 diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..4f3003d --- /dev/null +++ b/README.MD @@ -0,0 +1,18 @@ +# ULN2003 for ESP32 +uln2003 for `MicroPython` has been in github, here's the [link](https://github.com/IDWizard/uln2003). But it's work for `microbit`, so I change a little to transplant for my NodeMCU's `ESP-32s`. Here's the example: +```python +import Stepper +from machine import Pin +s1 = Stepper.create(Pin(16,Pin.OUT),Pin(17,Pin.OUT),Pin(5,Pin.OUT),Pin(18,Pin.OUT), delay=2) +s1.step(100) +s1.step(100,-1) +s1.angle(180) +s1.angle(360,-1) +``` +function `angle` is angle, and the PIN map: +`IN1` link `PIN_16` +`IN2` link `PIN_17` +`IN3` link `PIN_5` +`IN4` link `PIN_18` +My uln2003 board like this, and stepper runing:
+![uln2003](img/uln2003.jpg) ![uln2003](img/stepper.gif)
\ No newline at end of file diff --git a/Stepper.py b/Stepper.py new file mode 100644 index 0000000..a62620c --- /dev/null +++ b/Stepper.py @@ -0,0 +1,58 @@ +import time + +# only test for uln2003 +class Stepper: + FULL_ROTATION = int(4075.7728395061727 / 8) # http://www.jangeox.be/2013/10/stepper-motor-28byj-48_25.html + + HALF_STEP = [ + [0, 0, 0, 1], + [0, 0, 1, 1], + [0, 0, 1, 0], + [0, 1, 1, 0], + [0, 1, 0, 0], + [1, 1, 0, 0], + [1, 0, 0, 0], + [1, 0, 0, 1], + ] + + FULL_STEP = [ + [1, 0, 1, 0], + [0, 1, 1, 0], + [0, 1, 0, 1], + [1, 0, 0, 1] + ] + def __init__(self, mode, pin1, pin2, pin3, pin4, delay): + if mode=='FULL_STEP': + self.mode = self.FULL_STEP + else: + self.mode = self.HALF_STEP + self.pin1 = pin1 + self.pin2 = pin2 + self.pin3 = pin3 + self.pin4 = pin4 + self.delay = delay # Recommend 10+ for FULL_STEP, 1 is OK for HALF_STEP + + # Initialize all to 0 + self.reset() + + def step(self, count, direction=1): + """Rotate count steps. direction = -1 means backwards""" + for x in range(count): + for bit in self.mode[::direction]: + self.pin1(bit[0]) + self.pin2(bit[1]) + self.pin3(bit[2]) + self.pin4(bit[3]) + time.sleep_ms(self.delay) + self.reset() + def angle(self, r, direction=1): + self.step(int(self.FULL_ROTATION * r / 360), direction) + def reset(self): + # Reset to 0, no holding, these are geared, you can't move them + self.pin1(0) + self.pin2(0) + self.pin3(0) + self.pin4(0) + +def create(pin1, pin2, pin3, pin4, delay=2, mode='HALF_STEP'): + return Stepper(mode, pin1, pin2, pin3, pin4, delay) diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..b189fb0 --- /dev/null +++ b/boot.py @@ -0,0 +1,5 @@ +# This file is executed on every boot (including wake-boot from deepsleep) +# import esp +# esp.osdebug(None) +# import webrepl +# webrepl.start() diff --git a/img/stepper.gif b/img/stepper.gif new file mode 100644 index 0000000..c227291 Binary files /dev/null and b/img/stepper.gif differ diff --git a/img/uln2003.jpg b/img/uln2003.jpg new file mode 100644 index 0000000..4483732 Binary files /dev/null and b/img/uln2003.jpg differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..d5673cb --- /dev/null +++ b/main.py @@ -0,0 +1,13 @@ +import Stepper +from machine import Pin +''' +IN1 --> 16 +IN2 --> 17 +IN3 --> 5 +IN4 --> 18 +''' +s1 = Stepper.create(Pin(16,Pin.OUT),Pin(17,Pin.OUT),Pin(5,Pin.OUT),Pin(18,Pin.OUT), delay=2) +s1.step(100) +s1.step(100,-1) +s1.angle(180) +s1.angle(360,-1) \ No newline at end of file