kopia lustrzana https://github.com/zhcong/ULN2003-for-ESP32
upload v1.0
commit
b0d7349b4e
|
@ -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 />
|
||||||
|
 <br />
|
|
@ -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)
|
|
@ -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()
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 835 KiB |
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 5.0 KiB |
|
@ -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)
|
Ładowanie…
Reference in New Issue