Voltage comparator help

Thread Starter

matthew798

Joined Jan 16, 2013
38
Hello everyone!

This is my first post!

I was hoping someone could help me out with a voltage comparator (LM339) that i bought yesterday. I am really excited to get it working!

Here is an image of my circuit so far (from a website)


The only difference with my circuit is that I have 333.3...Ω for R3, and am using + and - input and output 2.

My problem is that the LED seems to work as if it was attached to a potentiometer... As i crank up the pot, about half way through, the LED comes on, at roughly the right voltage. The problem is that it comes on EXTREMELY dim, and as i crank the pot higher and higher, it becomes brighter. What I was expecting was a solid 5v or 0v... The 339 seems to be varying the voltage. I don't quite understand what i've done wrong. I know i haven't burnt out the 339 because I looked back over what I did and I never had more than 16mA going through it, wich is it's max.

Thanks in advance!
 

panic mode

Joined Oct 10, 2011
2,715
it is a very simple comparator circuit but this is not image of YOUR breadboard, how can we help with troubleshooting if we don't see what you have done?

are you sure GND and Vcc on both sides of the breadboard are connected to each other? what is the supply voltage Vcc?
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
I know my circuit is the same as the one in the photo because it works. I guess what my question should have been is: Is it normal that the LED will SLOWLY brighten once the minimum voltage (2.5) is reached? Or should the LED have a full 5V as soon as the 2.5v is hit.

It seems as though all the comparator is doing is delaying the effect of the potentiometer until the + input voltage reaches 2.5V. As far as i know, the 339 should give either 5v or 0v, never anything in between.

