Trouble with H-Bridge

So I just built this H-Bridge with some MOSFETs from Sparkfun:

A&D go HIGH for forward
B&C go HIGH for reverse

Here’s my code:

int forward0 = 5;   //input A 
int forward1 = 6;    //input D
int reverse0 = 10;    //input B
int reverse1 = 11;    //input C

int analogInPin = A0;  //potentiometer (motor speed selector)

int sensorValue = 0;
int motorSpeed = 0;

void setup()  { 
  Serial.begin(9600);
  digitalWrite(A1, HIGH);  //can't reach the 5v pin, using A1 for a positive voltage
} 

void loop()  { 

  sensorValue = analogRead(analogInPin);

  motorSpeed = map(sensorValue, 0, 735, 0, 255);

  analogWrite(forward1, motorSpeed);
  analogWrite(forward0, 255);
  analogWrite(reverse0, 0);
  analogWrite(reverse1, 0);

  Serial.print("sensor value = ");
  Serial.print(sensorValue);
  Serial.print("  |  motor speed = ");
  Serial.println(motorSpeed);
  delay(100);
}

Everything works fine with a small 3V toy motor, but when I hook it up to my 9/12V - 3 amp car seat motor, the Arduino crashes, and can’t control the H-Bridge. It just turns the motor off and the Serial Monitor stops reading. I am using the Vin to power my Arduino and motor from a bench top power supply. I replaced the small protection diode by the barrel jack with one that can handle high current, but I’m still having the same issue. Any recommendations?

The arduino is crashing because you have not isolated the motor power from the arduino power. You need to forget whatever plan you had and follow my advice. You CANNOT , REPEAT CANNOT have the motor power and the arduino power tied together for reasons I won't even go into right now. You will just have to take my word for it. Use the 12V bench supply for the motor only .
Is it variable or fixed 12V ? Use the USB cable to power the arduino. If you can't do that for some reason then buy a 9V wall -wart
and plug that into the arduino ext.pwr dc barrel jack. You can order one of these :
5 x AA Holder With 2.1mm Barrel Plug - Solarbotics Ltd. .
Do not post any more about this until you change your power setup. There is no point. Your arduino must have a separate power supply. It's as simple as that. There is no mystery to why you are having a problem. You are not the first to try that and you won't be the last. Put a multimeter on the 12V bench supply BEFORE you attempt another test. When you do the test , watch the meter as soon as you start the program.

Any recommendations?

Yes. Don't do that again.

Thanks, although that was a pretty obnoxious way help.

Thanks, although that was a pretty obnoxious way help.

Perhaps, but you'll live.
If I seemed a bit harsh on you it is because you should have known that already if your breadboarding an H-Bridge.
I have to hand it to you, you're doing pretty good for such a newbie to be building a discrete H-bridge and having it work right off the bat, but if you are going to build circuits, you really should do a little more research first.
Let me explain the reason. The motors are inductors . One of the characteristics of an inductor is that upon the application of power, there is an INRUSH current, that, depending on the size of the inductor, can be quite extreme. What this does to the arduino power is it robs the regulator of voltage long enough for the voltage to spike below the allowable level for the system and it does a reset to recover. As long as your arduino is powered by the USB or an external power source, it should be ok.

If you want a suggestion, add LED indicators to your circuit for DIR (four) and PWM (two). You'll find it handy because if anything goes wrong or when your testing your software, you will immediately see that the direction status is incorrect (or correct) if you have a mistake in your code or if for some reason the circuit doesn't seem to be working correctly you can rule out hardware
or software by looking at the leds. It's faster than checking it with a meter.
Good luck with your projects, and try not to be too sensitive. Just learn from it and move on. No one is trying to insult you. We're just trying to emphasize the importance of something. I think you'll get over it.

xxmamakinxx:
Everything works fine with a small 3V toy motor, but when I hook it up to my 9/12V - 3 amp car seat motor, the Arduino crashes, and can't control the H-Bridge. It just turns the motor off and the Serial Monitor stops reading. I am using the Vin to power my Arduino and motor from a bench top power supply. I replaced the small protection diode by the barrel jack with one that can handle high current, but I'm still having the same issue. Any recommendations?

Well without seeing the whole circuit I've nothing much to go on, but it sounds
like you have massive interference going on, which could indeed be caused by
powering the Arduino from the 12V rail - so that's the first thing to correct if so.

However there can be other routes to interference once you have large currents
being switched, perhaps the next most common being bad circuit layout. Current
loops in high-current circuits are to be avoided since they radiate magnetic flux
which can link with other more sensitive parts of the circuit. In practice make
all cables compact, twisted pairs for instance.

You also must keep high current ground wiring away from the low current
electronics, sharing grounds at a single point is a good plan.

Any recommendations?

Well, there was really no reason to replace the diode. You can try an experiment. Disconnect a PWM lead from the arduino and touch it to the +12v supplying the h-bridge. Does the motor run at full speed? To add to the arduino isolation from the h-bridge, you could put diodes on the PWM lines. Also you might put some large capacitors between the arduino +5v pin and ground. The motor you are driving may well be momentarily pulling the power supply output voltage low enough to cause the arduino to reset.

Using separate power supplies solves the problem, but I'm eventually going to need to use only one power supply, so I'd rather fix the problem.

I purchased one of these opto-isolators from SparkFun, and hopefully that will do the trick? :

The reason I originally replaced the diode is because it only handles one amp, and in my previous circuit, using the Vin to power my motors was pulling 3 amps through it.

