Comparator - resistor/hystereis calculations

Discussion in 'General Electronics Chat' started by SPQR, Dec 22, 2012.

  1. SPQR

    Thread Starter Member

    Nov 4, 2011
    Hi all, me again.
    I've learned a lot :) since I posted THIS thread earlier this month - most importantly, that every part of a digitial circuit needs to be carefully considered and engineered.:eek:

    I focused on the LM393 comparator.
    After searching 20-30 sights on the web, I found that there is a formal way to approach the construction of a LM393 circuit.
    So I thought I would present what I did here, get some comments and ask a couple of questions.

    1. Vcc = 5V Vdd = 0V
    2. Input voltage from 555 is 0-5 V
    3. Arbitrary low and high thressholds of 1V and 3V

    Then using R1/R2 = (Vth-Vtl)/(Vcc-Vdd) = 2/5 = 0.4
    I arbitrarily chose two resistors that met the 0.4 ratio - 1,000 and 2,700 ohms
    Then using Vref = Vth*R2 + Vtl*R1 / (R1+R2) = 3*2700 + 1*1000 / 3700 = 2.5V
    Then calculating a voltage divider to generate the 2.5V reference voltage on "-" (easiest part, 10K each:D)
    So here's the circuit, and when I breadboarded it, it seems to work fine.
    1. Is this the way you do it?
    2. Are there any subtlties that I have missed?
    3. How do you select the feed back resistor?
    __I see many 1meg feedback resistors in circuits, but I don't know why. Mine's only 2.7k
    __Any insight there?

    Again, I thank you in advance.
    Last edited: Dec 24, 2012
  2. tubeguy

    Well-Known Member

    Nov 3, 2012
    Keep in mind that the LM393 needs the pullup because it has an open collector output.
    So, when the output is high the positive feedback is supplied by the 10k pullup plus the 2.7 k resistor. When the output is low it's pulled actively so the feedback is basically just the 2.7k.
    The feedback path forms a voltage divider on the + input with the 1k resistor and whatever the input voltage is.

    The 2.7k and 1k are both much lower than the 10k pull up. What would be the effect of keeping their ratio the same but making them 10x or 20x higher in value ?

    Do you know why you might use hysteresis in a circuit ?
    Last edited: Dec 22, 2012
  3. SPQR

    Thread Starter Member

    Nov 4, 2011
    Thanks for the reply.
    I picture "hysteresis" as a way of "slowing things down" (lagging X with respect to Y) depending on the type of hysteresis.
    (that dates back to my ham radio days - coils and tubes, and all...:))
    I've read that in this particular case, the problem arises in the chip when it transitions the "+"="-" point.
    Apparently the chip is in a no-mans land and can start to do all kinds of funny things, such as resonating (for lack of a better term) or self-oscillation.
    The feedback seems keep the chip out of that transition point for any significant length of time, thus not causing oscillations.

    I'm beginning to better see how voltage dividers are used in a variety of circuits, and I can even do simple calcs.

    I'm concerned about the "arbitrariness" of the resistor selection - the formula just says to "pick two resistors with X ratio" - but there are an infinite number of choices I might make. Thus my confusion (in addition to seeing 1 meg resistors used for feedback in many circuits).

    Again, thanks for the nice answer.

    PS: And yes, I learned about that "open collector" concept about a month ago, and watch out for it whenever possible.
  4. tubeguy

    Well-Known Member

    Nov 3, 2012
    Yes, that's the idea.
    It's called positive feedback because once the comparator switches say to HI, the feedback pulls the positive input a bit more high. This then requires the actual input to go 'more' LO before the output switches LO.

    A good experiment would be to swap the 555 on the input for a 1k-10k pot so you can adjust the input voltage.
    Then experiment with say a 10k input resistor and 10k-1meg feedback resistor
  5. WBahn


    Mar 31, 2012
    One of the primary reasons that hysteresis is used is to prevent bouncing and glitching of the output of a comparator. Imagine you have a single threshold and an input with some noise on it (and there will always be some noise). So the signal is rising, on average, bit it is going up and down about the general rise. So it will probably first cross the threshold when the signal, on average, is still slightly below the threshold but has enough positive noise for the sum to be above it. So the output changes state. But now the noise drops back down so that the sum is below the threshold and the output changes back. This might happen several times before the input's average value is sufficiently above the threshold to prevent the negative going noise excusions from dropping the overall signal back below the threshold. The same thing will happen when the input signal is coming back down. The end result is that the output can "flail" around at each transition point. For many circuits, this is "a bad thing". With hysteresis, as soon as the signal first rises above the threshold, the threshold is quickly reduced to a level that is well below the voltage that any expected noise signal will move the overall input signal below. Similarly, as the signal is coming down, as soon as it goes below the lowered threshold (probably due to some negative noise on top of an average signal this is simply getting in the neighborhood of the threshold), the threshold is quickly moved back up. The greatly reduces the likelihood of getting multiple bounces on the output when there should only be one, clean, signal transition.
  6. ScottWang


    Aug 23, 2012
    Vout = 3.66V + (5.08V - 3.66V)x((1K+2.7K)/(1K+2.7K)+10K)
    Vout = 3.66V + (1.42V x (3.7K/13.7K))
    Vout = 3.66V + (1.42V x 0.27)
    Vout = 3.66V + 0.3834V
    Vout = 4.043V

    If you want to get the Vout more higher, then you should reduce the pull up 10K or raise the Rf(2.7K).
  7. tindel

    Well-Known Member

    Sep 16, 2012
    I see you have the right idea, but I think there's still a bit missing in your theory. You said this is a digital circuit. While the output intent is to be either high or low, this is in no way a digital circuit. A comparator can be used for many things, and is often used as a buffer between the analog and digital worlds, but doesn't need to be.

    Just a side note: All circuits are analog circuits. We use highly efficient arbitration's of analog circuitry to complete digital objectives, but in the end it's all analog.

    As you correctly assumed, the threshold voltage for the + terminal is 2.5V. That means regardless of your transition state the + terminal needs to be 2.5V for the comparator to switch output state.

    Starting with the output low.
    If the output is low then it will be about 0.2V (the internal npn is saturated). Therefore the current through Rf when it switches state is (2.5-0.2V) / 2700 ohm = 0.85mA and your threshold voltage is then (0.85mA * 1000) + 2.5 = 3.35V Once your input rises above 3.35V then your output will go open collector (note that we haven't determined the actual output voltage - yet.

    Now the output is open collector. We still need to cross the same 2.5V threshold at the + terminal. So then we know the current through the pullup and Rf will be (5 - 2.5V) / 12700ohm = 0.196mA. The current will also go through the input resistor so your threshold voltage would be 2.5V - 0.196mA * 1000ohm = 2.30V. Once your input falls below 2.30V your comparator will drive low.

    Therefore your nominal hysteresis will be 3.35V - 2.30V = 1.05V.

    Now what about that pesky output voltage? When the output is low, the comparator is set to about 0.2V, and it will remain there. But what about when the comparator is open collector. Well, theoretically, if the input is 5V then there is no current through Rin, Rf, and Rpull-up, and your output voltage would be 5V. What would the output be if the input were 2.3V - your threshold for going low? It would be 5V - (0.196mA * 10000ohm) = 3.04V!

    So your output can be anywhere from 5V to 3V when you're not loading the output. As soon as you load your output - these values will change, as you saw with your LED. Because of the loading effects, I typically use a high input impedance digital buffer on the output to try to minimize the loading effects of the circuit.

    Armed with this knowledge what do you think a high resistance feedback resistor will do for you? How do you think it will hurt you?
  8. SPQR

    Thread Starter Member

    Nov 4, 2011
    I thank you all for the absolutely superb comments!

    @TubeGuy - This afternoon I'm going to build a little PCB with some pots on it so I can play with multiple resistances in the circuit. I played a little last night and was able to change the brightness of the output LED at a particular thresshhold and also change the thresshold itself. More Experimentation!

    @Wbahn - Very nice explanation of "noise" effects on thresshold. I've had this problem before and learned that a Schmitt trigger is a nice switch debouncer, and over the last couple of days I've learned that a comparator can be made into a Schmitt trigger!:)

    @ScottWang - Again, excellent. I thought that pullup resistors were all the same, at 10K. So this gives me some more options.
    I'll play with your formulae a bit this evening.

    @tindel - Superb discussion. Being a neophyte, I use "digital" to mean "boolean" circuits like AND, NOR, etc. But you are absolutely correct that even "boolean" circuits are truly analog, and subject the the vagaries of noise, and impedance, and capacitance, etc. I've learned a ton of this reading about a variety of "digital chips" over the last few months.

    You mentioned the thresshold at +2.5V, and I laugh, because when I initially saw that "+" and "-" on the diagram, I thought it referred to the polarity of the input signal:eek:. Wrong answer!:D So I'm getting the idea that I should always use a voltage divider on the "+" to set the Vref at 50% of Vcc - do you see it that way?

    Nice calculations on the change of thresshold voltage, and on hysteresis. Interesting how Ohm's law is applicable in so many places.:)
    I'll play around with that some more this evening.

    Very interesting comment about "loaded output" - that explains some of the abberations I noted in voltage on the diagram.
    I'll be playing with my spreadsheets and those formulae this evening.

    You mention a "high impedance digital buffer" - can you give me the name of a chip that you typically use?

    And now to your test question on a high value feedback resistor -hmmm...rumble...rumble...whirrr...whirr...

    So if I bump the feedback resistor to 1 meg, in LOW ouput state, the current falls to 0.0023 mA, and the thresshold voltage changes very little, to 2.50023V. The same applies to output HIGH (.0025 mA) - very little change in thresshold, to 2.4976V. So the hysteresis falls to minimal levels - 0.00563V.

    So with low hysteresis, the circuit is more subject to noise effects, but the thresshold voltage is very stable.
    While with a low Rf resistor, the hysteresis is higher, less subject to noise, but thresshold voltage varies more.

    Did I get lucky with my analysis?

    Again, to ALL of you, thanks very much for the enlightening discussion, and the addition to my knowlege!

    Ohm's law...Ohm's law...Ohm's law...Ohm's law...:D
    Last edited: Dec 23, 2012
  9. tindel

    Well-Known Member

    Sep 16, 2012
    You're getting it SPQR... I'm confident you understand the calculations now! You were off by an order of magnitude on one of your numbers, the low to high transition voltage would be 2.5023V. so, your total hysteresis is off just a hair, but is in the right ball park. I calculate 4.78mV.

    Yes - 4.78mV is very little hysteresis and will be prone to noise - what do you think you could do to get more hysteresis while leaving Rf at 1Mohm?

    How does the 1Mohm Rf change your output voltage?
  10. SPQR

    Thread Starter Member

    Nov 4, 2011
    Excellent. I cannot thank you enough for your time.

    Ok, here we go...bang...bang...rumble...pow...

    Let's see if I can generalize the situation based on your equations:

    Vtrans L-H = Vthresh + [(Vthresh-Vfb)/Rfb) * Ri]
    Vtrans H-L = Vthresh - [((Vcc-Vthresh)/(Rf+Ri) * Ri)]
    Vtrans L-H = Voltage at transition from Low to High
    Vtrans H-L = Voltage at transition from High to Low
    Vthresh = Threshhold voltage (predetermined)
    Vfb= Feedback voltage
    Rfb = Feedback resistor value
    Ri = input resistor value
    Vcc = supply voltage

    So Vhysteresis = f (Threshhold, Vfb, Rf, Vcc, Ri).

    So threshhold is fixed, Vcc is fixed (assume), Rf and Vfb are related, so the only other way to change hysteresis would be by changing the input resistor.
    And if you increase the value of Ri, then you increase hysteresis, and vice versa.

    I'm going to bet that I can generalize it even more (one equation), but I'll have to think about it some more.

    Again, thanks very very much!

    PS - I'm still thinking about your output voltage question. At first glance, I'll bet it doesn't change it very much because of its magnitude, but let me think some more.
    Last edited: Dec 23, 2012
  11. WBahn


    Mar 31, 2012
    Try working out two equations, one for the center point of the two threshholds and one for the hysteresis deviation above and below that.

    (V_H + V_L)/2

    (V_H - V_L)/2

    This will make it easier to see what you have to do to change one but not the other, which is commonly a desirable trait.
  12. SPQR

    Thread Starter Member

    Nov 4, 2011
    Ok, more good questions and suggestions!

    I did my last post by writing on paper, so now, it's time for a spreadsheet!!!

    So using the equations previously, and the 2,700 ohm feedback resistor, I get:
    Mean thresshold = 2.83V
    Total hysteresis = 0.52V

    Changing the feedback resistor to 1 meg, I get:
    Mean thresshold = 2.49V
    Total hysteresis = 0.0023V

    Which confirms that a higher feedback resistor decreases hysteresis.
    So I changed the input R to 10K, and hysteresis went up 10x - 0.024V

    This is very nice. I thank you!

    PS: @tindel - could you give me the type/number of buffer you typically use for "high input impedence"?
  13. tindel

    Well-Known Member

    Sep 16, 2012
    74AC540 and 74AC541 and their cousins are popular buffers.
  14. SPQR

    Thread Starter Member

    Nov 4, 2011
    Excellent, thanks.
    I just ordered some 541's last week...I'll play with them a bit.
    Thanks again for your time.