mega1280 hangs when trying to use H-bridge

SETUP: i have a mega 1280 and a L293D H-bridge. The H-Bridge is powered using an external 6v 600mA wall adapter. The ground to the bridge is connected to both the arduino ground and the adapter's ground. The chip logic, enable 1, and enable 2 pins of the h-bridge are connected to the arduino's 5v (i've tried using the 6v source too and that didn't fix my problem). The input1 and input 2 pins are connected directly to digital pins 28 and 29.

PROBLEM: In a really basic program (about 15 lines of code) where I blink an LED and variate pins 28 and 29 between HIGH and LOW, the motors spin up just fine. The program runs, although once in a while the program sometimes slows down and later speeds up.

In a much longer code (about 200 lines) where an LED stays on, an accelerometer and gyro are active, and a serial port operating at 57600bp/s that is constantly sending/receiving, the code just stops in its tracks when enabling pins 28 and 29. HOWEVER, it doesn't hang if i unplug the wires, it doesn't hang if I connect LEDs to the pins instead of the h-bridge, and it takes a long longer for it to hang if I either slow down the serial port or if I connect the h-bridge to pwm operating at less than 120.

I'VE TRIED... 1. ...putting 1.1K and 2.2K resistors behind pins 28 and 29 to see if that would just reduce the amount of current. 2. ...using a brand-new L293D to see if maybe the other one was faulty somehow. 3. ...using a transistor where I used 5v for the collector, pin 28/29 as base, and emitter to the h-bridge input pin. 4. ...same setup as 3 except used a resistor between the input pins and ground 5. ...same setup as 3 except used a resistor between the transistor's base and pin 28/29 6. ...a combination of setup 4 and 5 7. ...using a transistor where pin 28/29 was the collector, 5v was base, and ground to input 8. ...a combination of setup 4 and 5 with 7 9. ...not using an external power source

I would like to point out that the h-bridge seems to be VERY sensitive. If input1 is even slightly grounded while input2 is powered by anything, it will work (and vise versa). Just by having input 1 connected to a digital pin set to LOW while input 2 is connected to ground, the motors will move at full speed. So my point is, I don't need almost any power at all to get this bridge to work.

So any idea of what is wrong and how to fix it?


You have Vs and Vss correctly connected? (Vs to motor supply, Vss to 5V logic supply)

Does you motor only need 600mA max? (sounds unlikely).

Does your circuit have decoupling on the logic and motor supplies to the L293D?

Have you checked that the motor supply voltage (nominally 6V) never falls below 5V even on high motor current transients?

Yes, I quadruple checked that VS and VSS are connected properly and as I stated before, I have tried powering both with the arduino 5v and both with the external 6v.

I'm not sure what the motor's ratings are - all I know is it needs at least 300mA to move with reasonable speed and torque.

If you haven't figured it out by now, I'm not very good with electronics haha. I've heard of decoupling but I don't know what it is. Are you asking if the VS and VSS are both connected to the 6v power source? Because no, they aren't - only VS is.

The motors will still spin even at 4v. The 6v power source is a wall adapter - I don't think it's voltage is going to drop THAT easily from a handheld motor. It takes almost no power at all to make these motors spin up at full speed.

So I just tried 4 more things: If I connect pin 28/29 to the L293D's input 1 and input 2 but I disconnect the chip's 6v power source, the arduino continues to run stable. So in other words, it only freezes if the motors are being powered. Not sure if that makes a difference but I wanted to bring that up. If I connect enable 1 to digital pin 30 and turn that on (along with either 28 or 29), the program runs MUCH longer, but it still eventually freezes. If I connect the L293D's enable pins and VSS pin to the 6v power source while using a transistor on the same power source to amplify the 28/29 signal, it still crashes immediately. I also tried yelling at it, which surprisingly did nothing.

Clearly the motor is overloading the motor’s supply - once this drops below 5V the L293D will force down the logic supply (motor supply must not fall below logic supply, see datasheet). This resets the miroontroller at a guess.

Measure the static winding resistance of your motor - this is how you determine the stall current - if the motor supply can’t provide this then you’ll have to rethink how to power things.

Its actually wise to power logic circuit entirely separately from motor and driver. So use 6V for both power supplies to L293D, add resistors in the logic inputs to the L293D (1k?) and just common the grounds.

In general stall currents are much larger than normal running current in DC motors - 10 to 20 times larger isn’t uncommon for an efficient motor. Current detection and limiting becomes important for larger motors, but with small motors the temporary overloading of power supply is often how the current gets limited(!)

So I tried doing a 5v 20A (yes, 20 full amperes) power source from a computer PSU. The program lived longer, but it still eventually froze after a few seconds. I tried adding 1.1K resistors to the inputs and 1.1K resistors from those inputs to ground, which made the program live even longer, but it still froze. Then, I tried 12v 15A and surprisingly the program froze even sooner - I guess that's a good thing considering my motors shouldn't go that high for very long.

I tied the enable and VSS pins to the external power source as you said - it doesn't seem to have any impact on whether or not the program continues.

I got some info on the motors: They run from 3v-6v, and have a "locked rotor current" of 2.8A at 6v.

So, here's some interesting news:

If I make the motors move by using the external power source and manually connecting wires to the input 1 and input 2 pins to make the motor move WHILE the arduino program is running, the program freezes! I was not expecting that.

So what this means is the code on the arduino has absolutely nothing to do with this problem - I believe it has something to do with the ground, what does anybody else think?

EDIT: I noticed that if I unplug the SDA wire going to my accel and gyro, the program also doesn't crash anymore. This I'm assuming is a very easy fix and caused by too much electrical interference going through ground. What do I do about this?