Connecting your circuits to the outside world is one of the most important and most terrifying things for a circuit designer. It is both what makes your circuit useful and also the time an unsuspecting user plugs in that out-of-spec power adapter from Joe Schmoe's House of Crappy Cables and lets out the magic smoke. One problem you might want to address is turning off an input switch if the input voltage gets too high.
Let’s consider this (somewhat contrived) example. You have a shiny new test board that is being powered through USB - but not just any USB - USB Type-C. Your board only needs (and the input power path is only rated for) 5V. However, you’re worried that some knock-off cables might over enthusiastically present 20V instead of negotiating (!) and destroy your board. What to do? One possibility is adding a comparator that would turn off your input switch if the voltage goes above a certain limit. For this example, you could probably just limit the input to something somewhat high (maybe 6V or even 7V), rate your power path to 7V and just use a simple comparator. However, let’s say that you found the best buck converter ever but it is only rated to 6V, so you need to cut off the input before that. In this case, when you take component tolerances into consideration, just having a simple comparator that cuts off around ~5.5V will not be good because the USB spec allows the input voltage to be as high as 5.5V and you don’t want to end up slamming on and off your input switch if you have a somewhat noisy cable presenting 5.5V. What you need is hysteresis!
Before you select your comparator, you might have the same question I had: Could I use an op-amp? The short answer is, you might be able to get away with it, but you shouldn’t. Here’s a quick table summarizing the differences between op-amps and comparators - the main difference is the output stage. For more information see: Op Amp and Comparators - Don't Confuse Them!
OpAmp | Comparator |
Designed for analog inputs/output | Designed to be a 1 bit ADC |
Cannot achieve true rail-to-rail operation | Often open-drain/collector meaning output can essentially be rail-to-rail |
Output stage designed for linearity | |
May not be designed to operate in saturation (may consume more power) | |
May switch more slowly |
Confident that you should be using a comparator not an op-amp in open loop, how do you achieve hysteresis? Easy-peasy - use this configuration and I’ll walk you through how to select the three resistors. One note that I’m using a open-drain comparator. You can use a push-pull comparator, but you run into the problem of it not being rail-to-rail, which affects your comparator hysteretic set points.
To understand this circuit, start by considering the two modes of operation: (1) Vin is rising and is less than Vref and (2) Vin is falling and is greater than Vref. In (1), we are interested in the Vthr (Vin threshold rising) where Vout goes from 0V to VCC. In (2), we are interested in the Vthf (Vin threshold falling) where Vout goes from Vcc to 0V. Using KCL, let's consider the equations for these two cases:
Case (1): Vthr (recall that Vout = 0V)
Case (2): Vthf (recall that Vout = Vcc)
Looking at these equations, we can see that the term (R1/R3)*Vcc is the hysteresis band between the rising and falling Vin thresholds. To start choosing resistor values, let's consider the example we have above: Vcc = 6V (I've glossed over how you have this 6V supply given that we're looking at our power path, but let's imagine for the sake of simplicity that it exists), Vthr = 5.9V (so you don't overvoltage your buck converter), Vthf = 5.6V (so you can accept a USB-C input of 4.5V), and let's say Vref = 5.5V. (Note: to get Vref you can voltage divide Vcc, but be careful about tolerances of your resistors and voltage source).
In this example, your hysteresis band (HB) = .3V so the ratio of R1/R3 is .3V/6V. In other words, R3 is about an order of magnitude larger than R1. Given the fact that we don't want a lot of quiescent current through R2, we can conclude that R3 should be large - at least in the 100s of kOhms, maybe even MOhms. The only upper limitation on R3 is the input leakage current of the comparator, but as long as the current through R3 is always 10x that or more, it shouldn't be an issue. This article has a nice explanation of choosing R3, but I just selected R3 = 2MOhm since I don't have a particular comparator selected.
From there, it's easy to calculate R1 given that my HB = .3V. R1 = 100kOhm. Also, R2 can be quickly calculated using the Vthr equation: R2 = 383kOhm.
Of course, it's not quite this simple. First, you need to take into account the fact that you may not be able to find these exact resistor values. I like to reference the e96 series table to see what is actually available. Moreover, all of your resistances and power supplies have tolerances, which you have to factor into these equations.
Here's a spreadsheet I developed to make this process easier. To use the spreadsheet, first select your desired Vthr and Vthf. Also, enter Vcc and Vref. This will allow you to calculate the theoretic resistor values we have above (I always default R3 = 2MOhms, but you can adjust that). Then, using the e96 series table, choose some actual resistor values and enter them on the second sheet. This sheet also gives you the option to add upper and lower tolerances on your Vcc and Vref (which you will have, particularly if Vref is just a voltage divided version of Vcc). This sheet will then show you the maximum Vthr and minimum Vthf you will end up with based on your resistor tolerances as well as your Vref and Vcc tolerances. The key thing I found playing around with this spreadsheet is that it's hard to make a comparator circuit with as tight a hysteresis band as it appears on paper!