I am trying to build my first robot with a raspberry pi 3 B. I am starting with two motors to move the wheels. For that, I am using a L293D and a 7404 inversor, as the L293D requires an inverted input for each motor.
I am using 2 source of power, one for the pi (a 5V usb powerbank represented in the diag by the left side 4 AAA batteries) and 4 AAA batteries for the motors.
The setup works when I run my program, BUT when I stop it, the motors (or randomly one of them) starts moving continuously, which seems weird to me. If I set the two EN pins on the L293D manually to LOW in the program, the two motors stop BUT, when the program stops, there is voltage on those pins, and I don´t know why.
I know I could remove the 7404 and use 2 more pins from the GPIO, but I would like to know why this may be happening as, for me, the whole point of building this is learning!
Another weird thing is that if split the two grounds (currently they are connected together) the problem seems to go away. I would like to understand why. [EDIT] I have just checked again and this doesn´t seem to happen always. When I first start everything up it works, but after I start the program, when I stop it, the motor keeps running. What I have discovered is that if I disconnect pin 9 of the L293D (EN2) to the GPIO22, there is voltage (around 2v) between that pin and the GND pin.
Can you see anything especially wrong in this circuit? any idea why this issue may be happening?
[EDIT]: The circuit is derived from the following lesson: https://docs.sunfounder.com/projects/superkit-v2-pi/en/latest/Lesson_7_how_to_drive_a_dc_motor.html
This is the connection diagram:
Also the schematic:
This is the program running in the pi:
import RPi.GPIO as GPIO
import time
MotorPin1 = 17
MotorPin2 = 18
MotorEnable1 = 27
MotorEnable2 = 22
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by BCM
GPIO.setup(MotorPin1, GPIO.OUT) # mode --- output
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable1, GPIO.OUT)
GPIO.output(MotorEnable1, GPIO.LOW) # motor stop
GPIO.setup(MotorEnable2, GPIO.OUT)
GPIO.output(MotorEnable2, GPIO.LOW) # motor stop
def loop():
print ("Press Ctrl+C to end the program...")
while True:
GPIO.output(MotorPin1, GPIO.HIGH) # clockwise
GPIO.output(MotorPin2, GPIO.LOW)
GPIO.output(MotorEnable1, GPIO.HIGH) # motor driver enable
GPIO.output(MotorEnable2, GPIO.HIGH) # motor driver enable
time.sleep(2)
GPIO.output(MotorEnable1, GPIO.LOW) # motor driver enable
GPIO.output(MotorEnable2, GPIO.LOW) # motor driver enable
time.sleep(1)
GPIO.output(MotorEnable1, GPIO.HIGH) # motor driver enable
GPIO.output(MotorEnable2, GPIO.HIGH) # motor driver enable
GPIO.output(MotorPin1, GPIO.LOW) # anticlockwise
GPIO.output(MotorPin2, GPIO.HIGH)
time.sleep(2)
GPIO.output(MotorEnable1, GPIO.LOW)
GPIO.output(MotorEnable2, GPIO.LOW)
time.sleep(1)
def destroy():
GPIO.output(MotorEnable1, GPIO.LOW) # motor stop
GPIO.output(MotorEnable2, GPIO.LOW) # motor stop
GPIO.cleanup() # Release resource
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the function destroy() will be executed.
destroy()
Thanks!!