pull/2/head
zhcong 2018-12-18 15:50:06 +08:00
commit b0d7349b4e
6 zmienionych plików z 94 dodań i 0 usunięć

18
README.MD 100644
Wyświetl plik

@ -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:<br />
![uln2003](img/uln2003.jpg) ![uln2003](img/stepper.gif)<br />

58
Stepper.py 100644
Wyświetl plik

@ -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)

5
boot.py 100644
Wyświetl plik

@ -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()

BIN
img/stepper.gif 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 835 KiB

BIN
img/uln2003.jpg 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 5.0 KiB

13
main.py 100644
Wyświetl plik

@ -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)