Adding LEDs in parallel to BJT H-bridge

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

I'm building my first H-bridge and I'd like to add a small LED to either side of the H-bridge so that I have an indicator from a distance as to whether the AVR is trying to turn the motor clockwise or counterclockwise. I'm planning to use 2N2907 PNP transistors at the top and 2N2222 NPN transistors at the bottom, and tie the left inputs together and the right inputs together. That way I only use two AVR pins rather than 4, and should be able to alternate between clockwise, counterclockwise, high-side braking, and low-side braking. I know this doesn't allow either side to be fully disconnected for "coasting" but I'm fine without that. My last Line Follower used an SN754410 motor driver and I didn't find the coast mode any more useful than braking.

My question is about the current limiting resistor R1 (and R2 on the other side). My motor will use the same voltage as the AVR (4.8 volts) so I'm okay driving the PNP transistors directly from the AVR, and I'm assuming I can use a single current-limiting resistor on each AVR pin rather than one on the base of each transistor. That is, since only transistor on either side is ever on at the same time, I assume I can use 470 ohm resistor and have (5 - 0.7) / 470 = 9 milliamps on the base and hopefully fully saturate the transistor. But some of that current will also go into the LED, right? And how do I calculate how much? If there were no NPN transistor and I had a small red LED with 1.9 volt drop, I would think the 470 ohm resistor is going to drop the remaining 3 volts and actually only allow (5 - 1.9) / 470 = 6 milliamps through it. But again, some of that must go to the base of the 2N2222. And equally important, is the resistor dropping 3.1 volts or 4.3 volts? Would I solve this problem by using three separate resistors - one for each transistor base and one for the LED?

Thanks very much for any help or advice!
BJT H-bridge with LEDs.png
BJT H-bridge with LEDs.png (65.55 KiB) Viewed 869 times

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by zener »

There are some issues there. If you were using mosfets then you could hook the gates together in the manner you show. However with bipolars that won't work. So your last idea of 3 resistors is the way to go. It is basically like this circuit:
dualhbridge.PNG
dualhbridge.PNG (62.38 KiB) Viewed 842 times
Then add the LED with its own resistor as you stated.

My other concern is those transistors are pretty small so I hope your motor is not too big.

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Thanks, Zener! I really appreciate the info - I've added the three separate resistors.
BJT H-bridge with LEDs revised.png
BJT H-bridge with LEDs revised.png (49.72 KiB) Viewed 837 times
As for why that's necessary, I'm guessing it requires some trickier circuit analysis? Figuring out the ESR for the two transistors and the LED all in parallel? With just the two MOSFETs, I assume the voltage they would see on their gates is the same regardless of the size of the resistor? But is that still true with an LED in parallel? But with an LED, we assume the resistor is dropping whatever voltage the LED doesn't, right? I guess I don't understand how to calculate the ESR with LEDs or transistors in parallel. Otherwise I'd think rather than having three 1K resistors I could get the same effect just by placing one 333 ohm resistor (330) before the branches?

