View Full Version : lm339 "open collector" question...

02-24-2007, 03:22 PM
. I'm coming into this "building of neat toys and gizmos" from a sideways direction·(programming) and have a third-grade question...· my very first LM339 comparator.

. actually, I had a TLV3401 in the circuit, first.· I bought them because it was a pain to wire the unused inputs of a quad unit, to ground - so I bought singles.··They were·open collector, also.· It didn't work, either, but maybe it had something to do with "micropower" touted in the datasheet...·· surely an LM339, mentioned·in every one of the electronics books would work the first time...· I'm into printed circuit boards now, so the work of wiring of the unused pins is history.

. all I want it (the LM339 or the TLV3401) to do, is to trigger·an NPN transistor (which will trigger something larger...)

. my barn-door (after the fact) prototype works just fine, too - with 4.96 volts at the output pin when the positive input is higher than the negative input --- and just a few microvolts when it's lower.· But as soon as I connect the base·of the transistor - (at point A) - the voltage drops to .67 (two thirds of a) volt, and the transistor doesn't conduct.

. help ! --- I'm on my third printed circuit board (they're small, but it still takes 90 minutes)...·· I've read any tutorial I could find.· -- maybe an·opto-isolator (?) where you just have to light the little hidden diode·to trigger the switch ? ? ?· --

. thank you.


Post Edited (tommy) : 2/24/2007 8:28:34 AM GMT

Phil Pilgrim (PhiPi)
02-24-2007, 03:50 PM

With the base connected, and when the LM339 isn't "pulling down", the 10K resistor will forward bias the transistor's base-emitter (PN) junction. A silicon PN junction has about a 0.6V forward voltage drop, so that's why you're seeing the 0.67V. This is good and completely normal.

In such a circumstance the transistor should turn on. But in your schematic, you have the LED backwards, which is preventing any current from passing. I'll bet it you reverse it, your circuit will behave as you want it to.


02-24-2007, 04:17 PM
Also, the 10K resistor, which is biasing the transistor and controlling the base current, may be too large (not enough current gets through the transistor to light the LED). Try a 4.7K, maybe even a 1K, resistor and see if that helps. Beware about using a resistor below 4.7K, as you'll start pulling real power off the supply when the -339 goes low and has to fight the low resistance to +5V. "micropower" just means the part pulls microamps of current while sitting switched off (output high). When switched on (output low) it can supply several milliamps, probably even enough to power the LED without the transistor.
We use a mix of LP339s (really REALLY low power!) and LM311s. The -339 is the quad part; the -311 is the single gate comparator.

FWIW, unless you plan on seeing very slow voltage swings (less than a Volt/second or so) at the input , the 1 meg feedback resistor is not needed. In fact, Hill's "Art of Electronics" says that feedback on a commercial op-amp is a. unneeded and b. not recommended. And a single feedback resistor is only effective at the negative input. Feedback to the positive input is a little trickier. However, if you configure a simple op-amp to work as a comparator, feedback can be a real friend (and hysteresis, which is a close cousin).

Hope that helped. Good luck with your circuit building.

Phil Pilgrim (PhiPi)
02-25-2007, 01:53 AM
The positive feedback resistor is fine. It provides hysteresis to the comparator, making it less susceptible to oscillation near the threshold point.


02-25-2007, 04:49 AM
originator's reply:

.·¡·what a stroke of luck ! --- the·LED in the picture was backwards only because I wasn't paying attention while making the picture (using Paint. and Edit - paste from and a directory of·pictures of parts.· The picture of the LED was sideways in the file and I turned it the wrong way while making the drawing...
· so then I check the real LED in my barndoor prototype and would'ja'believe !!! - the diode is backwards there, too.· good golly.· I'm human !

