kopia lustrzana https://github.com/zhcong/ULN2003-for-ESP32
Merge pull request #8 from yousseftechdev/master
Updated Stepper.py to be more convenient and removed unnecessary function `create()`master
commit
0e5fe3fbad
25
README.MD
25
README.MD
|
@ -1,13 +1,24 @@
|
||||||
# ULN2003 for ESP32
|
# 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:
|
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
|
```python
|
||||||
import Stepper
|
from stepper 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)
|
in1 = 16
|
||||||
s1.step(100)
|
in2 = 17
|
||||||
s1.step(100,-1) # or s1.step(-100)
|
in3 = 5
|
||||||
s1.angle(180)
|
in4 = 18
|
||||||
s1.angle(360,-1) # or s1.angle(-360)
|
delay = 2
|
||||||
|
mode = 1 # 0 for half step, 1 for full step
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
s1 = Stepper(in1, in2, in3, in4, delay, mode)
|
||||||
|
s1.step(100)
|
||||||
|
s1.step(100,-1)
|
||||||
|
s1.angle(180)
|
||||||
|
s1.angle(360,-1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
```
|
```
|
||||||
function `angle` is angle, and the PIN map:
|
function `angle` is angle, and the PIN map:
|
||||||
`IN1` link `PIN_16`
|
`IN1` link `PIN_16`
|
||||||
|
|
8
boot.py
8
boot.py
|
@ -1,5 +1,3 @@
|
||||||
# This file is executed on every boot (including wake-boot from deepsleep)
|
# from main import main
|
||||||
# import esp
|
|
||||||
# esp.osdebug(None)
|
# main()
|
||||||
# import webrepl
|
|
||||||
# webrepl.start()
|
|
||||||
|
|
28
main.py
28
main.py
|
@ -1,13 +1,15 @@
|
||||||
import Stepper
|
from stepper import Stepper
|
||||||
from machine import Pin
|
|
||||||
'''
|
in1 = 16
|
||||||
IN1 --> 16
|
in2 = 17
|
||||||
IN2 --> 17
|
in3 = 5
|
||||||
IN3 --> 5
|
in4 = 18
|
||||||
IN4 --> 18
|
delay = 2
|
||||||
'''
|
mode = 1 # 0 for half step, 1 for full step
|
||||||
s1 = Stepper.create(Pin(16,Pin.OUT),Pin(17,Pin.OUT),Pin(5,Pin.OUT),Pin(18,Pin.OUT), delay=2)
|
|
||||||
s1.step(100)
|
def main() -> None:
|
||||||
s1.step(100,-1)
|
s1 = Stepper(in1, in2, in3, in4, delay, mode)
|
||||||
s1.angle(180)
|
s1.step(100)
|
||||||
s1.angle(360,-1)
|
s1.step(100,-1)
|
||||||
|
s1.angle(180)
|
||||||
|
s1.angle(360,-1)
|
|
@ -1,4 +1,5 @@
|
||||||
import time
|
import time
|
||||||
|
from machine import Pin
|
||||||
|
|
||||||
# only test for uln2003
|
# only test for uln2003
|
||||||
class Stepper:
|
class Stepper:
|
||||||
|
@ -21,15 +22,17 @@ class Stepper:
|
||||||
[0, 1, 0, 1],
|
[0, 1, 0, 1],
|
||||||
[1, 0, 0, 1]
|
[1, 0, 0, 1]
|
||||||
]
|
]
|
||||||
def __init__(self, mode, pin1, pin2, pin3, pin4, delay):
|
def __init__(self, pin1, pin2, pin3, pin4, delay, mode=1):
|
||||||
if mode=='FULL_STEP':
|
if mode == 1:
|
||||||
self.mode = self.FULL_STEP
|
self.mode = self.FULL_STEP
|
||||||
else:
|
elif mode == 0:
|
||||||
self.mode = self.HALF_STEP
|
self.mode = self.HALF_STEP
|
||||||
self.pin1 = pin1
|
else:
|
||||||
self.pin2 = pin2
|
raise ValueError("Mode must be either 0 or 1")
|
||||||
self.pin3 = pin3
|
self.pin1 = Pin(pin1, Pin.OUT)
|
||||||
self.pin4 = pin4
|
self.pin2 = Pin(pin2, Pin.OUT)
|
||||||
|
self.pin3 = Pin(pin3, Pin.OUT)
|
||||||
|
self.pin4 = Pin(pin4, Pin.OUT)
|
||||||
self.delay = delay # Recommend 10+ for FULL_STEP, 1 is OK for HALF_STEP
|
self.delay = delay # Recommend 10+ for FULL_STEP, 1 is OK for HALF_STEP
|
||||||
|
|
||||||
# Initialize all to 0
|
# Initialize all to 0
|
||||||
|
@ -56,6 +59,3 @@ class Stepper:
|
||||||
self.pin2(0)
|
self.pin2(0)
|
||||||
self.pin3(0)
|
self.pin3(0)
|
||||||
self.pin4(0)
|
self.pin4(0)
|
||||||
|
|
||||||
def create(pin1, pin2, pin3, pin4, delay=2, mode='HALF_STEP'):
|
|
||||||
return Stepper(mode, pin1, pin2, pin3, pin4, delay)
|
|
Ładowanie…
Reference in New Issue