Drekoooo
Posts: 20
Joined: Fri Sep 11, 2020 9:59 am

stepper motor and l293d motor driver

Thu Jun 17, 2021 6:02 pm

I want to connect a stepper motor to the pi through a l293d motor driver .could you help me out regarding the code .its a 4 point stepper motor I removed the stepper motor from my internal dvd rom .thank you

pcmanbob
Posts: 11784
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: stepper motor and l293d motor driver

Fri Jun 18, 2021 8:07 am

Assuming you have the motor connected correctly and in the same way as I did, try this code

Code: Select all

import RPi.GPIO as GPIO
import time

# Variables

delay = 0.5025
steps = 10

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Enable pins for IN1-4 to control step sequence

coil_A_1_pin = 23
coil_A_2_pin = 24
coil_B_1_pin = 18
coil_B_2_pin = 17

# Set pin states


GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)

# output pin order list

#pinList = [(1,0,1,0), (0,1,1,0), (0,1,0,1), (1,0,0,1)]
pinList = [ (1,0,0,1), (1,0,1,0), (0,1,1,0), (0,1,0,1)]
c = 0 # count for pinlist

# Function for step sequence

def setStep(w1, w2, w3, w4):
  GPIO.output(coil_A_1_pin, w1)
  GPIO.output(coil_A_2_pin, w2)
  GPIO.output(coil_B_1_pin, w3)
  GPIO.output(coil_B_2_pin, w4)

# loop through step sequence based on number of steps
time.sleep (2)
print "go forward"
for i in range(0, steps):

    c = c + 1
    if c == 4:
        c = 0
    print i, c, pinList [c]
    setStep(*pinList [c])
    time.sleep(delay)

print "stop"

time.sleep (3)
# Reverse previous step sequence to reverse motor direction
print "go reverse"

# loop through step sequence based on number of steps

for i in range(0, steps):

    c = c -1
    if c == -1:
        c = 3
    print i, c, pinList [c]
    setStep(*pinList [c])
    time.sleep(delay)
        
print "stop"        
#setStep(0,0,0,0)      
        
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General discussion”