# # Importing modules
# from machine import Pin
# from utime import sleep_us
# import Stepper
# # Creating instances
# sw = Pin(16 , Pin.IN , Pin.PULL_UP)
# motor_dir = Pin(0 , Pin.OUT)
# motor_step = Pin(4 , Pin.OUT)
# # Functions
# def direction(d):
# if d == "clockwise":
# motor_dir.value(1)
# elif d == "anticlockwise":
# motor_dir.value(0)
# def rotation(steps,e):
# for i in range(steps):
# motor_step.value(1)
# sleep_us(e)
# motor_step.value(0)
# sleep_us(e)
# def both(steps,d,e=1000):
# direction(steps,d)
# rotation(e)
# steps = 200
# d = "clockwise"
# both(steps,"clockwise",1000)
# d = "anticlockwise"
# both(steps,"anticlockwise",1000)
# # if sw.value() == False:
# # both(d,1)
# # else:
# # continue
# import machine
# import utime
# # Define GPIO pins
# DIR_PIN = machine.Pin(0, machine.Pin.OUT)
# STEP_PIN = machine.Pin(4, machine.Pin.OUT)
# ENABLE_PIN = machine.Pin(17, machine.Pin.OUT)
# SW_PIN = machine.Pin(16 , Pin.IN , Pin.PULL_UP)
# # Enable the motor driver
# ENABLE_PIN.off() # Set to low to enable (if using enable pin)
# button_timer = machine.Timer(1)
# # Set direction
# def set_direction(clockwise):
# if clockwise:
# DIR_PIN.on() # Set direction to clockwise
# else:
# DIR_PIN.off() # Set direction to counterclockwise
# # Step the motor
# def step_motor(steps, delay=1):
# for _ in range(steps):
# STEP_PIN.on()
# utime.sleep_us(delay)
# STEP_PIN.off()
# utime.sleep_us(delay)
# # Control the motor
# def run_motor(steps, clockwise=True, delay=1000):
# set_direction(clockwise)
# step_motor(steps, delay)
# # Example usage
# steps = 200 # Number of steps to move
# delay = 1000 # Delay in microseconds between steps
# clockwise = False
# run_motor(steps, clockwise=not clockwise, delay=delay)
# # def button_pressed(steps,delay,clockwise):
# # if SW_PIN.value() == False:
# # run_motor(steps, clockwise=not clockwise, delay=delay)
# # button_timer.init(period=50 , mode = machine.Timer.PERIODIC , callback = button_pressed)
# # run_motor(steps, clockwise=False, delay=delay) # Run motor clockwise
# # utime.sleep(1) # Wait for 1 second
# # run_motor(steps, clockwise=True, delay=delay) # Run motor counterclockwise
import machine
import utime
# Define GPIO pins
DIR_PIN = machine.Pin(0, machine.Pin.OUT)
STEP_PIN = machine.Pin(4, machine.Pin.OUT)
ENABLE_PIN = machine.Pin(17, machine.Pin.OUT)
SW_PIN = machine.Pin(16 , machine.Pin.IN)
LED = machine.Pin(13, machine.Pin.OUT)
# Enable the motor driver
ENABLE_PIN.off() # Set to low to enable (if using enable pin)
# Set direction
def set_direction(clockwise):
if clockwise:
DIR_PIN.on() # Set direction to clockwise
else:
DIR_PIN.off() # Set direction to counterclockwise
# Step the motor
def step_motor(steps, delay=1):
for _ in range(steps):
STEP_PIN.on()
utime.sleep_us(delay)
STEP_PIN.off()
utime.sleep_us(delay)
# Control the motor
def run_motor(steps, clockwise=True, delay=1000):
set_direction(clockwise)
step_motor(steps, delay)
utime.sleep(1)
set_direction(not clockwise)
step_motor(steps, delay)
step_motor(0, delay)
# Example usage
steps = 100 # Number of steps to move
delay = 10000 # Delay in microseconds between steps
clockwise=True
while True:
if SW_PIN.value() == True:
LED.on()
run_motor(steps,not clockwise,delay)
LED.off()
else:
LED.off()