1

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:

connection diagram

Also the schematic:

enter image description here

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!!

6
  • you have optical isolation of the control signals ... there is no reason why there should be an electrical connection between the two sections ... remove the ground connections between the control section and the drive section
    – jsotola
    Oct 22, 2022 at 18:15
  • the schematic diagram shows the bottom opto-isolator and the 7404 not connected to ground
    – jsotola
    Oct 22, 2022 at 18:16
  • You are misusing the L293D which is designed for bidirectional control of 2 motors.
    – Milliways
    Oct 23, 2022 at 3:02
  • I don't know what the opto-isolators are intended to do, but if you are seeking isolation this is illusory if there is ANY connection between input & output (including Gnd).
    – Milliways
    Oct 23, 2022 at 3:05
  • Thanks for you comments. @jsotola, regarding the grounds, yes they are connected, although the schematic was not showing correctly, I have edited and added tags instead. WRT the isolation of signals, you are right I shouldn´t need the two grounds together, but I thought I should due to the GPIOs 27 and 22 being connected directly to the L293D. Do you think the should go through optoisolators too instead of connecting the grounds?
    – Guinnesss
    Oct 23, 2022 at 9:01

1 Answer 1

0

Your unusual setup (which includes unnecessary components) will either drive the motors full speed forward or backward. It is more normal to control speed with Enable rather than as an On/Off switch (although you could do so if that is what you want).

The reason it is unpredictable is because GPIO.cleanup() does exactly what the comment # Release resource implies; restoring pin state to what they were BEFORE running the program (most likely INPUT) so both will likely end up with HIGH on the ENABLE pins but this is unpredictable.

5
  • Thanks for the suggestion, I am planning to add speed control as you suggest once the circuit works as intended. I am also planning to add two transistors to convert the output 3.3v from the GPIO to 5v, to be able to reach high speed (since what I understand is that the EN pins of the L293D work in reference to the VCC which is 5v). Regarding the unnecessary components, could you please elaborate? Thanks for the info regarding the cleanup, that makes sense. Is there another way to do it? or the problem is just the circuit?
    – Guinnesss
    Oct 23, 2022 at 9:19
  • @Guinnesss the optocouplers do NOTHING (except consume current) and add no isolation and I fail to see what the inverters add. Using normal GPIO connections gives you more flexibility (which you will NEED if you include speed control). If you need isolation EVERYTHING needs to be isolated i.e. NO direct connections.
    – Milliways
    Oct 23, 2022 at 10:19
  • The circuit, including diodes and optocouplers is derived from this lesson from a kit (edited in the question): docs.sunfounder.com/projects/superkit-v2-pi/en/latest/… they were included in a paper version that shipped with the kit, and are mentioned in the summary section of the online version
    – Guinnesss
    Oct 23, 2022 at 10:49
  • What is the difference between L293 and L293D? letter D in the name indicates an internally fitted diode and it means that we don’t need to add any external components.
    – Milliways
    Oct 24, 2022 at 5:30
  • thanks for that observation. It was helpful to understand what you meant by unnecessary components.
    – Guinnesss
    Oct 24, 2022 at 13:31

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.