0

i'm tryging to control dc motors with my raspberry pi card. i created two codes in python: main.py : i execute this code to control dc motors util.py : this code is like utility class where i put all my functions and call them from main.py

I have 4 dc motors connected to my RPi but for now i use only Motor1 and Motor2 When i execute the main.py, the 2 dc motors run in Forward, then in backward and then it runs in backward. Normal it should run in Forward, Backward and stop

I have this error:

Traceback (most recent call last):
 File "main.py", line 44, in <module>
       util.Backard()
File "/..../util.py, line 43, in Backard
   GPIO.output(Motor1A,GPIO.LOW)
RuntimeError: Please set pin number mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

main.py

import util
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 23
Motor2B = 21
Motor2E = 19

Motor3A = 3
Motor3B = 5
Motor3E = 7

Motor4A = 11
Motor4B = 13
Motor4E = 15

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

#GPIO.setup(Motor3A,GPIO.OUT)
#GPIO.setup(Motor3B,GPIO.OUT)
#GPIO.setup(Motor3E,GPIO.OUT)

#GPIO.setup(Motor4A,GPIO.OUT)
#GPIO.setup(Motor4B,GPIO.OUT)
#GPIO.setup(Motor4E,GPIO.OUT)



util.Forward()
sleep(10)
GPIO.cleanup()

util.Backward()
sleep(10)
GPIO.cleanup()

util.Stop()

GPIO.cleanup()

util.py

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 19
Motor2B = 21
Motor2E = 23

Motor3A = 3
Motor3B = 5
Motor3E = 7

Motor4A = 11
Motor4B = 13
Motor4E = 15

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

print "Going forwards"
def Forward():
    GPIO.output(Motor1A,GPIO.HIGH)
    GPIO.output(Motor1B,GPIO.LOW)
    GPIO.output(Motor1E,GPIO.HIGH)

    GPIO.output(Motor2A,GPIO.HIGH)
    GPIO.output(Motor2B,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.HIGH)


print "Going backwards"
def Backward():
    GPIO.output(Motor1A,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.HIGH)
    GPIO.output(Motor1E,GPIO.HIGH)

    GPIO.output(Motor2A,GPIO.LOW)
    GPIO.output(Motor2B,GPIO.HIGH)
    GPIO.output(Motor2E,GPIO.HIGH)


print "Now stop"
def Stop():
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor2E,GPIO.LOW)


Forward() 
sleep(10)
Backward()
sleep(10)
Stop()

GPIO.cleanup()

What i do not understand when i execute the util.py the code works perfectly but why the main.py is not working as expected?

Thank you for your help.

1 Answer 1

1

When you import a module all the code outside of classes and defs is executed.

So all the print statement etc. will be executed as part of the import.

I'm not sure if that is what you intend.

The pricipal problem is the call to cleanup() at the end of util.py. If you comment that out your code may do what you expect.


EDITED TO ADD

Remember that any code in an imported module outside of defs and classes will be executed as part of the module import. This is intended for any necessary module initialisation.

You import util.py.

util.py will execute the following code during import.

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22
Motor2A = 19
Motor2B = 21
Motor2E = 23
Motor3A = 3
Motor3B = 5
Motor3E = 7
Motor4A = 11
Motor4B = 13
Motor4E = 15

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

print "Going forwards"

print "Going backwards"

print "Now stop"

Forward() 
sleep(10)
Backward()
sleep(10)
Stop()

main.py will then do its own code

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 23
Motor2B = 21
Motor2E = 19

Motor3A = 3
Motor3B = 5
Motor3E = 7

Motor4A = 11
Motor4B = 13
Motor4E = 15

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

util.Forward()
sleep(10)
GPIO.cleanup()

util.Backward()
sleep(10)
GPIO.cleanup()

util.Stop()

GPIO.cleanup()

So the code is doing exactly what you ask it to do.

8
  • yes i want the print statement to be executed too, i removed the cleanup() in util.py, i have the same error Commented Oct 6, 2015 at 16:36
  • @rpiprojectsberry You need to remove all the cleanups apart from one. You should cleanup exactly ONCE just before you stop using RPi.GPIO (normally at program exit).
    – joan
    Commented Oct 6, 2015 at 17:33
  • I removed all cleanup section from main.py and i kept only cleanup line at the end of util.py, when i run main.py i the motor1 and motor2 run in forward, then in backward and then agin in forward. Commented Oct 6, 2015 at 22:42
  • I cannot resolve my problem, after googling about this error, i saw may persons have the same problem, i added only in util.py this line GPIO.cleanup(), i have the same error Commented Oct 7, 2015 at 16:06
  • I thought when I ran the amended code it ran fine. Could you add the code you are currently using to the question?
    – joan
    Commented Oct 7, 2015 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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