(Sorry if this is a noob question or there's an obvious answer, my only electronics knowledge is from reading hobby books and websites over the past couple years.)

As for motor size, I'm using Pololu's micrometal motors with a stall current of 360ma at 6V. The transistor datasheets say the 2907 can handle 800ma and the 2222 can do 1A, but is that optimistic? I'm working on a revised version of my little LineFollower (which Adafruit Tweeted about a couple weeks ago - http://www.instructables.com/id/2pi-Line-Follower/). My first version used an SN754410 and a 7.4V battery that was boosted up to 9volts, so I think that means the stall current was 540ma (50% higher than at 6V). That version was much too fast for me to control so I ended up driving the PWM at 20%, which gave the motors little torque or control. For the new version I want to try running off a single-cell LiPo battery (Adafruit #258) without any boost converter, so both my AVR and motors will just be at 3.7-4.1 volts (depending on battery charge). With the lower voltage, I need to build my own dual H-bridge, and I'm guessing the stall current at 4volts will only be 240ma (33% lower than 6V)? So maybe I could even use the 3904/3906 like in your picture, although I figure the 2222/2907 will be more efficient and cost the same. I'm hoping I'll be able to run at PWM speeds much closer to 100%. I was considering using high-performance 4amp Zetex transistors like David Cook recommends on RobotRoom.com, although again, the higher cost seems like a waste at this low-voltage? Or at this low-voltage maybe I need the most efficient transistors I can find to get as much of the 4 volts to the motor as I can?

Anyway, thanks again for your tips and advice!
-Mark

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by zener »

As far as 3 resistors vs 1, the issue is not current, it is voltage. Each transistor wants a diode drop across its emitter-base junction. So the PNP wants to see 4.6V on its base while the NPN wants 0.6V on its base. But they are connected together. So what will likely happen is they will split the difference and blow up shortly thereafter.

As far as the transistors. There is more than one spec to the transistor. There is a max current, but there is also a max temp and a max power, and de-rating. If these are metal cans and you have heat sinks then you might be OK. I recommend not touching them with your finger while it is running, if you like your finger. They will probably be pretty hot. Worst case they blow up and you rebuild with bigger ones.

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Hey Zener-

Does the dual-bridge circuit you show work? I've had a lot of problems with shoot-through - sometimes more than 3 amps! I hadn't realized that when the chip was being programmed or first turned on, the control pins still configured as inputs and apparently that causes a short with this design. I think I'm also getting some shoot through anytime the PWM is alternating and the transistors are turning on and off, which causes my bottom right NPN one to get so hot it smokes and blistered my finger (as you warned me it would!). I'm using ZTX968 and ZTX1049A transistors which supposedly can handle 4 amps despite the small package size. I had hoped that switching them to MOSFETs would solve the problem, but on AVR Freaks I was told I'd still have the issue and really need to have separate pins to control each transistor, which means I'd need 8 pins to drive two motors. Ugh.

Anyway, I was wondering if you've had luck with the similar version you posted with 3904/3906 pair? Thanks for any advice.

-Mark

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by zener »

Sorry, I just grabbed that circuit off the web somewhere. I can see how a floating input could allow both transistors to turn on. I disagree that switching to mosfets could not solve the problem, or be part of the solution. In that case a pull up (or pull down) could take care of the floating input problem. However, you would still need to control shoot through during transitions. This is why people often use IC's that are designed for this exact task. However, maybe someone else has some ideas for you. If I find a better circuit I will post it.

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by zener »

OK, here is a list of 22 different circuits. I kind of like #19...

http://www.talkingelectronics.com/proje ... dge-1.html

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Thanks, Zener, I really appreciate it. My original version of the Line Follower just used the SN754410 motor driver. I thought it'd be fun and simple to make my own H-bridge and that it'd also be more efficient if I wanted to run my robot from a single, light-weight LiPo cell. Since PNP and NPN transistors use opposite inputs, I couldn't understand why everyone didn't just use the same design I did - i.e. one input to each side of the H-bridge, figuring only the top or the bottom transistor could ever be on at the same time. I didn't realize that they could both be on at the same time during switching or that they'd leak current if the base input was floating. Since a Line Follower never really needs to drive backwards, I'm considering getting rid of the H-bridges all together and just suing a single transistor for each motor with version 3.

Oh well, I guess part of the fun is all the failures along the way. I'm collecting a stack of useless PCBs and components that I sometimes try to desolder and sometimes don't bother. I also didn't realize that my 3.7V power supply and 20MHz crystal would not be a good match either, forgetting that 20MHz requires at least 4.5V. Nor did I understand that the crystal's 22pF caps are supposed to have their own private connection to ground and that the the crystal needed to be physically right next to the XTAL inputs. So lots of mistakes to learn from.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adding LEDs in parallel to BJT H-bridge

Post by adafruit_support_mike »

You can make a well-behaved H-bridge with one microcontroller pin, four gates from a 74HC14 hex Schmitt-trigger inverter, and this non-overlap circuit:
h-bridge.jpg
h-bridge.jpg (28.7 KiB) Viewed 767 times
The diodes, resistors, and capacitor on the left form an alternating delay network.

If we assume the input starts HIGH, the bottom of the lower diode will be about 0.6v below Vcc. The capacitor will charge to the same voltage through the lower resistor, and the upper path will be tied to that voltage through the upper resistor.

When the input goes LOW, the upper diode pulls the upper path down to about 0.6v instantly. The lower diode is reverse-biased though, so the only connection to the lower path goes through the lower resistor and up to the capacitor. It takes a little while for the cap to discharge though the upper resistor and diode, so the low-going edge seen by the lower path lags behind the low-going edge seen by the upper path.

When the input goes HIGH again, same basic thing happens the other way. The lower diode pulls the lower path HIGH instantly, but the upper diode is reverse-biased. The upper path's rising edge will lag behind the lower path's because it has to wait for the cap to charge.

The Schmitt inverters act like comparators with built-in threshold voltages. The input has to go higher than about 70% of Vcc (3.5v) before the output will go LOW, then has to fall below about 30% of Vcc (1.5v) before the output will go HIGH again. Those thresholds convert the lagging edges as the capacitor charges and discharges to nice, clean edges driving the transistors.

Squaring up the alternating delays allows the upper pair of transitors to turn off before the lower pair turn on, and vice versa.

The secondary inverters flip the signals so each pair of transistors turn on or off at the same time.

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Wow, thanks Mike. Such a clever design. What values of resistors and capacitor do you recommend if I'm using a 2000mAh LiPo (product 2011)?

Looking at a data sheet for a 74HC14, each pin can source or sink 4ma at 4.5V, so I'm guessing the base resistors for the four transistors should be 1K (figuring ~4V power supply)? Should the ones around the capacitor be smaller in order not to "slow down" the circuit? And I'd need a 200 ohm resistor or higher on the AVR's output pin to make sure it doesn't output more than 20ma, right?

Finally, with this circuit, is the motor stopped by giving it a 50% PWM? And half speed forward would be 75% and half speed backward 25%? Or am I misunderstanding it still?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adding LEDs in parallel to BJT H-bridge

Post by adafruit_support_mike »

For the delay circuit, 1.5k resistors and a 1nF capacitor will give you about a microsecond of lag between one side shutting off and the other coming on, and as far as the electronics are concerned, that's plenty.

To size the resistors between the inverters and the transistors, decide how much current you want to have flowing through the coils, divide that by 50, and choose resistors that will use about that much current when the voltage across them is 4.3v.

You won't need any resistors between the Arduino and the delay circuit. The ones already there will do everything the circuit needs.

For on/off control, you'd be better of using a P-mosfet between the PNP transistors and Vcc. A power mosfet's channel resistance drops to about 0.1 ohms when the voltage between gate and source is more than about 4.5v, so that will give you much better on/off control than the H-bridge itself.

Use the H-bridge for direction control, and for speed control use PWM for the P-mosfet.

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Ah, got it, so for speed control, modulate power to the H-bridge rather than trying to rapidly alternate between forward and reverse. Since the motors will almost always be running at less than full speed, is this design with a microsecond delay still one you would recommend? That is, will the delay kick in every time the PWM cycles the motor off for a fraction of a second, or is it only there in cases of switching between forward and reverse? (Which I'm not sure will actually come up except when I spin the robot in place left and right to measure where the line is, just as Pololu's 3pi does.) Still great to have the built-in shoot-through protection, but I'm curious if it is only needed when switching directions or also useful or necessary when the bridge turns on and off to allow for 80% speed for example.

Also, when the direction control pin is high-Z (during programming and while starting up), will the motors turn either direction? That is, assuming the outputs of the left and right inverters are floating, does that allow the PNP transistors to turn on which then turns on the NPN ones? Or does this circuit never allow that? (Sorry if this should be more obvious, I'm staring at your schematic trying to figure out how current flows when one part is high-Z, which is one of the things that got me in trouble with my previous design.)

Also, just to make sure I understand, the key to the timing is the Schmitt trigger which creates hysteresis, right? That is, 7404 or 7406 hex inverters without the Schmitt triggers would not work, right? (I ask since I have some 74HC04 and 74HC06 chips. I'm pretty sure from your original explanation that I should order a 74HC14 though.)

I've put your schematic into the H-bridge layout that I'm used to, and also added a P-channel MOSFET for speed control. Does it look like I got it right? I'm assuming I still need flyback diodes to protect the four transistors whenever the motor powers down. I used Schottky diodes before to minimize voltage loss and did so for the far left diodes in your diagram as well, although I guess I could use even small signal diodes there, right?

Thanks again for all your help. I really appreciate it.
Hbridge-PWM.png
Hbridge-PWM.png (68.34 KiB) Viewed 743 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adding LEDs in parallel to BJT H-bridge

Post by adafruit_support_mike »

markmoran wrote:Ah, got it, so for speed control, modulate power to the H-bridge rather than trying to rapidly alternate between forward and reverse.
Yup.. much cleaner.
markmoran wrote:Since the motors will almost always be running at less than full speed, is this design with a microsecond delay still one you would recommend? That is, will the delay kick in every time the PWM cycles the motor off for a fraction of a second, or is it only there in cases of switching between forward and reverse?
The microsecond non-overlap time will only occur when you change directions.

The PNP and NPN transistors will remain open even if there isn't any current available to flow through the collector-emitter path. As soon as you make current available, it will flow.

Also, the microsecond non-overlap period is much faster than the PWM signals you'll use. Those are usually down in the kilohertz range.

The H-bridge direction control and P-mosfet speed control are more or less independent of each other. Only one H-bridge path will ever be open at any given time, but both paths look identical to the P-mosfet. The base voltages that control the H-bridge are independent of the P-mosfet, so the BJTs will be ready to conduct whatever current is available at the moment, whether the P-mosfet is open or closed.
markmoran wrote:Still great to have the built-in shoot-through protection, but I'm curious if it is only needed when switching directions or also useful or necessary when the bridge turns on and off to allow for 80% speed for example.
Shoot-through is only a problem when you change directions. The problem there is how to go from 'this current path' to 'that current path' without doing 'both' somewhere along the way.

Speed control only involves a single current path, whichever one happens to be active.
markmoran wrote:Also, when the direction control pin is high-Z (during programming and while starting up), will the motors turn either direction?
That's a good question.

The H-bridge control circuit naturally drifts to a state where only one PNP-NPN path is open at any time. It has no reason to prefer one path over the other though, so it would be worth adding a 10k resistor from the direction input to GND so the quiescent state is well-defined.

By the same token, it will be a good idea to put a 10k resistor between VCC and the P-mosfet's gate. That will hold it closed until the Arduino wakes up and starts sending HIGH/LOW signals.
markmoran wrote:Also, just to make sure I understand, the key to the timing is the Schmitt trigger which creates hysteresis, right? That is, 7404 or 7406 hex inverters without the Schmitt triggers would not work, right? (I ask since I have some 74HC04 and 74HC06 chips. I'm pretty sure from your original explanation that I should order a 74HC14 though.)
Honestly, the Schmitt trigger's hysteresis isn't critical to the way the delay circuit works. It's nice to have because it takes a little longer for the capacitor to pass the high/low thresholds, but a single threshold would probably work.

A 7404 would probably work as a drop-in replacement for the 7414, but a 7406 has open-drain outputs. You'd need pull-up resistors on every output to make that one work.
markmoran wrote:I've put your schematic into the H-bridge layout that I'm used to, and also added a P-channel MOSFET for speed control. Does it look like I got it right?
Looks good. Throw in a couple of resistors on the inputs to hold them in known states when there's no other input, and I think you've got a winner.
markmoran wrote:I'm assuming I still need flyback diodes to protect the four transistors whenever the motor powers down. I used Schottky diodes before to minimize voltage loss and did so for the far left diodes in your diagram as well, although I guess I could use even small signal diodes there, right?
You do want the flyback-protection diodes, and Schottkys will work for the input too. Pretty much any diode will work as long as the input voltage crosses the inverter thresholds.

Going all the way back to the title of the thread, you can use the remaining two inverters in the package to control your direction-indicator LEDs.

Connect resistor-and-LED indicators to the outputs of both IC1E and IC1F. Connect IC1E's input to the direction input, and IC1F's input to IC1E's output. By definition, their outputs will always be opposite of each other, so only one of the two LEDs will be on at any given time.

User avatar
markmoran
 
Posts: 24
Joined: Fri Mar 02, 2012 6:47 am

Re: Adding LEDs in parallel to BJT H-bridge

Post by markmoran »

Brilliant. I was hoping I'd still be able to use my directional LEDs with this new design. Although since only one direction is on at the same time, is there any reason to use both inverters rather than just hooking the direction pin into the middle of the two LEDs between V+ and GND? For that matter, do I even need to use an inverter at all? Could I just flip which LED is on top and feed the direction pin directly into the center of the two LEDs, or is having the buffer aspect beneficial? (Assuming I'm not close to maxing out the 20mA from a given AVR pin.)
Direction LEDs.png
Direction LEDs.png (15.61 KiB) Viewed 723 times
Or will that not work? If it does work, I think I can go one step further and use the PWM speed pin to set the brightness of the forward or reverse LED, which would require using an inverter for the ground connection (either the 5th or 6th one available, depending on if I need one on the direction pin above).
Direction LEDs w Speed.png
Direction LEDs w Speed.png (18.61 KiB) Viewed 723 times
Finally, I'm curious what you think of design 19 that Zener highlighted? It also uses two microcontroller pins to allow PWM speed in forward or reverse, although in this case one is dedicated to forward speeds and the other to reverse speeds. (Since your design has one pin for direction and another for speed, only one PWM pin is needed in yours, which is an advantage. But this one just uses two extra transistors instead of the hex inverters to make sure that only opposite corners are on, rather than the hex Schmitt and built-in delay circuit to protect on direction change.) Since my motors stall at 400mA, I don't think I'd use TIP transistors - which I assume are Darlington - but instead regular PNP/NPN pairs. But overall I'm curious what you think of this design and if I'm missing any relative merits?
H-Bridge-6.gif
H-Bridge-6.gif (15.5 KiB) Viewed 723 times
Thanks again for the great advice!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adding LEDs in parallel to BJT H-bridge

Post by adafruit_support_mike »

Putting an inverter in the center of a two-resistor-and-LED chain will work. Just be aware that left to themselves, both LEDs will light.

A red LED's forward voltage LED is about 1.6v and a green's is about 1.8v. If you connect those across a 5v supply, you'll have 1.6v left over, which will appear across the resistors. Having voltage across the resistors means current flows through them, so the default state for a two-LED stack is "both on".

I've been caught by that in the past, and generally prefer to use one LED per inverter so there are no unexpected current paths. This circuit leaves a couple of unused inverters in the package anyway, and using both doesn't require any more parts than using a single one. I also prefer to move indicators like LEDs out of the main signal path to eliminate the chance of the LED current doing something unexpected in the main circuit.

WRT the circuit published as #19 at Talking Electronics, it will work, but has some subtleties that don't appeal to me.

The NPN transistors on the bottom are controlled by resistors that go to the opposite PNPs. That links the current through the coil to the voltage across the resistor, which makes the whole thing sensitive to the value of the resistor. If the resistor is too large, the transistor won't open as far as it should and you won't get enough current through the coil. If the resistor is too small, it will limit the voltage across the coil, again costing you current through the coil.

The resistor value that works best depends on coil's current-to-voltage performance and the transistor's current gain. Both are hard to predict, and transistor current gain is especially hard to pin down. It's easier to keep the circuit well behaved if the control current and coil current are handled separately.

the circuit also doesn't prevent shoot-through. It's possible to turn both sides on at the same time, though that will only happen at times when you change direction.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”