Zoomkat - Why do you recommend putting a cap between 5V & ground?

MarkT - Are you suggesting that the MOSFETs I'm using could potentially be generating a magnetic field and interfering with the Arduino because they're too close?

Thanks for the help, it's much appreciated!

Are you suggesting that the MOSFETs I'm using could potentially be generating a magnetic field and interfering with the Arduino because they're too close?

Mosfets don't generate magnetic fields because they are semiconductors, not inductors.
The cap is necessary because any circuit driving motors or switching loads generates negative spikes (uSecs duration) that can cause errors in logic circuitry. the caps help eliminate this "noise".

Zoomkat - Why do you recommend putting a cap between 5V & ground?

It will act somewhat like a battery backup for power on the arduino board. When the motor tries to start, it may pull the single power supply output voltage so low that the arduino board has a power blackout and resets.

Got it. So the cap on the 5V/gnd will hold a small amount of current and possibly stave off a voltage drop, as well as eliminate some excess noise.

xxmamakinxx:
MarkT - Are you suggesting that the MOSFETs I'm using could potentially be generating a magnetic field and interfering with the Arduino because they're too close?

All high-current circuitry generates magnetic fields, but its the rate of change
of magnetic field that induces interference, note - so frequency and magnitude
of the current and the area of any loop in the circuit all contribute. This is the
impact of Maxwell's equations.

The MOSFETs switch the current fast, so the rate of change of current is due
to them.

so frequency and magnitude
of the current and the area of any loop in the circuit all contribute.

What loop ? I think you are talking about an inductor.
Wires carrying current generate magnetic fields which is why they are kept separate from signal wires but I never heard of any one
talking about a power switching device like a mosfet generating magnetic fields. I’m sure there is some magnetic field but it isn’t anything to be worried about and certainly nothing to be talking about. Power devices are separated from other devices on pcbs because the traces carrying their current could affect small signal traces and also because there is some heat involved but location of power switching devices like mosfets is not chosen because of any concern about magnetic fields. All of those concerns are reserved for inductors, solenoids, and motors. If you have evidence to the contrary I would be interested in seeing it because this is the first time I have heard this issue brought up in 30 yrs of working with electronics.

raschemmel:

so frequency and magnitude
of the current and the area of any loop in the circuit all contribute.

What loop ? I think you are talking about an inductor.
Wires carrying current generate magnetic fields which is why they are kept separate from signal wires but I never heard of any one
talking about a power switching device like a mosfet generating magnetic fields. I'm sure there is some magnetic field but it isn't anything to be worried about and certainly nothing to be talking about. Power devices are separated from other devices on pcbs because the traces carrying their current could affect small signal traces and also because there is some heat involved but location of power switching devices like mosfets is not chosen because of any concern about magnetic fields. All of those concerns are reserved for inductors, solenoids, and motors. If you have evidence to the contrary I would be interested in seeing it because this is the first time I have heard this issue brought up in 30 yrs of working with electronics.

Please re-read what I said:

All high-current circuitry generates magnetic fields, but its the rate of change
of magnetic field that induces interference, note - so frequency and magnitude
of the current and the area of any loop in the circuit all contribute. This is the
impact of Maxwell's equations.

The MOSFETs switch the current fast, so the rate of change of current is due
to them.

And yes frequency, magnitude of current and the area of any current-carrying loop
all contribute to the amount and range of interference generated. If you can build a
circuit that isn't a current-carrying loop I'd be very surprised.

I specifically did not say that MOSFETs generate magnetic fields, I said currents
generate fields and that the rate of change of current is what's important and that
the switching devices are responsible for that rate of change.

Take a standard 'scope probe, fix the ground clip to the probe tip, then dangle it
near an H-bridge carrying 60A and you'll see what I mean. Also if the battery
wires aren't run together that forms a big loop which will radiate big-time if
there isn't substantial decoupling at the bridge.

Ok. I understand. You were referring to the dV/dt or dI/dt or Bd/dt . I agree then,

Awesome. Thanks for all the info, this is steering me in the right direction for sure!

One more thing: 1N4004 are really really slow (reverse-recovery time) and thus not suitable for use as catch diodes. You need faster diodes, e.g. a Schottky of some sort. Using slow diodes while PWMing the motor current can put high-voltage spikes back into your DC bus, and can also blow up the FETs by exceeding their Vdsmax (V = L * dI/dt).

polyglot:
One more thing: 1N4004 are really really slow (reverse-recovery time) and thus not suitable for use as catch diodes. You need faster diodes, e.g. a Schottky of some sort. Using slow diodes while PWMing the motor current can put high-voltage spikes back into your DC bus, and can also blow up the FETs by exceeding their Vdsmax (V = L * dI/dt).

Free wheel diodes only have to switch on fast to limit voltage rise,
reverse recovery time isn't relevant surely?

Or are you talking about switching losses with fast PWM as in a DC-DC converter. Motor PWM
frequencies are less demanding I think.

Typical motor PWM frequencies are certainly less demanding than a fast switching converter, but both reverse-recovery and switch-on time are important. For a low-power (and/or low inductance) motor and slow transistors, a plain diode is probably OK but since Schottky's aren't exactly hard to come by, it's best to use them for this purpose.

Considering the case of an H-bridge with 4 freewheel diodes:

  • if the reverse-recovery time is long, there is some shoot-through via the conducting diode and its opposite transistor (on the same leg of the load), which can cause the diode or transistor to fail,
  • if the switch-on time is long, you get high-voltage spikes appearing on your transistors, which can kill them.