# How can I make an ideal comparator in LTspice?

For fast simulations I sometimes need an ideal comparator. I guess it is not a built-in component. How can it be implemented by ideal sources or VCVS ect.?

• What do you need ideal comparator for? LTspice is usually for real world circuits. Commented Oct 1, 2017 at 16:04
• Several options. I use a digital buffer or schmitt trigger for simulating a comparator. Commented Oct 1, 2017 at 17:51
• @winny I tried them but couldnt make them work. Can you write that schmitt ot digital buffer way as answer with a small example? Commented Oct 1, 2017 at 18:11
• Search for ltspice digital schmitt trigger model command. There are examples. There are probably better ways around it, but I use e to scale down signals to the 0-1 V level the ideal digital support, and then e on the output to scale up in voltage again to your Vcc. Commented Oct 1, 2017 at 18:41

First create a B-source

Then set the value of the B-source to an expression like below:

Now B1 is the comparator with 1V/0V output.

Another example function can be something like:

V{if(V(Vref)>0.2, cos(12*time),cos(36*time))}

I don't know how it can be done by using VCVS.

• Minor correction: it should be V=... if you're using a behavioural voltage source. Even more minor note: the curly braces are not needed, but they don't hurt, either. Commented Oct 12, 2017 at 6:17
• Yes thanks for the correction; it has to be: V=if(V(in1)>V(in2), 1,0) Commented Oct 12, 2017 at 18:36

While not disregarding user16307's answer (it is a valid one), I have to warn you that behavioural sources, while versatile in terms of mathematical expressions, tend to suffer from limited dynamic range and slower simulation speed when higher numbers are involved. If your application does not involve great dynamic ranges (on both y and x axes), or high values to be processed (say hundreds, or kV, upwards), then behavioural sources are just fine.

Else, you should know there is an A-device (in [Digital]): the Schmitt trigger. It comes in different flavours, with differential/single input and single/complementary ouputs. I highly recommend this one over anything else, since A-devices work natively, are the fastest and most accurate, plus they have control over their rise/fall times and convergence is taken care of behind the scene (i.e. no discontinuities during switchings).

If your reference is a fixed voltage, that doesn't change during the simulation, you can use the single input with the internal parameter vt set to the reference voltage. vh is optional, but useful to set, sometimes, maybe to circumvent possible noisy inputs, or maybe simply to make the behaviour closer to reality. If not, use whichever input you want, or need, for the variable reference voltage.

Another way is to use the table() function with a VCVS or a VCCS, thought the latter is preferable due to it being recommended by the manual as being convergent-friendly, compared to its voltage cousin.

To exemplify the two proposed examples, here's a picture:

vt can be have any value, representing the trigger point, and vh is the hysteresis voltage, can be null (for the VCCS, it can be negative, as well). ref is the fixed (if used as a parameter, encolsed in curly braces), or variable (for the rest) reference voltage, and the 10p capacitor is meant to aid convergence (avoid steep changes that can cause hiccups to the simulator, e.g. timestep too small, or similar).

• The Schmitt Trigger was the solution for me, thanks to this answer. Tip: By adding e.g. Vhigh=5 Vlow=0 you can adjust the output logic levels of the trigger to suit the rest of your circuit. Commented Sep 4, 2020 at 12:09

LTSPICE DOES have the equivalent of an ideal comparator. It is the "Voltage Controlled Switch", listed as "SW" in the top level symbol list. This component looks like an open switch (OFF) when the differential input voltage is greater than the threshold (Vt) and like a closed switch (ON) when the input is less than the threshold. So if Vt is set to 0 volts and the negative terminal of the output is tied to ground the SW component will act as an ideal comparator with an open drain output. The SW component also allows you to specify a hysteresis value for the input.

Another way would be to use an ideal op-amp as shown below, but flip the terminals around (and ditch the gain):

I found a more elegant solution. based on the comment from "a concerned citizen". Get a diffschmitt out of the logic library, right click on it and enter this text in the spice line:
Vhigh=5 Vh=0 Vt=0 I'm using 5V logic so Vhigh is 5. Vh controls the hysteresis, but for an absolutely ideal comparator a value of 0 recommented.

• I'm afraid you got it wrong: it is not a comment, but an answer, and one that expanded on the subject. As it stands, you're not adding anything new to the already existent answers. Commented Apr 24, 2020 at 7:53