L293D motor shield, using transistor logic to route PWM signal.

Hi, I am building a motor shield using an L293D for my sons robot. I want to use PWM to drive the H-bridge and use a standard logic pin to determine the direction the motor turns. To do this, I need to route the PWM signal to one or the other control pins for the bridge and I have come up with using a pair of AND gates and a NOT gate. I am probably 75% sure that this logic would work and have sketched out a small circuit to achieve this. I am less sure about my circuit itself, so I was hoping I could get some constructive feedback please.

The PWM signal is fed into 2 AND gates. The direction signal is fed into one of the AND gates directly, whereas it is inverted before feeding into the other and gate. The AND gates then output the PWM signal dependant on the direction signal. Does this sound correct?

The attached schematic shows how I intend to wire up the logic gates using BC546 transistors. Q1 is an inverter with a pull up resistor. Q2 and Q3 are paired up as an AND gate with a pull down resistor, along with Q4 and Q5. I am not sure if R9 is necessary or wise as R3 already limits the current to Q5’s base.

If this is feasible and not fatally flawed I will duplicate the logic for the second H bridge.

Thank you :slight_smile:

schema.png

You need one transistor only, to invert the direction signal pins 2 and 7 (so that 1A = !2A) PWM to the enable pin.

similar for the other 1/2 chip

want to use PWM to drive the H-bridge and use a standard logic pin to determine the direction the motor turns. To do this, I need to route the PWM signal to one or the other control pins for the bridge and I have come up with using a pair of AND gates and a NOT gate. I am probably 75% sure that this logic would work and have sketched out a small circuit to achieve this. I am less sure about my circuit itself, so I was hoping I could get some constructive feedback please.

I'm not sure why you think this is necessary. The L293 takes two signals per motor. One is the PWM, the other is LOW. The input that is LOW tells you which direction the motor will turn because to turn the opposite direction you reverse the logic and make the one that WAS the PWM input LOW and the one that WAS LOW the PWM input. All you need to know is which pin you are making low. That's in the software. You don't need nand gates to detect that. If you wanted to detect it you could connect two digital inputs to the L293 inputs and check which one is LOW. I've seen dozens of L293 circuits and nobody has ever needed logic gates to know which direction the motor is turning.

@knut_ny, that is a much more efficient way to look at it. The PWM enables the h-bridge, thus rendering the AND gates obsolete, if I understand correctly.

@Raschemmel, the thought behind using the logic gates was to reduce the number of pins used so they could be used in the future for other sensors or outputs. From how I understand your reply, you would need two PWM outputs per motor? I looked at the arduino motor shield and thought that was a sensible use of the pins.

If both these replies are equally valid, it shows there are different ways to approach these things and the more of these ideas I can pick up, the better prepared I will be for my degree.

I had tweaked the schematic because of some slight errors, but with these replies I will revisit and have another go. Thank you.

.. I tried to answer to your problem: one direction pin, one 'speed' pin.

@Raschemmel, the thought behind using the logic gates was to reduce the number of pins used so they could be used in the future for other sensors or outputs. From how I understand your reply, you would need two PWM outputs per motor? I looked at the arduino motor shield and thought that was a sensible use of the pins.

You’re not understanding how the L293 works.
There are THREE pins per motor.
ONE ENABLE/PWM
TWO DIRECTION

ARDUINO - L293

L293 WIRING.jpg

It's indeed best to PWM the enable line. If you PWM a A-line it will switch between motor run when PWM is HIGH and fast motor stop (aka emergency stop) when PWM is low. If you PWM the enable it switches between running and free running. This gives better speed control. You don't see that in the table of raschemmel because the datasheet has an error. The last line should read "free running", not "fast motor stop". When enable is low the output is high impedance thus not actively breaking (but not driving it either).

This leaves 1A and 2A for direction. If you never want to use emergency stop / motor lock (which, depending on the robot construction, might be a handy feature when standing still) you always want 1A = !2A and you could indeed use a simple transistor inverter for that and save a pin.

Or, if both motors always need to turn in the same direction (but different speeds) you could connect 1A to 3A and 2A to 4A. (If the motors now run opposite just swap the connections of one motor.)

FYI, ALL ENABLE pins on ALL motor drivers have one MAIN purpose, which is to DISABLE a motor system in the event of a mishap or mechanical jam or safety issue. Making the ENABLE LOW, is the fastest way to disable the motors if there is a safety issue or mechanical binding. Aside from that , you would NEVER PWM a direction pin because it is NOT a PWM input. Only the EN pins are PWM inputs. The direction pins are only for direction and braking.

Aside from that, the EN pins must be high to drive the motor and double as the PWM input. In case you don't realize it, protecting the safety of the user is a higher priority than operating the system. That's why I say the first function of the EN is to DISABLE, and the second funtion is to ENABLE.

I think I understand it now. The PWM signal now connects to the EN pin, as this is the correct way to control the motors status. The direction pins are solely to be used with logic and as such I have a digital high/low signal controlling these, one of them inverted. The part count is reduced greatly, making the board easier to build, too.

I am extremely grateful for the help and patience shown to me, thank you ever so much.

schema_v2.png

It seems you are unaware of the need for FLYBACK DIODES.
SEE PAGE 7 OF DATASHEET

They should be 1n4002 diodes.
Without them, you could damage the chip with the back emf of the motor.

Sorry, I have the L293D which has the flyback protection built in. I should have been more precise as it is an important point.