From bc7ca57d581dfefb479e66859be35538efc66529 Mon Sep 17 00:00:00 2001 From: yousseftechdev Date: Fri, 14 Mar 2025 14:22:48 +0200 Subject: [PATCH 1/4] updated README.MD, Demo and Stepper.py --- README.MD | 21 +++++++++++++++------ Stepper.py | 26 +++++++++++++------------- boot.py | 8 +++----- main.py | 28 +++++++++++++++------------- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/README.MD b/README.MD index 3013117..d715432 100644 --- a/README.MD +++ b/README.MD @@ -1,13 +1,22 @@ # 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 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) -s1.step(100) -s1.step(100,-1) # or s1.step(-100) -s1.angle(180) -s1.angle(360,-1) # or s1.angle(-360) + +in1 = Pin(16,Pin.OUT) +in2 = Pin(17,Pin.OUT) +in3 = Pin(5,Pin.OUT) +in4 = Pin(18,Pin.OUT) +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) ``` function `angle` is angle, and the PIN map: `IN1` link `PIN_16` diff --git a/Stepper.py b/Stepper.py index 9a1e0ea..4c7dfd9 100644 --- a/Stepper.py +++ b/Stepper.py @@ -1,4 +1,5 @@ import time +from machine import Pin # only test for uln2003 class Stepper: @@ -21,15 +22,17 @@ class Stepper: [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 + def __init__(self, pin1, pin2, pin3, pin4, delay, mode=1): + if mode == 1: + self.mode = self.FULL_STEP + elif mode == 0: + self.mode = self.HALF_STEP else: - self.mode = self.HALF_STEP - self.pin1 = pin1 - self.pin2 = pin2 - self.pin3 = pin3 - self.pin4 = pin4 + raise ValueError("Mode must be either 0 or 1") + self.pin1 = Pin(pin1, Pin.OUT) + 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 # Initialize all to 0 @@ -49,13 +52,10 @@ class Stepper: time.sleep_ms(self.delay) self.reset() def angle(self, r, direction=1): - self.step(int(self.FULL_ROTATION * r / 360), direction) + 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) + self.pin4(0) \ No newline at end of file diff --git a/boot.py b/boot.py index b189fb0..4d5e7d8 100644 --- a/boot.py +++ b/boot.py @@ -1,5 +1,3 @@ -# This file is executed on every boot (including wake-boot from deepsleep) -# import esp -# esp.osdebug(None) -# import webrepl -# webrepl.start() +from main import main + +main() \ No newline at end of file diff --git a/main.py b/main.py index d5673cb..987d5e7 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,15 @@ -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 +from stepper import Stepper + +in1 = 16 +in2 = 17 +in3 = 5 +in4 = 18 +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) \ No newline at end of file From caaf74304dbe4a5b833e3a5fbc8136d3ac271ab7 Mon Sep 17 00:00:00 2001 From: yousseftech Date: Fri, 14 Mar 2025 14:24:38 +0200 Subject: [PATCH 2/4] Update and rename Stepper.py to stepper.py Unlike class names, library names are preferably all lowercase --- Stepper.py => stepper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Stepper.py => stepper.py (95%) diff --git a/Stepper.py b/stepper.py similarity index 95% rename from Stepper.py rename to stepper.py index 4c7dfd9..0595b45 100644 --- a/Stepper.py +++ b/stepper.py @@ -58,4 +58,4 @@ class Stepper: self.pin1(0) self.pin2(0) self.pin3(0) - self.pin4(0) \ No newline at end of file + self.pin4(0) From 66a66698fb1d9d5631716fc8ca762db4496e2845 Mon Sep 17 00:00:00 2001 From: yousseftech Date: Fri, 14 Mar 2025 14:28:00 +0200 Subject: [PATCH 3/4] Update README.MD --- README.MD | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.MD b/README.MD index d715432..85efc16 100644 --- a/README.MD +++ b/README.MD @@ -2,12 +2,11 @@ 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 from stepper import Stepper -from machine import Pin -in1 = Pin(16,Pin.OUT) -in2 = Pin(17,Pin.OUT) -in3 = Pin(5,Pin.OUT) -in4 = Pin(18,Pin.OUT) +in1 = 16 +in2 = 17 +in3 = 5 +in4 = 18 delay = 2 mode = 1 # 0 for half step, 1 for full step @@ -17,6 +16,9 @@ def main() -> None: s1.step(100,-1) s1.angle(180) s1.angle(360,-1) + +if __name__ == "__main__": + main() ``` function `angle` is angle, and the PIN map: `IN1` link `PIN_16` From b37e4450e6a790d5d82bd04490170849a60d8ff3 Mon Sep 17 00:00:00 2001 From: yousseftech Date: Fri, 14 Mar 2025 14:28:29 +0200 Subject: [PATCH 4/4] Update boot.py --- boot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boot.py b/boot.py index 4d5e7d8..7a97160 100644 --- a/boot.py +++ b/boot.py @@ -1,3 +1,3 @@ -from main import main +# from main import main -main() \ No newline at end of file +# main()