EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: jbs on May 25, 2020, 10:07:33 pm

Title: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: jbs on May 25, 2020, 10:07:33 pm
So I have this peculiar problem.

When I hook up a LM339 comparator with two 10K trimpots (A & B), I find that the Output switch from HIGH/LOW doesn't happen at the threshold I set with trimpot A. R5 is the pullup resistor (4K7 Ohm).

I understand why this happens (Thanks to Zero999's help).
Here's a simplified schematic. Trimpot A is represented by resistors R1 & R2 (e.g. R1=10K-R2) and trimpot B is represented by resistors R3 & R4 (R4=10K-R3).
(https://i.ibb.co/1TWPFjK/Screenshot-2020-05-25-at-23-36-23.png)
 
Let's assume the comparator Output is HIGH, then R5 is in parallel with R3 and the two together with R4 are defining a voltage divider (R3//R5 at the top and R4 at the bottom).
When I set a threshold voltage at the LM339 Positive input with trimpot A (R1 & R2), then I can solve for R3 in the equation given by the voltage divider (R3//R5 and R4).

threshold voltage = 5 * R4 / (R4 + (R3*R5/(R3+R5))), where R4=10K-R3 and R5 =4K7.

I guess what I find interesting is that once the switch in Output happens, the voltages across both trimpots don't match. I always thought, that it would suffice to check the voltages across both trimpots at the LM339 pins (pos. input and neg. input), and that they had to be the same.

Hopefully I've made myself clear. Can someone please confirm if I got this right?
Also, is there a way around this, e.g. I would like both trimpots to have the same voltages when the switch occurs. (reading the result directly at the pins).

Thanks,
Joël
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: gbaddeley on May 25, 2020, 10:16:40 pm
Why do you think R5 has any influence on the threshold??
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: RoGeorge on May 25, 2020, 10:21:17 pm
R5 has nothing to do with R1,2,3,4, no matter if the output is high or low.  Only R1,2 and R3,4 dividers are the ones who set the two voltages that will be compared by the 339.

If V pin 1/339 > V pin 2/339, the output V pin 4/339 will be +5V.
If V pin 1/339 < V pin 2/339, the output V pin 4/339 will be 0V.

LTspice can help simulating the circuit and playing with the R1,2,3,4 values to study how it works.
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: jbs on May 25, 2020, 10:38:55 pm
R5 has nothing to do with R1,2,3,4, no matter if the output is high or low.  Only R1,2 and R3,4 dividers are the ones who set the two voltages that will be compared by the 339.

If V pin 1/339 > V pin 2/339, the output V pin 4/339 will be +5V.
If V pin 1/339 < V pin 2/339, the output V pin 4/339 will be 0V.

LTspice can help simulating the circuit and playing with the R1,2,3,4 values to study how it works.

Thanks. Although, this is not what happened when I took the measurements. I'm not sure what I did wrong.
When I measured the voltages at the pins, the positive input was higher than the negative input and yet, the output was staying LOW. Only when I had a delta of .33 V (with 3.3 source voltage) did the switch happen.
It's late now, I will try tomorrow and write more. Thanks again for all your help.
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: jbs on May 26, 2020, 12:39:32 pm
So I have tried again. I'm feeding 5V from an arduino to the circuit.

I measure 2.5V at the Positive Input (Pin 10) and 2.503V at the Negative Input (Pin 9).
The Output (Pin 16) reads HIGH, but it should read LOW, since 2.5<2.503

Only at 2.508 is the switch happening, and the Ouput isn't a steady LOW but it's fluctuating, I guess because of noise. At 2.509 V it is steadily LOW.

Is this behavior normal for a LM339 ? Meaning, is there always a delta?

Edit: I measure the voltage with a Fluke 87V Multimeter, if that matters (can the instrument be to blame)?
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: RoGeorge on May 26, 2020, 01:07:10 pm
Is this behavior normal for a LM339 ? Meaning, is there always a delta?

Yes, it is normal behavior, the small difference you see is caused by the internal "input offset voltage" of the 339 comparator.  Look in the datasheet of the 339 comparator http://www.ti.com/lit/ds/symlink/lm339.pdf (http://www.ti.com/lit/ds/symlink/lm339.pdf) in the table called "7.7 Electrical Characteristicsfor LMx39and LMx39A".  Note that there is also an "input offset current", too, not only "input offset voltage".  And apart from offset errors, there is "input bias current".

- input offset voltage
- input offset current
- input bias current
must all be taken into account as unknown values (but they are all less than the max guaranteed value written in the datasheet, i.e. max 4mV input offset voltage guaranteed, but can be as well only 1 mV, or maybe -4mV).

Of course, the measuring instrument has some internal resistance, too, and that can contribute to small measurement errors when you measure the divider voltage, but in your case this is negligible.

For your circuit the most errors are caused by the input offset voltage.  Input offset current and bias current start to produce errors only when you use very high value resistors for the dividers, for example in the megaohms range (guess why offset and bias current doesn't matter much for dividers made out of low value resistors - if it's not clear why, ask, this is an important step to understand).

Note that the same circuit, i.e. 339, might have different max offset and bias depending on what company made the circuit, depending of the ambient temperature, or the supply voltage, etc.
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: jbs on May 26, 2020, 05:45:19 pm
That's very informative post. Thank you very much. The LM339 datasheet mentions a max input offset voltage of 5mV, however, my delta is more like 9-10mV.
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: RoGeorge on May 26, 2020, 06:12:11 pm
Connect the DMM between pin 1 and 2 of the 339 and use a LED or some other indicator to read the state of the 339 output.  Leave the DMM connected while you are operating the voltage dividers.

- at what voltage (between pin 1 and 2 of the 339) does the comparator starts to shift it's output state?  Make a table with a few measurements and post the results here.
- what value does those trimmers, or R1,2,3,4 have (with approximation)?
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: Benta on May 26, 2020, 06:46:06 pm

Only at 2.508 is the switch happening, and the Ouput isn't a steady LOW but it's fluctuating, I guess because of noise. At 2.509 V it is steadily LOW.


Congrats, you've just learned why hysteresis is almost always built into comparator circuits. Try adding a 1M resistor between output and non-inverting input, You'll be astounded at the difference in stability. However, the switch voltages will shift of course, and will be different at "off" and "on".
Title: Re: LM339 and trimpots. Threshold inaccurate. Normal?
Post by: Doctorandus_P on May 28, 2020, 03:42:06 am
It's not only noise.


When the output of the LM339 switches, the bias currents through the input pins change, and these bias currents create an additional offset voltage that can lead to oscillations. If the resistances on the inputs of the LM339 gets bigger the offset gets worse.