from machine import Pin, PWM
import time
# Define the STEP and DIR pins
step_pin = Pin(14, Pin.OUT)
dir_pin = Pin(12, Pin.OUT)
def move_stepper_with_accel(steps, direction, min_delay=0.0005, max_delay=0.005, accel_steps=50):
"""
Function to move the stepper motor with acceleration and deceleration.
Args:
steps (int): Total number of steps to move.
direction (bool): Direction of movement (True for one direction, False for the opposite).
min_delay (float): Minimum time delay between steps (fastest speed).
max_delay (float): Maximum time delay between steps (slowest speed).
accel_steps (int): Number of steps to accelerate and decelerate.
"""
# Ensure accel_steps doesn't exceed total steps
accel_steps = min(accel_steps, steps // 2)
# Set the direction
dir_pin.value(direction)
# Acceleration phase
for i in range(accel_steps):
delay = max_delay - (i / accel_steps) * (max_delay - min_delay)
step_pin.value(1)
time.sleep(delay)
step_pin.value(0)
time.sleep(delay)
# Constant speed phase
for i in range(steps - 2 * accel_steps):
step_pin.value(1)
time.sleep(min_delay)
step_pin.value(0)
time.sleep(min_delay)
# Deceleration phase
for i in range(accel_steps):
delay = min_delay + (i / accel_steps) * (max_delay - min_delay)
step_pin.value(1)
time.sleep(delay)
step_pin.value(0)
time.sleep(delay)
# Example usage
print("Moving stepper motor with acceleration...")
while True:
move_stepper_with_accel(steps=400, direction=True, min_delay=0.0005, max_delay=0.005, accel_steps=100) # Forward
time.sleep(1) # Pause for a second
move_stepper_with_accel(steps=400, direction=False, min_delay=0.0005, max_delay=0.005, accel_steps=100) # Backward
time.sleep(1) # Pause for
# Function to move the stepper
def move_stepper(steps, direction, delay=0.001):
"""
Function to move the stepper motor.
Args:
steps (int): Number of steps to move.
direction (bool): Direction of movement (True for one direction, False for the opposite).
delay (float): Time delay between steps in seconds (controls speed).
"""
# Set the direction
dir_pin.value(direction)
# Generate pulses for the steps
for _ in range(steps):
step_pin.value(1) # Set STEP pin high
time.sleep(delay) # Wait
step_pin.value(0) # Set STEP pin low
time.sleep(delay) # Wait
# Example usage
print("Moving stepper motor...")
while True:
move_stepper(steps=200, direction=True, delay=0.001) # Move 200 steps forward
move_stepper(steps=200, direction=False, delay=0.001) # Move 200 steps backward
print("Done.")
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/micropython-i2c-lcd-esp32-esp8266/
from machine import Pin, SoftI2C
from machine_i2c_lcd import I2cLcd
from time import sleep
# Define the LCD I2C address and dimensions
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C and LCD objects
i2c = SoftI2C(sda=Pin(21), scl=Pin(22), freq=400000)
# for ESP8266, uncomment the following line
#i2c = SoftI2C(sda=Pin(4), scl=Pin(5), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
lcd.putstr("It's working :)")
sleep(4)
try:
while True:
# Clear the LCD
lcd.clear()
# Display two different messages on different lines
# By default, it will start at (0,0) if the display is empty
lcd.putstr("Hello World!")
sleep(2)
lcd.clear()
# Starting at the second line (0, 1)
lcd.move_to(0, 1)
lcd.putstr("Hello World!")
sleep(2)
except KeyboardInterrupt:
# Turn off the display
print("Keyboard interrupt")
lcd.backlight_off()
lcd.display_off()