1

I want to move a motor with raspberry pi... here is my work I followed this example enter image description here

This is my raspberry pi 3 model b v1.2

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
coil_A_1_pin = 4 # pink
coil_A_2_pin = 17 # orange
coil_B_1_pin = 23 # blue
coil_B_2_pin = 24 # yellow
// adjust if different
StepCount = 8
Seq = range(0, StepCount)
Seq[0] = [0,1,0,0]
Seq[1] = [0,1,0,1]
Seq[2] = [0,0,0,1]
Seq[3] = [1,0,0,1]
Seq[4] = [1,0,0,0]
Seq[5] = [1,0,1,0]
Seq[6] = [0,0,1,0]
Seq[7] = [0,1,1,0]

GPIO.setup(enable_pin, GPIO.OUT)
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)

GPIO.output(enable_pin, 1)

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)

def forward(delay, steps):
for i in range(steps):
    for j in range(StepCount):
        setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
        time.sleep(delay)

def backwards(delay, steps):
for i in range(steps):
    for j in reversed(range(StepCount)):
        setStep(Seq[j][0], Seq[j][1], Seq[j][2], Seq[j][3])
        time.sleep(delay)

if __name__ == '__main__':
while True:
    delay = raw_input("Time Delay (ms)?")
    steps = raw_input("How many steps forward? ")
    forward(int(delay) / 1000.0, int(steps))
    steps = raw_input("How many steps backwards? ")
    backwards(int(delay) / 1000.0, int(steps))

i got this error :

"Traceback (most recent call last):
File "stepper.y", line 23, in <module>
GPIO.setup(enable_pin, GPIO.OUT)
NameError: name 'enable_pin' is not defined"
  • 1
    There is no variable enable_pin . did you forget to declare it or did the code you copied have this variable – Steve Robillard Jul 2 '17 at 0:05
  • can you give me an example please? – Eduardo Herrera Jul 2 '17 at 0:11
  • an example of what a variable? – Steve Robillard Jul 2 '17 at 0:12
  • @eduardo2207fromPanama: This is your code.... Why would you need an example from us? The error is very clear. You are trying to use a variable (enable_pin) that doesn't exist. – Jacobm001 Jul 4 '17 at 3:17
3

Edit : this answer is working for the ULN2803A chip, question has been edited since for another chip.

enable_pin definition is missing in your code. Something like this may have been added just after the import section to solve the issue : enable_pin=18

A better solution is to remove the :
GPIO.setup(enable_pin, GPIO.OUT) and
GPIO.output(enable_pin, 1) lines, as this pin is not connected with the ULN2803A chip.

This pin configuraton was only provided in the adafruit tutorial for compatibility with other stepper chips.

  • this not helped me – Eduardo Herrera Jul 2 '17 at 5:14
  • Applying one of these 2 solutions will surely change the error message. If you face another error message, please make another question :) – Technico.top Jul 2 '17 at 9:46
  • If you still have the same error message, it's may be because you had a problem while writing to the file (permission issue ?). – Technico.top Jul 2 '17 at 9:53
0

It seems the chip you are using : L293D is not the one in the shematic you provided.

You need to connect your components like this : enter image description here https://learn.adafruit.com/adafruits-raspberry-pi-lesson-10-stepper-motors/hardware-l293d

With this chip, you also need to define the enable_pin in your code. Add this just after the import section:
enable_pin=18
And everything will go fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.