Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: greywanderer012345 on June 16, 2013, 09:57:08 PM

Title: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 16, 2013, 09:57:08 PM
    I'm designing a new setup for my son's HO train set. I've built the $50 robot, and want some more circuitry experience, so I'm including some automation. I wanted my son to be able to reach all points, so instead of setting up on a table, I'm putting it together on shelves along 2 walls of his room.

    My goal is to have the train reverse before it reaches the end of the track without any input to do so. I have my detection idea pretty solid. I always try to do things as inexpensive as possible, so I'm planning on using a photosensor, like those on the $50 robot, watching an LED on the opposite side of the track to detect when the train crosses track towards either end.

    When the train reaches this point, I want to power down the track over the course of about one second, wait about one second, then increase the power, but with opposite polarity on the tracks, so that the train takes off in the opposite direction.

    Before putting the concept down on paper, I thought I could pull off switching the polarity with a couple transistors, but when I started trying to build the circuit (on paper), I realized it would end up having to be an H-bridge, since the rails are essentially motor leads. (I don't have any experience with H-Bridges or transistors other than reading about them on this site and wikipedia when I got curious about controlling larger motors with an MCU)

    I thought that I simply needed to find an H-bridge online that could handle the 18V 25W circuit and receive the 5V gate voltage from my atmega328 board originally built for the $50 bot. However, after searching through Digikey for an hour or so, I'm not sure I know what I'm looking for anymore.

    Does anyone have any suggestions on finding a suitable H-Bridge for this project, or suggestions on any simpler path to take? I also thought of making the H-Bridge described by admin, but I run into the same kinds of confusion with looking for MOSFETS.

////////////BASICALLY THE END OF MY QUESTION, BUT I'M ADDING ADDITIONAL INFO BELOW.(Thank you and you're welcome, Soeren. :D ) ///////////

    Specifically, I now understand that Vgs is the voltage that would turn a MOSFET "on" all the way and get the smallest resistance, the gate to source voltage. I'm a little confused about Vdss though. I've seen the definition. It's the Voltage between drain and source. This makes sense in a circuit -in mine this would be 18V, right? But what does this mean in the specs of an H-Bridge or MOSFET: maximum, minimum, recommended voltage, or what? Can I use an H-bridge with 30V Vdss in the specs for my 18V circuit?

    Should I be looking for a motor controller, rather than an H-Bridge? I understand the idea of how an H-bridge works to control the voltage going to leads. What else does a motor controller do? I just looked them up and they seem to have varying numbers of pins, all more than an H-bridge though.

    Also, I really can't wrap my head around N-type, P-type, NPN, and PNP. What are the these?

    Thanks in advance for any info!
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 16, 2013, 10:01:08 PM
http://www.digikey.com/scripts/dksearch/dksus.dll?pv129=13&FV=fff40027%2Cfff802d2%2C1140050%2Cc100070%2Cc1001d8%2Cc1001df%2Cc1001ec&k=h-bridge&mnonly=0&newproducts=0&ColumnSort=0&page=1&quantity=0&ptm=0&fid=0&pageSize=25 (http://www.digikey.com/scripts/dksearch/dksus.dll?pv129=13&FV=fff40027%2Cfff802d2%2C1140050%2Cc100070%2Cc1001d8%2Cc1001df%2Cc1001ec&k=h-bridge&mnonly=0&newproducts=0&ColumnSort=0&page=1&quantity=0&ptm=0&fid=0&pageSize=25)

That's the only motor controller I've found on Digikey that I'm pretty absolutely sure would work for what I'm trying to do. They don't have any, but hopefully this can give someone an idea of where I'm going wrong/right.
----------------------------------------------------------------------------------------------------------------------------

http://www.digikey.com/scripts/dksearch/dksus.dll?pv606=17&FV=fff40015%2Cfff80346&k=h-bridge&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=25 (http://www.digikey.com/scripts/dksearch/dksus.dll?pv606=17&FV=fff40015%2Cfff80346&k=h-bridge&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=25)

These products are also confusing me. They would seem to work, until you get to the max power, which is not above 2.5W, but they all suggest higher voltages than I'm using, so the amperage is in the right range.
-----------------------------------------------------------------------------------------------------------------------------

What is meant by "Voltage - Supply" and "Voltage - Load" on Digikey?  I'm guessing that "supply" is the gate voltage range and "load" is the voltage range of what is being controlled...?
-----------------------------------------------------------------------------------------------------------------------------


Am I getting close? Even if I'm right about any of these working, please let me know. Even if I guess right, I won't know that I've guessed right, right?
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 16, 2013, 11:03:09 PM
http://www.digikey.com/product-detail/en/A4975SB-T/620-1435-5-ND/2783714 (http://www.digikey.com/product-detail/en/A4975SB-T/620-1435-5-ND/2783714)

So, this is what I would get if I didn't have the great members of Society of Robots to tell me exactly how wrong I am. So, how wrong am I?
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: jkerns on June 17, 2013, 08:42:49 AM
First question - will an H bridge work for this application? Many HO trains run on AC power and just switching the two leads via an H bridge will do nothing.  Have you tried switching the wires manually to see if it will, in fact, run backwards?

Oh - back to your questions. An L298N should have enough poop to run your train and can be controlled via 5 volt logic from a micro controller.  Assuming an H bridge will work.

Quote
Also, I really can't wrap my head around N-type, P-type, NPN, and PNP. What are the these?

Different ways the internals in a FET (N, P type) or bipolar transistor (NPN, PNP) are arranged.  The primary difference is wether you pull the gate/base high or low to turn the transistor on.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: jwatte on June 17, 2013, 10:36:33 AM
I agree with jkerns: Back when I had HO scale toys (30+ years ago) the command to switch direction was twisting the AC transformer all the way to the bottom, which sent a special voltage spike. The direction wasn't controlled by polarity.

If an H-bridge will work, the I suggest buying an existing motor controller on a circuit board, rather than trying to build your own from scratch. Pololu.com has several varieties with various levels of capability in voltage and current, you can find something reasonably priced that does 18V 2A -- again, assuming you're talking about DC here.

Then it comes to finding parts on Digi-Key, most motor controllers do not do the actual switching of the current themselves; instead, they are intended to drive four MOSFET transistors that actually implement the H-bridge. The controller chip takes care of things like preventing cross-conduction, charging the gate for high-side N-channel MOSFETs, etc.

PNP and NPN are bipolar transistors. These let a amount of current through their collector/emitter junction proportional to the amount of current they let through the base. What proportion that is is measured through the hFE of the transistor. For high-current transistors, it may be as low as 10x. For signal transistors, it's often 100x. So, to let 2A through a bipolar power transistor, you may need to let 200 mA through the base. This is both hard to do from a microcontroller (where allowable currents are in the 2-20 mA range) and pretty wasteful. To drive 200 mA through the base, you will want a buffering transistor that takes 2 mA in and lets 200 mA through.

N-channel and P-channel are MOSFET transistors. These let current through their drain/source junction proportional to the charge at the gate. Actually, the proportional range is very small -- typically, these are either considered "on" or "off." An N-channel needs a voltage higher at the gate than at the source; a P-channel needs a voltage lower at the gate than the source. There are "logic level" MOSFETs that work fine with + (or -) 5V; however, really high-power MOSFETs will want 10 V differential to perform well. MOSFETs also work as a diode in the reverse direction; they will always conduct (but with a diode voltage drop) in the reverse direction. Once switched "on" by charging a gate, a MOSFET will draw very little current on the gate, and the conduction resistance will be very small for a power MOSFET, so they won't heat up very much. The problem is that the resistance is higher in the "transition" region, which means if you stay in the transition region (not fully charging the gate) for too long, it will develop too much heat, and blow up. Thus, you typically use buffer transistors or MOSFET gate driver chips. Motor controllers are a special form of MOSFET gate driver chips.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: johnwarfin on June 17, 2013, 11:23:31 AM
I always try to do things as inexpensive as possible,

the lowest cost solution is to use cheap fets for your h-bridge. in most cases nothing besides these two components is required. one p and one n for each of the 2 legs. one pulls up, the other pulls down. only one is on at a time. since you like digikey heres a couple part numbers:

568-5956-1-nd si3202 n-fet
irf7425-nd 7425 p-fet

ive used these with great success with 5s (18.5v) rc motors. would probably work fine for my n-scale trains too which are dc but havent actually tried. you did get me thinking though.

Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 17, 2013, 02:25:22 PM
Great information everyone!

I'm thinking I'll be getting the FETs suggested by johnwarfin.

I'm still confused about how an H-bridge is set up. I'm looking at the basic concept as explained by admin here:
http://www.societyofrobots.com/schematics_h-bridgedes.shtml (http://www.societyofrobots.com/schematics_h-bridgedes.shtml)
and also considering what's been said here.

Forgive my lack of comprehension; I'm only a hobbyist. I still don't understand why each leg needs one P and one N. Are they both turned 'on' by a supplied voltage and 'off' at 0V, or is one turned off by supplying a voltage?

Do I need to get a motor controller or MOSFET drivers too, or will my MCU board and the 4 mosfets be everything? What about the capacitors suggested by admin's sketch? The track is 18VDC for sure, and there's already enough resistance in the circuit that nothing blows even if the track is shorted out.

Also, why does admin suggest adding a resistor before the gate?

Quote
First question - will an H bridge work for this application? Many HO trains run on AC power and just switching the two leads via an H bridge will do nothing.  Have you tried switching the wires manually to see if it will, in fact, run backwards?

The control box has a physical switch that just reverses which rail is 18V and ground.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 17, 2013, 02:39:09 PM
I think that the "Power-Max" Spec was confusing me more than anything on Digikey. The FETs suggested say 20V Vdss and 2.5A continuous drain. However, the Power-Max is listed as 830mW. I was thinking that this spec was the max power that could be going through the FET, but now I'm guessing that this is actually either the maximum power going through the gate, or the maximum power being dissipated as heat by the resistance of the FET. Either way, I probably shouldn't have been so concerned with this spec as I searched.

Also, are there H-bridge ICs that can be bought and have the FETS built into them, or are the ICs just 'drivers' which are separate from the actual transistors?
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: johnwarfin on June 17, 2013, 03:37:55 PM
there are h-bridge ics like mentioned earlier in this thread and some do use fets. but they cost a LOT more than plain fets and personally i prefer the basic circuit you just posted. i suggest you read what the other two fellows just posted on h-bridge operation. its accurate and complete. however imo most of those extra components are only needed for highr power applications. for me the fet only solution has worked well.

you simply need to picture the fets as switches. with  one end of motor shorted to neg and the other to pos it turns cw. reversed it turns the other way. some think there should be a small period (deadband) where neither fet is on. ofr me i just tie the gates together and switch from one bit. true theres small spike but in my case no heating or other issues.

btw an alternate p-fet i use is irlm6401 probably also available from digikey. a little below spec but ive had no problems. these and the 2302 only cost 10-20 cents ea in small qty so tend to be the route i prefer.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: jwatte on June 17, 2013, 04:18:08 PM
A few things:

The maximum power 830 mW means maximum power dissipated. Look at the Rdson (internal resistance when on) value. That's the resistance (at a particular gate voltage value, which may be specified as high as 10V!.) Now, multiply I-squared by resistance, and you get power dissipated. This puts a maximum limit on the current.
You actually have to also look at the thermal resistance of the device. It may be that, at the rated power, it will actually overheat in plain air, and you will need a heatsink to actually be able to dissipate that much.

I think the device seems way under-specified for your case, though. MOSFETs may simply die if they see a higher gate-source or drain-source voltage than they are designed for. 20 V design limit with a 18V inductive load leaves no margin at all, and inductive loads are well-known to generate up to 2x the rated voltage as kickback! I'd use a 30V rated MOSFET at least. Also, use one with a lower Rdson (thus higher amp rating) to have some margin.

The reason you use P-channels for the high switcher is that it will conduct when the gate is more negative than the source (which is tied to the positive voltage.) An N-channel conducts when the gate is more positive than the source (which is tied to ground.) Tying the drain of an N-channel to positive, and the source to the load, using the N-channel as high switch, means that the gate needs to be more positive than the top end of your load -- you need to boost the gate voltage above the positive supply voltage! There are circuits that can do this (high-side N-channel MOSFET drivers) but that's why home-built H-bridges generally use P-channels instead.
Also, if your supply voltage is 18V, and your MCU output is 5V or 3.3V, then that MCU output is not enough to turn off a P-channel. Typically, you will use a pull-up resistor for the P-channel gate, and have the MCU controller drive a small N-channel device that, when open, pulls the gate of the P-channel to ground. This means you waste some current through the pull-up resistor while the P-channel is conducting. Again, with MOSFET driver chips, you can get around this -- look at something like the IR2183 or IR2301 for example (make sure you pay attention to the required supply voltage!)

Finally, again, if the driving current is AC, an H-bridge won't work. Make sure you check the output of your transformer. Ideally, using a scope, but if you don't have one, a multimeter can detect AC simply by measuring the output both in AC mode and DC mode. If it's the same value in both modes, it's likely DC, else it's likely AC.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: waltr on June 17, 2013, 07:15:28 PM
AC or DC depends on exactly which model trains are used. The Lionel and some others use an AC Voltage on the track. Almost all other simple system use a DC Voltage on the track which goes directly to the motor. These can work perfectly fine with an H-bridge and can use PWM to control the motor speed (easy to do which an atmega328.
The newer systems are DCC (Digital Control & Command) which puts encoded data onto the track with an H-bridge. In the locomotive is a decode board that rectifies this 'pulsed AC' to power itself and the motor and also decodes the digital data. The controller uses an H-bridge to put the encoded data onto the track.

For the OP. Look at the H-bridges sold by Pololu. They have a few that should meet your needs.
http://www.pololu.com/catalog/category/9 (http://www.pololu.com/catalog/category/9)

There are a number of small companies that sell track detectors and there is a good bit of info on the web to build your own. There are also a few good model train forums with people that have done what you are looking to do. Here are two forums I like:
http://www.modeltrainforum.com/ (http://www.modeltrainforum.com/)
http://www.modelrailroadforums.com/forum/ (http://www.modelrailroadforums.com/forum/)
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 20, 2013, 02:49:55 PM
in most cases nothing besides these two components is required. one p and one n for each of the 2 legs. one pulls up, the other pulls down.

So, I think I'm finally starting to understand this. pMOSFETS need a lower voltage at the gate than at the source, so to turn them off, you would need to apply the same voltage to the gate as to the source.

So what I need to do is use another nMOSFET and a pull-up resistor at the high end of each leg. When I supply voltage to the nMOSFET, it will drop the pMOSFET gate voltage to ground, and when no voltage is supplied to the extra nMOSFET, the pMOSFET gate voltage will be the same as the source.

I'm pretty sure I get this now. Let me know if I appear to be missing something. Thanks again, everyone!

Also, the track is definitely DC, but I think the engine might be DCC capable. Building a DCC control board could be an awesome project. Thanks for getting me to look into it.
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: jwatte on June 20, 2013, 03:45:54 PM
That sounds about right, IF the voltage you switch is the same as the voltage of your microcontroller output. For example, if your microcontroller output is 5V, and the voltage you switch is 5V, then the microcontroller can make the gate of the P-channel sufficiently positive to turn off the P-channel.

If the microcontroller is 5V, and the switched voltage is 18V, then the gate of the P-channel can see either -13V (5V MCU - 18V source) or -18V (0V MCU - 18V source.) Both of those states are "on" states, and thus you can't turn the P-channel off with the MCU alone.

Again: If controlling an 18V voltage using a 5V MCU output, you can't simply use a P-channel/N-channel pair for the entire half-bridge.

The simplest way to solve this is to use a 1 kOhm pull-up from MCU p-switch gate to source, and an N-channel from p-switch gate to ground, and controll the p-channel gate indirectly through this N-channel swtich. The n-channel gate will see 5V above ground, which will turn it on, which will pull the p-channel gate to ground, which will turn it on. Then when the N-channel gate sees ground, it turns off the N-channel, which makes the pull-up on the p-channel gate bring it all the way up to 18V, which will turn the p-channel off.
Separately, you also hook the gate of the switching N-channel to an MCU output that is the inverse of that which drives the n-channel that drives the p-channel gate.

(http://watte.net/h-bridge.png)
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 20, 2013, 10:16:26 PM
Whoa! I thought I totally had this. I get everything except for that second nMOSFET (Lower right in your diagram) Why is this necessary? Or are you just showing the other lower leg... ... ...oh, wait, that's totally what that is. Thanks for such a complete diagram!

Also, why use 1K pull-up instead of 10k? Shouldn't the gate to source current be small enough that the voltage drop across this resistor is less than threshold?
Title: Re: Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 20, 2013, 10:34:41 PM
I'm going to mark this thread as solved since I got loads of information and an answered question. If anyone has anything to add, please do. Also, if this thread almost answered a question you were searching for, but not quite, feel free to keep asking questions here. Now that the concept has clicked pretty thoroughly, I'm quite certain I could spell it out for anyone.

THANKS AGAIN, EVERYONE!!

Finally figured out what "Power - Max" is on Digikey, and why it's so confusing there. It's the maximum power dissipation, but the Datasheets show 2 kinds of this, Ta and Tc. Because of this, you can't really use this spec to filter or compare on digikey, except to filter by which parts have heatsink mounts. (Tc is always much lower than Ta, so choosing only the low values will show only ICs with a heatsink mount.)
Title: Re: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 23, 2013, 11:16:57 PM
I'm going to finally buy these things. These are what I'm going with:

http://www.digikey.com/product-detail/en/NTD2955-1G/NTD2955-1GOS-ND/1484753 (http://www.digikey.com/product-detail/en/NTD2955-1G/NTD2955-1GOS-ND/1484753)

http://www.digikey.com/product-detail/en/NTD5867NL-1G/NTD5867NL-1GOS-ND/2401422 (http://www.digikey.com/product-detail/en/NTD5867NL-1G/NTD5867NL-1GOS-ND/2401422)

 Please let me know if you see this in the next couple days and see that I'm about to order something that won't work for some reason I haven't accounted for.
Title: Re: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: jwatte on June 24, 2013, 10:16:02 AM
Quote
Shouldn't the gate to source current be small enough that the voltage drop across this resistor is less than threshold?

There are two currents to balance for the pull-up.

First is the current while the signal N channel pulls the gate down. This is wasted heat, and you want it small. 10 kOhm is great for this.

The internal switching resistance in the pull-down N-channel is likely 5 Ohms or less (BS170 value) so you don't really have to worry about being able to actually pull the gate down.

Finally, the switching speed when the pull-up turns the gate off is also decided by the pull-up resistor. You want this to be as fast as possible, both to reduce the amount of time the switch spends in the transition region (warming up) and to avoid cross-conduction with the low leg pulling the control down. A 1 kOhm resistor is actually kind-of too large to make it possible to drive both the N-channel leg and the P-channel leg with the same signal; similarly, if you're using PWM, 1 kOhm is not enough to switch a power P-channel fast enough. I've had to run with 100 Ohm pull-ups to make PWM H-bridges built this way robust before (driving a 5A RC car motor.) If you can find a way to make !MCU turn on later than MCU turns off, and turn off earlier than MCU turns on, for example by using two different comparator values on a PWM timer, then that's probably a good safeguard.

So what's an engineer to do? This is why MOSFET gate driver circuits are a thing! Start looking at IR2101 or IR2183 for examples. Note that those will lock-out if VCC is < 10 V, so not good for cases where the top-level voltage is small, but there are others that work for that case. Also note that those drivers generate a bootstrap voltage *above* the supply voltage, so that you can use an N-channel switch both for high leg and low leg. The trade-off is that you can't keep the high leg on forever; you have to have a PWM duty cycle to keep charging the bootstrap capacitor.
Title: Re: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 25, 2013, 12:15:01 PM
Quote
Finally, the switching speed when the pull-up turns the gate off is also decided by the pull-up resistor.
How does that work? Could you give a reference or explain why the resistance of the pull-up resistor affects switching speed? It shouldn't matter for this project, since it's not using PWM, and the direction is only going to be switched every 10 seconds or so. Thank you, though. This is vital information that would be frustrating to have to discover only after a future project doesn't work as expected.
Title: Re: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: jwatte on June 25, 2013, 07:09:54 PM
So, after the lower-left N-channel turns off, the charge/voltage in the top P-channel gate needs to build up for it to turn off.
This charge is built up through the pull-up resistor.
The capacitance of the gate is actually substantial (can be up to 20 nF for a power MOSFET) and with a 1 kOhm resistor, it can take over a millisecond. With 10 kOhm, it will take 10x longer. And if you run PWM, you will be doing this hundreds or thousands of times in a second -- with a 10 kOhm resistor, it's possible you won't ever turn fully "on" or "off" and spend all the time in the transition zone. That way lies blown MOSFETs. You really do want to spend an absolute minimum of time in the transition zone if you switch any amount of current at all (say, more than 1/20th to 1/50th of rated current) to avoid switching out-heating the conduction heating and causing failure.

Title: Re: [SOLVED] Switch positive and negative (Choosing/Building an H-bridge)
Post by: greywanderer012345 on June 25, 2013, 09:55:05 PM
Thanks for that reply. Quick, simple, and complete. I didn't know that FETs had capacitance and needed to charge to turn on/off. Definitely expands my understanding of the concept, and the importance of the resistor size. I'll make sure to get mosfet drivers or a motor controller for any project I plan on using PWM with.  :)

Update: Ah, after fully considering the implications of the resistor value on PWM, I realised why another suggestion I found makes sense. The suggestion was to keep the upper leg on and use PWM only on the lower leg. This would allow use of a higher value pull-up resistor, saving power, without the switching time problem. This requires 4 pins committed to operating the H-Bridge, though.