. so now I have a dimly glowing diode on the prototype·- but I can see that the circuit·IS working (and I wouldn't have started this thread if it had been dimly glowing last night...)

. AND --- now I can (with confidence) take voltage readings and figure the current:
afterthefact comment:· ignore the diagram above.· it is not a good circuit.· it was sent to·illuminate the words in the text...·

. point-six volts and 470 ohms is just above a single micro-amp of current though the LED·(through the whole vertical line)·- - no wonder·the original wouldn't pull up the relay on the board ! --· the 2n2222 doesn't produce an "all-or-nothing" output as I had pictured in my mind - the transistor itself is dropping two and a half volts ! -- and I also get to see with my meter that·.67 volts at the base is a normal reading when the circuit is all together.

. I'll bet'cha that back to back NPN's will get my relay to actuate...

.·let·me go now and try that.· that means another circuit board...· ·thank you for all of your other comments - I picked the value of the 10K pull-up from a tutorial - and the 1-meg positive feedback made sense there too, as the fluctuating input·is sometimes·'microvolts' from a slowly swinging temperature-sensing LM134...

. I will post my (indoor)outdoor thermometer-for-the-almost-blind (2½" 7-segment display) within a week.


Post Edited (tommy) : 2/27/2007 3:52:23 AM GMT

Phil Pilgrim (PhiPi)
02-25-2007, 05:16 AM

Before you go to all that trouble, take kjennejohn's advice and replace the 10K pullup on the comparator with something smaller. There's no reason that a single 2N2222 shouldn't be able to sink enough current to turn on your relay.

Another option, which takes better advantage of the comparator's open-collector output would be to use a PNP (e.g. 2N2907) instead of an NPN. The simplest circuit would be to connect it as an emitter follower, with the LED and series resistor on the emitter (positive) side. That way you won't need a base resistor. This would also give the comparator output more swing, yielding more hysteresis.


02-25-2007, 05:29 AM
Actually, that's 1.27 milliAmps going through the LED and resistor. A microAmp would leave it dark.
Now, are you still using a 10K pullup resistor? Have you tried the 4.7K I recommended? It looks like your transistor is not being turned on hard enough.

I checked Digikey and found that the typical TI LM339 will put out 20 mA, and operate from 2.0V to 36V. They also have reed relays that will work at 5V and 12V that need only 10mA to suck up, but can handle 0.5 A to 1.0A. And these relays can be had with spike suppression diodes built in. For less than $3.00. Transistor? We don' need no steenkin' transistor!

The 5V version is P/N 306-1020-ND, $2.54.
The TI LM339 is P/N 296-1393-5-ND, $0.45 each.

I know this works, because we're using the 12V version of the relay, driven directly by a LP339.


PJ Allen
02-25-2007, 08:42 AM
"back to back NPNs"?·· http://forums.parallax.com/images/smilies/rolleyes.gif

02-25-2007, 03:11 PM
originator's reply:

...um, yeah.·· back to back.··· this wouldn't work ?· --· assuming that power from the lm339 was weak.


afterthefact comment:· ignore the diagram above.· it is not a good circuit.· it was sent to·illuminate the words in the text...·

.· I haven't gotten to try anything yet.· ' called away.· but I am·getting much quicker at making little drawings...

. I'm a jack-of-some-trades kind o' guy - and at this point in electronics I'm about 70% book-knowledge and only 30% experience...· I'd like to make mirrors that follow the sun, and mechanical flowers (for the robot's gardens ?) and I'm gonna do it all with SX-28's ! ? !·· <-- there's a question mark, there - and a bit of laughing, too.

. In the late 80's and early 90's I found that (MS-DOS) assembly language consisted of about 50 different "rules and/or proceedures", but the tricky part was that there was an exception to almost every one of them.· Book knowledge alone was not enough.· I'm not surprised that the world of electronics is turning out to be similar.· This is real fun, though - I get to build real things.


Post Edited (tommy) : 2/27/2007 3:53:39 AM GMT

PJ Allen
02-25-2007, 09:46 PM
As a couple before me have posted (correctly insisting) -- you need that pull-up resistor on the LM339 output because it's an open-collector output (it's not like a 7400/4000-series logic gate output.)

02-26-2007, 03:59 AM
originator's reply:

. I'm sorry I omitted the LM339's output pull-up resistor from the last drawing -- I thought that was a "given" and I assumed it to be there...

. now then.· an open-collector output means:··· that many of them can be placed in parallel as inputs to another device...·· because when there is no positive output (the sucking of electrons), there is no pushing out of electrons either - it just sits there in a neutral mode...

. and an open collector output means that it requires a pull-up-resistor.

. and this thread, indeed - is me, trying to learn how to best use that output to trigger a device down the line...·· It has not been obvious from things I have tried to find... (google: lm339 tutorial, etc.)

. I will try all of the above (4700 not 10K // PNP // etc.) in varying combinations over the next 48 hours...· That will be my immediate project - and not the auto-fan I was building.·

· thanks all - results by monday evening.


Post Edited (tommy) : 2/25/2007 11:06:19 PM GMT

PJ Allen
02-26-2007, 04:54 AM
Sorry, but·all that is beyond me.· I wouldn't parallel outputs for current, as you suggest (but maybe you know something that I don't.)

Here's an LM339 configured as a·non-inverting comparator (with hyteresis =·that feedback resistor) straight out of the databook [attached.]

02-26-2007, 06:00 AM
originator's reply:

. well that's what I get for just trying to describe something without posting a picure...

. Here ! I found what looks like is going to be an excellent site:· the text in blue is from there...

······ http://www.hardwaresecrets.com/article/237/10

Open collector configuration has far more applications. The most common is a technique called wired-AND, where the junction works as an AND gate. See the example on Figure 27. The output Y will be equal to (A OR B) AND (C NAND D). The junction will work as an AND gate.



also:· this:

·Open collector means that the transistor used on the output of the gate doesn’t have its collector internally connected to the integrated circuit’s VCC (voltage). So, you have to do this connection by yourself. This means that you need to install an external resistor (called “pull-up”) between the output and VCC to make the circuit work. The good thing is that VCC doesn’t need to be the +5 V power supply. You can install it to a + 12 V power supply and feed your 12 V relay, for example.

The term open-drain is used for CMOS integrated circuits and is exactly the same thing.

·...I'm going to go read some more...


Post Edited (tommy) : 2/25/2007 11:05:11 PM GMT

02-26-2007, 09:27 AM
originator's (final ?) reply...

... I got it !·· I got it !!!·· I understand "open-collectors" One Hundred Percent ! ! !

an open-collector device is a one stroke engine...· any and all "strength and power" is used to pull the output to ground.· When the ground state is no longer called for, the output just sits there and floats where it wants to (or is pulled·away from ground·through a pull-up resistor).


When thinking of something to control down the line ---> it is most advantageous to use the power stroke - the one that pulls to ground.· Okay there - who was it that said, "PNP" ? ?· That was you, Phil - the sixth post from the top.
.· Here is my new circuit:
·The relay pulls down hard and fast - no current wasted flowing through any resistors.· The 1-meg as a pull-up resistor is enough, and wastes no current when the relay isn't being used.· The relay's magnetic field·will not hold up with no current flowing - so in this particular case (not using the positive-going force) I probably don't need the pull-up resistor at all... I can not tell the difference when I pull it out from·the actual circuit on the prototype board.

I shyed away from PNP transistors so far because the NPN's, using the positive stroke, made more sense to my upright mind.·· But now that I understand how to use the negative-going power stroke of an open-collector device - well, I'll be using them whenever they are called for.

.· thanks ! everybody ! !·· end of story.


Post Edited (tommy) : 2/26/2007 2:47:28 AM GMT

Beau Schwabe
02-26-2007, 12:34 PM

Glad you got it working, but it's not likely to work very long unless you use a current limiting resistor.

Page 12 of the reference datasheet below shows this schematic for a single comparator...


When a transistor is "ON" and saturated the voltage across the C-E junction should only be about 0.6V

Likewise the voltage across the B-E junction of a transistor should not exceed 0.6V

Looking at the NPN transistor inside the LM339 and the external PNP transistor, you are driving 9V across
something that really only needs 1.2V

A current limiting resistor between transistors can satisfy the situation, but first you need to know how
much current is required from your relay.

Measure the resistance across your relay without it being in the circuit. Divide 8.4V by the resistance in Ohms.
(Why 8.4V? ... remember that a transistor will drop 0.6V across the C-E junction... 9V - 0.6V = 8.4V)

I = V / R

If R measures 50 Ohms, then I = 8.4V / 50 = 168mA

The worst case hFE (transistor gain) for a PNP 2907 is about 30

To calculate the B-E current requirement divide the current above by 30

Using 168mA above, the B-E current should be about 5.6mA

Now we have enough information to calculate the proper current limiting resistor value.

Rvalue = (Vsupply - 1.2V) / BEcurrent
Rvalue = (9V - 1.2V) / 5.6mA
Rvalue = 7.8V / 5.6mA
Rvalue = 1393 Ohms

Using a 1K or 1.5K resistor should work fine


...Also there should be a reverse biased diode with twice the current rating capacity supplying the relay to prevent any ESD (Electrostatic Discharge) damage.

www.ortodoxism.ro/datasheets/nationalsemiconductor/DS005706.PDF (http://www.ortodoxism.ro/datasheets/nationalsemiconductor/DS005706.PDF)
www.ensc.sfu.ca/reference/data-sheets/PN2907.PDF (http://www.ensc.sfu.ca/reference/data-sheets/PN2907.PDF)

Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Post Edited (Beau Schwabe (Parallax)) : 2/26/2007 5:39:28 AM GMT

02-26-2007, 02:01 PM
originator's reply:

.· Beau ! -- you beat me by 30 minutes -- I came back to add those two exact things...

. I didn't know all of that stuff you said, (and picked a 470 Ω resistor to limit current to 10 milliamps - under the datasheet number of 16 milliamps max) ---·but let me do·your numbers·now...

+++ I have a 5V relay with a 102 Ω coil ---·
++++·· that's 4.4 / 102 =· 43 milliamps required for the coil.
+++++·· divided by 30 (transistor gain) leaves 1.5 milliamps needed at the base

. and plunking into the last part...· (voltage less 1.2)

+++ (5.0 - 1.2) / 1.5 = 2533 Ω

. hmmm - that would tell me that one pin of an LM339 might drive seven of these relays ? ? ?

. but I'm not doing that and would now pick a 1500 Ω relay out of my box and use it as a current limiting resistor.· Here's my corrected picture:
· should I really pick a 2200 Ω one ? ? ?·· I guess I should - [A] I have a conscience, and [B] why else would you have told me all that ?...·· I'm going to have to save your example with my notes.· (thanks).·

. I'm happy to see that I've got the diode facing the right direction...· I have to think hard about that every time (and for years and years, too) - I find that more than half the time I have to go back and fix programming branches...· but it's okay - i'm persistant.

. thank you very much.···I wish to do a whole bunch of things with moving parts and sensors --- now I have some great new tools to work with...· there will be a lot of comparing, and if the SX does it all, the program gets messier than necessary.

. If I hadn't written "(final ?) reply" -- would you have watched and waited some more ? ? ?


Post Edited (tommy) : 2/26/2007 7:18:43 AM GMT

Phil Pilgrim (PhiPi)
02-26-2007, 02:20 PM

A reminder: you've got the emitter and collector swapped in the schematic of your PNP transistor (both places). The emitter (the terminal with the arrow) goes to +9V, and the collector goes to the relay. Just remember: whether NPN or PNP, the positive current flow follows the arrows.


Beau Schwabe
02-26-2007, 03:10 PM

You said...
+++ I have a 5V relay with a 102 Ω coil ---
++++ that's 4.4 / 102 = 43 milliamps required for the coil.
+++++ divided by 30 (transistor gain) leaves 1.5 milliamps needed at the base

. and plunking into the last part... (voltage less 1.2)

+++ (5.0 - 1.2) / 1.5 = 2533 Ω"

Hmmm..... Your driving a 5V relay from a 9V supply. I'm not terribly concerned here,
relays are actually current driven devices and should be rated in VA (Volt Amps) or Watts,
but your formula is wrong.

8.4V / 102Ω = 82mA for the coil

82mA / 30 = 2.7mA needed at the transistor base

(9V - 1.2V) / 2.7mA = 2888 Ω ... Use a 2.7K or 3K resistor

The 1Meg resistor should go to the same supply as the PNP transistor.

Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Post Edited (Beau Schwabe (Parallax)) : 2/26/2007 8:23:23 AM GMT

02-27-2007, 12:02 PM
originator's reply...

. well, no, Beau...·· I initiated that second problem.· the numbers are mine (and you're not allowed to·change them in the middle of the problem)...· my actual circuit starts off with a 7805, and I use 5v throughout the logic circuit and drive a relay with a 5v, 102Ω·coil...·· so I'm going to stick with my original, slightly rounded, answer...··· in this particular "3-hour" (ha-ha) project, the relay's contacts are used to power a small fan, and probably will conduct a different voltage...

. I used a 9v relay in the drawing only to illustrate what I learned about open-collector circuits - namely: that the pull-up resistor may be tied to either voltage source...· it's not critical to be tied to·the LM339's +5v.

. and as for the·upsidedown symbol·- thanks.· sometimes it's hard enough to associate the datasheet's·"1, 2, 3" from one drawing, to the "emitter, base, collector" in the other drawing - I learned that when looking down at the actual, plastic,·parts, that the flat side goes to my left (for both), the NPN goes below the load, and the PNP goes above the load...·· ·(I'll just avoid the ones in the little cans).···· and now (sarcasm) you want me to get the innards of the symbol correct, too ? ?· --- yes, I will.· I will invert my library drawing (it came from my source, upsidedown) within the next ten minutes.
. not only was this my first LM339, but it was my first use of a PNP transistor, too.

. thanks again.· all of you.


added note:· 5:00 am (EST):
.·· Thanks again, for that formula, Beau.· ' couldn't sleep.· ' wrote a quickie program to encompass the numbers...· now when I need it, I'll be right every time (with little (ouch !·) thinking).·· thank you.···

Post Edited (tommy) : 2/27/2007 10:28:27 AM GMT