Simulating LM339 comparators in NGSPICE

Hey there,

I am planning to simulate a circuit that uses the LM339. Would this be the correct way to simulate essentially the four comparators? I essentially made 4 instances with their own power supply, so the pin assignments do not override each other.

Would this be the correct way to simulate it?

The normal method is to use the units: U1A, U1B, U1C, U1D and U1E, so all opamps are from the same IC package. This does require that your spice model is aware of the pin mapping of those comparators.

I’m certain it can be done, and it’s also relatively easy. Basically you have one model of the comparator, and then you create multiple instances that use that same model to create different pinouts. I do not use this much myself, and forgot the exact syntax. In the examples linked below, the Triangle generator with MAX9000 uses a dual opamp. You can probably copy the syntax from that, but I have not verified this.

Don’t do this. You’ll run into endless problems both with schematic and PCB layout, and even more with simulation.
Just use one LM339 and place all five units on the schematic (A…E).

Sorry for sounding like a total noob about this :smiling_face_with_tear:

So attached is the spice model I found
lmx339.lib (5.7 KB)

When I inspect it I can see that the file only has 5 pins instead of the 14 on the LM339

So I thought I could get away with using 4 different instances of different LM339 since the pins won’t overwrite other (as you and ML9104 pointed out, I’m just creating more problems for myself). Do I need to edit the Spice Model to include all the pins?

About a year ago I started collecting some spice models of “common parts” into my personal library.

It’s got a sub circuit for a TL071:

 * TL071 OPERATIONAL AMPLIFIER "MACROMODEL" SUBCIRCUIT
 * CREATED USING PARTS RELEASE 4.01 ON 06/16/89 AT 13:08
 * (REV N/A)         SUPPLY VOLTAGE: +/-15V
 * CONNECTIONS:      NON-INVERTING INPUT
 *                   | INVERTING INPUT
 *                   | | POSITIVE POWER SUPPLY
 *                   | | | NEGATIVE POWER SUPPLY
 *                   | | | | OUTPUT
 *                   | | | | |
.SUBCKT TL071_TI    1 2 3 4 5
 *
  C1   11 12 3.498E-12
  C2    6  7 15.00E-12
  DC    5 53 DX
  DE   54  5 DX
  DLP  90 91 DX
 ...

And a bit further down, it’s got a few lines for a TL072:

* https://ngspice.sourceforge.io/ngspice-eeschema.html#multi
* 2024-10-17T23:12 Tested. Works.
.subckt TL072 1out 1in- 1in+ vcc- 2in+ 2in- 2out vcc+
XTL072A 1in+ 1in- vcc+ vcc- 1out TL071_TI
XTL072B 2in+ 2in- vcc+ vcc- 2out TL071_TI
.ends

And look at that, it’s even got a link to a tutorial for using a dual opamp.

Put this at the end of the LM339 file! It is pretty self explanatory. The order is for KiCad if they have not changed something inside. In my exp you have to place all units in the schematic even if you do not use them otherwise it can do weird errors.
When you selected “LM339_QUAD” in the simulation model click ok then open it again to see pins are in the correct order.

.subckt LM339_QUAD U2_out U1_out Vpoz U1_inv U1_noninv U2_inv U2_noninv U4_inv U4_noninv U3_inv U3_noninv Vneg U3_out U4_out
XU1 U1_noninv U1_inv Vpoz Vneg U1_out LM339A
XU2 U2_noninv U2_inv Vpoz Vneg U2_out LM339A
XU3 U3_noninv U3_inv Vpoz Vneg U3_out LM339A
XU4 U4_noninv U4_inv Vpoz Vneg U4_out LM339A
.ends LM339_QUAD

Ngspice is always right (a little note for simulation debugging) - Schematic / Simulation (Ngspice) - KiCad.info Forums

But, but… mine snippet has more colors.