supply voltage is 5.05v (because I don't have a "FINE" knob).

Vcc and GRND are both hooked up to the power supply and to each other correctly.

 
Last edited:

crutschow

Joined Mar 14, 2008
34,280
Your circuit is likely oscillating around the set point which generates a kind of PWM signal to the LED, and that varies the brightness.

Two things to prevent this:

1. Add a decoupling capacitor (0.1uF) directly across the power to ground pins of the LM339. Never build a circuit without decoupling.

2. Add hysteresis to the set point (small amount of positive feedback) which will generate a small difference in the voltage required to turn the LED ON and turn it OFF (similar to a snap-action switch).

Those two additions should allow the circuit to operate properly with a snap action ON and OFF.
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
Wow thanks for such a complete answer! Before trying either of those i'm going to read about them. I appreciate the answer you gave as it is complete and clear enough to guide me to the necessary resources!

THANKS!
 

Audioguru

Joined Dec 20, 2007
11,248
The datasheets for comparators say that of course they oscillate when the input voltages are almost the same. They say it is fixed when you add some hysteresis that you do not have.

Your LED is not getting 5V because it has a 333 ohm resistor in series with it.
The blue LED probably needs 3.5V so the 333 ohm resistor has about 1.5V across it then the LED current is only 4.5mA.
Some LM339 quad or LM393 dual comparators have an output current of only 6mA maximum.

You have not disabled 3 of the comparators so they might be messing up the one you are using.
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
Your LED is not getting 5V because it has a 333 ohm resistor in series with it.
The blue LED probably needs 3.5V so the 333 ohm resistor has about 1.5V across it then the LED current is only 4.5mA.
Perhaps I haven't quite grasped the concept of LEDs. If i understand what you are saying, the LED drops 3.5V? I find that a bit much. When I hook up my multimeter it's only dropping about 1.6V, wich gives my resistor 3.4V, totalling 10ma. Considering that the LM339 can output 16ma as it's max, I figured this would be a safe bet. In any case, the LED, when the pot is "full open", shines as bright as it should. Keep in mind that the LED i'm using is quite a bit smaller. I'm not 100% sure about it's ratings, but i'm keeping around some lower standard values to be safe.

1. Add a decoupling capacitor (0.1uF) directly across the power to ground pins of the LM339. Never build a circuit without decoupling.

2. Add hysteresis to the set point (small amount of positive feedback) which will generate a small difference in the voltage required to turn the LED ON and turn it OFF (similar to a snap-action switch).
I'll try a .1μF cap tonight, and I think I understand exactly WHY it does what it does. So thanks for that!

As for hysteresis, I am not enirely clear on what the logic of it is. I'll search around a bit to try to learn, but if someone could suggest a good resistor value so i can purchase it with the capacitor, I promise i'll read up on it all tonight!

Thanks to everyone for all the help! I've learnt more from this thread than I did all last semester!
 

Audioguru

Joined Dec 20, 2007
11,248
Perhaps I haven't quite grasped the concept of LEDs. If i understand what you are saying, the LED drops 3.5V? I find that a bit much. When I hook up my multimeter it's only dropping about 1.6V, wich gives my resistor 3.4V, totalling 10ma.
LEDs with different colors have different voltages. A RED one is 1.6V to 2.0V. A blue, bright green or white one is about 3.5V.

Considering that the LM339 can output 16ma as it's max, I figured this would be a safe bet.
Many spec's have a range of output.
The maximum output current from each LM339 or LM393 is different. Some have an output of only 6mA and others have an output of 16mA. The maximum output current is guaranteed to be between 6mA and 16mA.

As for hysteresis, I am not enirely clear on what the logic of it is.
It is positive feedback. When the signal input is slightly more or is slightly less than the reference input then the output suddenly switches very fast so it does not oscillate.
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
It is positive feedback. When the signal input is slightly more or is slightly less than the reference input then the output suddenly switches very fast so it does not oscillate.
Am I correct in saying that Hysteresis is:

When the voltage reaches a certain "limit", the output is set to 1, and the "limit" is immediately reduced in order to accommodate noise in the signal. The same applies in the reverse direction, with the limit immediately rising.

The positive feedback is only activated when the transistor (Q1 in my case) is activated.

Is there an image of a circuit with hysteresis that is clearly labelled so I can try to make sense of it visually?
 

Audioguru

Joined Dec 20, 2007
11,248
Sorry, I am not a teacher.
Hysteresis is positive feedback that ceates a "snap action" when the voltage at the signal input is slightly more or is slightly less than the voltage at the reference input.

When you try it then you will understand it.

EDIT: PLEASE POST A COMPLETE SCHEMATIC (NOT A PARTIAL PHOTO) THAT SHOWS YOUR TRANSISTOR SOMEWHERE.
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
I'm sorry, I got ahead of myself. Q8 (not Q1) is the label for the transistor on an equivalent circuit for the LM339. Q8, in my mind, is the output transistor with an open emitter.

The picture you see is the entire circuit, minus the power supply.

 
Last edited:

SPQR

Joined Nov 4, 2011
379
Am I correct in saying that Hysteresis is:

When the voltage reaches a certain "limit", the output is set to 1, and the "limit" is immediately reduced in order to accommodate noise in the signal. The same applies in the reverse direction, with the limit immediately rising.

The positive feedback is only activated when the transistor (Q1 in my case) is activated.

Is there an image of a circuit with hysteresis that is clearly labelled so I can try to make sense of it visually?
I think I'd like to take a shot at this, since I've been wrestling with it the last couple of months.
So here we go.

In terms of understanding feedback and hysteresis in an op amp circuit, you need to know:

The "+" and "-" terminals have NOTHING to do with electrical positivity and negativity - they are the "non-inverting" and "inverting" terminals.

The general concept of hysteresis is a "resistance/lack of desire to change state".
You can "see" hysteresis in a variety of circuits where current/voltage is slow the move in either direction.
But hysteresis is also seen in a "resistance to change" off of a high value, or "resistance to change" off of a low value.
This is the important way of thinking about hysteresis in the op amp circuit.
So remember
"I'm at 15V, and I'm not going to drop below that unless you hit me with a hammer!"
"I'm at 0V, and I'm not going to go any higher unless you hit me with a hammer".
I'll call that "hammer hysteresis":)

The other things to know about an op amp:
Any difference in voltage - ANY difference - between the "+" and "-" terminals will result in a change in state of the op amp to "maximum" (Vcc) or "minimun" (Vdd).

So look at A in the diagram below:
Note the sine wave on the left has been color-coded such that anything above a reference value is light blue,
and anything below a reference value is light red.

When there is a "blue" part of the sine wave, the output of the op amp will be "blue".
When there is a "red" part of the sine wave, the output of the op amp will be "red".
The "up" and "down" black lines are irrelevant and can be ignored.

In "A" there is no noise, and the op amp is acting perfectly
- a small change in input differential between "+" and "-", leads to maximal differential in the output.

"B" is a bit different. I've added a little noise to the sine wave around the transitions.
Note that at every bit of "noise" the op amp responds instantaneously and changes state from maximum to minimum or vice versa.

Now in "C" there is feedback to the "+" terminal, so that if the output is positive,
there is a stronger input to the "+" terminal, and this emphasizes the positive output
- makes it more difficult for the positive output voltage to go negative.
There is "hysteresis" induced into the system that makes it more difficult for noise to change the output from positive to negative.
"I'm not moving off of Vcc".

Now if the sine wave is light red, the output is low (@Vdd), and this is fed back to the "+" input,
emphasizing the low state of the "+" input, and making it more difficult for noise to force the system to change state.
The hysteresis here is "I'm not moving off of Vdd".

The tricky part is that you can change the amount of feedback by changing the value of the feedback resistor and the input resistor to "+".
You can change the reference voltage to anything you want by a voltage divider on the "-" terminal.

Nuff said...
 

Attachments

Last edited:

Thread Starter

matthew798

Joined Jan 16, 2013
38
Wow great reply!!!

I really think this thread might be worth printing!

Thanks to everyone, i got it working! And I can't wait to hook my arduino up to an IR LED and start converting some analogue to digital :)

I thought i'd share some photos of what my oscilloscope is seeing with the LM339:

First image: light on
Second image: transition point?
third image: light off

Is someone able to explain the middle image? It seems like the scope is picking up such a rapid voltage change that i can't display a wave no matter how fast i set it.



 
Last edited:

SPQR

Joined Nov 4, 2011
379
I thought i'd share some photos of what my oscilloscope is seeing with the LM339:

First image: light on
Second image: transition point?
third image: light off

Is someone able to explain the middle image? It seems like the scope is picking up such a rapid voltage change that i can't display a wave no matter how fast i set it.
I wonder if this is the noise I noted in diagram B above.
Add some feedback and see if it goes away.
 

Audioguru

Joined Dec 20, 2007
11,248
I think the "5V" power supply voltage is not regulated enough so it drops when the LED turns on which reduces the reference voltage. The reference voltage is supposed to be stable.

I am too lazy today to sketch hysteresis in the circuit.
 

Audioguru

Joined Dec 20, 2007
11,248
I think the "5V" power supply voltage is not regulated enough so it drops when the LED turns on which reduces the reference voltage. The reference voltage is supposed to be stable.

I am too lazy today to sketch hysteresis in the circuit.
First the output of the comparator needs a resistor to the positive supply to pull it up when it is high.
Second is adding a positive feedback resistor.
 

KnRele

Joined Jan 7, 2013
20
Is someone able to explain the middle image? It seems like the scope is picking up such a rapid voltage change that i can't display a wave no matter how fast i set it.
To me this looks like it may be related to the power supply, a kind of unhappy feedback loop that runs like this: The comparator turns the LED on, this causes increased current to flow and a slight voltage drop on both inputs, tipping the balance back to where the comparator turns the LED off, then the current from the LED stops flowing, power supply voltage goes up again and the comparator turns the LED on. Repeat a few tens of million times a second.

To help stabilize this, make sure there is a capacitor of 100 nF or so connected right across the power and ground pins on the comparator chip, pins 12 and 3 as per the diagram in post #9.

In addition, I would recommend, as others here, to add positive feedback, which basically means feeding a fraction of the output voltage back to the noninverting output, so that when the comparator goes to either output state it will take some slight change in the input voltages in order for it to change to the other state.

Specifically, I'd put a resistor of about 20 kOhm from positive supply voltage (pin 12) to the output (pin 2) to pull it high when the output transistor is turned off; then another resistor of about 300 kOhm from the output (pin 2) to the noninverting input (pin 5), and then see what will happen.
 
Top