KiCad simulation: Li'l help?

Started by Fancy Lime, August 31, 2019, 03:01:20 PM

Previous topic - Next topic

Fancy Lime

Hey guys,

does anyone do SPICE simulations with KiCad? I've been at it for a while with little success. I got passive circuits working but cannot get anything containing opamps or transistors to run. I was unable to find any helpful tutorials or manuals for KiCad 5.x SPICEing. Does anyone have any? Tried this question in the Simulation subforum but there is not a lot of traffic there, so I thought I'd try here as well.

Right now my very ambitious project is to simulate a Sallen-Key low pass. First with an opamp, then with a BJT, a JFET and a MOSFET. Simply as a way of trying to get active elements working in simulation with a simple circuit and a predictable result. Has anyone done exactly that in KiCad 5.x by any chance and would not mind explaining how?

Thanks,
Andy
My dry, sweaty foot had become the source of one of the most disturbing cases of chemical-based crime within my home country.

A cider a day keeps the lobster away, bucko!

ElectricDruid

#1
Sorry, doubt I can help. I do quite a lot of simulation (it's very useful once you get into it) but I use LTspice, which is a whole different branch of "quirky".;)

At least in LTspice, op-amps need a extra spice command putting in the file so that Spice knows where to find the op-amp model. Maybe Kicad is the same? (The underlying SPICE must be identical?)

T.

PRR

> cannot get anything containing opamps or transistors to run.

What happens? Smoke? Drip? Peruvian polka??

Cite any error you get. (You may have to dig to find it.)

From the symptoms, you do not have ANY device models beyond inbuilt V-R-L-C. (But there should be generic Q.) There is also the E device.


Also read: https://forum.kicad.info/t/kicad-and-simulation/17383
  • SUPPORTER

Rob Strand

#3
Quotecannot get anything containing opamps or transistors to run

It's likely you have problems with the paths to the model files.  Most packages have a model file (or library) which is a text file for the spice model and another file (or library) for the graphical symbols (which is specific to the cad package).

Another possible issue is you don't actually have the model file for the part you have specified.
In this case you have to find the model on-line then and add it to your model folder or library.

Try a DC circuit with a diode and it's likely you will have the same problem.

PRR suggestion will work since it doesn't use any models.

EDIT:
Check out this page,
https://mithatkonar.com/wiki/doku.php/kicad/kicad_spice_quick_guide

About half-way down,
"What you'll need" ---> "Component libraries with SPICE symbols"
Send:     . .- .-. - .... / - --- / --. --- .-. -
According to the water analogy of electricity, transistor leakage is caused by holes.

PRR

> Check out this page,

Says things changed between 4 and 5. (But yes, some essential steps are forever even if details differ.)

This page seems more 5-ish:
https://mithatkonar.com/wiki/doku.php/kicad/using_arbitrary_spice_engines_in_kicad_5
(but does not cover model linkage.)
  • SUPPORTER

bool


Fancy Lime

#6
The SPICE, sadly, does not flow yet. Unfortunately, it doesn't even play Peruvian Polka. Thanks for the comments guys! I get the impression that if I manage to describe the Problem precisely, we might figure this out together. I don't have time right now to do that but hopefully tomorrow.

Just quickly:
The problem seems to me to be somewhere in the linking of the SPICE library. I think KiCad uses ngspice by default but I need to verify that. Thanks to one of the links I now know how to. I did link the OPAMP to the NE5532 model. At least I think that's what I did but the interface is not super self explanatory so I might have been mistaken about the consequences of my actions. I also tried linking to a generic opamp model called "pspice-OPAMP" but to no avail. I may have produced the netlist incorrectly, need to read up on how to set the various options there before going forth. Anyway, detailed write-up to follow after some more reading and exploring.

Thanks,
Andy
My dry, sweaty foot had become the source of one of the most disturbing cases of chemical-based crime within my home country.

A cider a day keeps the lobster away, bucko!

MaxPower

Tina-ti is pretty easy to use (assuming there's still a free version).
What lies behind us and what lies before us are tiny matters, compared to what lies within us - Emerson

Fancy Lime

Hi guys!

So, here is the status report I promised. The precise version is:

Application: KiCad
Version: 5.1.4-e60b266~84~ubuntu16.04.1, release build
Libraries:
    wxWidgets 3.0.2
    libcurl/7.47.0 OpenSSL/1.0.2g zlib/1.2.8 libidn/1.32 librtmp/2.3
Platform: Linux 4.9.71-040971-generic x86_64, 64 bit, Little endian, wxGTK
Build Info:
    wxWidgets: 3.0.2 (wchar_t,wx containers,compatible with 2.8 ) GTK+ 2.24
    Boost: 1.58.0
    OpenCASCADE Community Edition: 6.8.0
    Curl: 7.47.0
    Compiler: GCC 5.4.0 with C++ ABI 1009

Build settings:
    USE_WX_GRAPHICS_CONTEXT=OFF
    USE_WX_OVERLAY=OFF
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_PYTHON3=OFF
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON
    KICAD_USE_OCC=OFF
    KICAD_SPICE=ON

The simulator version is: ngspice-30

First something that works. Maybe someone else can use it as an introduction, too. First I started KiCad, click File -> New -> Project, and call it BridgedT.pro. Then I clicked the "Schematic Layout Editor" icon and start drawing. I built the schematic of a bridged-T filter:



All used components are from the "legacy pspice symbol library". For resistors and capacitors, all I did was enter the value. The models seem to be linked automatically, which makes sense for these components. The generic VSOURCE is also from the legacy pspice symbol library. For this one I had to edit the SPICE model to set an AC amplitude (1V in this case) and make sure the "source type" is set to "voltage", not "current" (but that was so by default). Then I clicked "annotate schematic symbols" to give all the components unequivocal numbers (could do that by hand but I'm lazy). Then I went to Tools -> Simulator, which starts the Spice Simulator. Well, duh. In the Spice Simulator window, I clicked the "Settings" icon, went to the "AC" tab, set "Number of points" to 20, "Start frequency" to 10 Hz, and "STop frequency" to 100000 Hz, and clicked OK. Then I clicked the "Run/Stop Simulation" icon and when it was done (instantaneously in this case) The "Probe" icon. That took me back to the Schemati Editor, where I clicked on the top ot the load resistor R3. Went back to the Simulation window and found this wonderful plot of phase and magnitude:



Great, so that works!

Then I meant to try something equally simple but with an active element and remembered that I had not yet followed the links in the forum post that Paul linked. Turns out, the very first link therein leads to exactly the tutorial I was looking for: http://ngspice.sourceforge.net/ngspice-eeschema.html
I will try this tomorrow but I'm fairly confident that this should work as described. I'll report back when and if it works. Or doesn't.

Cheerio,
Andy
My dry, sweaty foot had become the source of one of the most disturbing cases of chemical-based crime within my home country.

A cider a day keeps the lobster away, bucko!

PRR

> "legacy pspice symbol library". ...The models seem to be linked automatically

R C and V devices are baked into naked SPICE. (You can override them with more sophisticated models.)

There should be a generic "Q" (BJT) model but it may not be on your parts selector.
  • SUPPORTER

Fancy Lime

#10
Incremental progress (I think)!

I followed this tutorial:
http://ngspice.sourceforge.net/ngspice-eeschema.html
and made a Sallen-Key low pass with a NE5532, using the corresponding model from here:
https://jeastham.blogspot.com/2011/11/adding-ne5532-op-amp-model-to-ltspice.html
Here's the schematic:



When I try to simulate that, I do get a plot but definitely not a correct one:


-3bd at 1kHz and -6.4bd at 100kHz? Probably not. BTW, red is supposedly magnitude and green phase. But that looks even more wrong than the magnitude. I get a bunch of error messages too that might as well be in Klingon:

Circuit: KiCad schematic
Error on line 0 :
a$poly$e.xu1.egnd %vd [ vp 0 vm 0 ] %vd ( xu1.99 0 ) a$poly$e.xu1.egnd
MIF-ERROR - unable to find definition of model a$poly$e.xu1.egnd
Warning: Model issue on line 0 :
.model a$poly$e.xu1.egnd spice2poly coef = [ 0 .5 .5 ] ...
Unknown model type spice2poly - ignored
Error on line 0 :
a$poly$f.xu1.fb %vnam [ v.xu1.vb v.xu1.vc v.xu1.ve v.xu1.vlp v.xu1.vln ] %id ( xu1.7 xu1.99 ) a$poly$f.xu1.fb
MIF-ERROR - unable to find definition of model a$poly$f.xu1.fb
Warning: Model issue on line 0 :
.model a$poly$f.xu1.fb spice2poly coef = [ 0 2.893e6 -3e6 3e6 3e6 -3e6 ] ...
Unknown model type spice2poly - ignored
Background thread stopped with timeout = 0
Reducing trtol to 1 for xspice 'A' devices
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
Reference value :  1.00000e+00
No. of Data Rows : 501

If anyone can make heads or tails of that, or even both, that'd be incredibly awesome.

Thanks and cheers,
Andy

EDIT: file upload keeps freezing on me. Something is wrong. I'll try to deliver the missing plot as soon as it'll work again. For now just know that it does not look like an SK with those values should. At all.
My dry, sweaty foot had become the source of one of the most disturbing cases of chemical-based crime within my home country.

A cider a day keeps the lobster away, bucko!

PRR

https://answers.launchpad.net/kicad/+question/402843

http://espice.ugr.es/espice/src/xspice/icm/spice2poly/icm_spice2poly/README

It looks like Kicad is *very* badly integrated. (My 1996 copy of what became PSpice had all this stuff ON the one floppy, and I've never had to fiddle with subsequent Win sims to get basic-parts sims to just work.)
  • SUPPORTER

Rob Strand

#12
Quotespice2poly
Not all spice versions use the exact same syntax.
I know some opamp models I have use "poly".

This document is the contains base-line Spice 2 statements,
https://people.ece.umn.edu/~harjani/courses/common/spice2G6.pdf

I've come across quite a few in incompatibilities over the years.  A few times I've had to decipher the whole model to understand it then fix the parts which are incompatible with my version of spice.   This can take
substantial re-jigging.   (In extreme cases, due to limitations and bugs,  it couldn't be fixed through just translating the model circuit so I had do Thevenin conversions and/or reformulate the model to swap current sources with voltage source.)

From this thread,
https://sourceforge.net/p/ngspice/mailman/ngspice-devel/thread/200705131341.08389.gudjon@mc2.chalmers.se/

At the bottom someone mentioned adding,
.INCLUDE /usr/lib/spice/spice2poly.cm

What that does I don't know!


This page has a little more info, although it doesn't spell things out,
https://github.com/imr/ngspice/tree/master/src/xspice

Send:     . .- .-. - .... / - --- / --. --- .-. -
According to the water analogy of electricity, transistor leakage is caused by holes.

Fancy Lime

All right.

File upload works again, so here is the plot missing from yesterdays post:


I figured out that I forgot to create a net list. Doing that took care of all the error messages but still produced the same wrong plot.

Robs idea that some models may require libraries that are not linked in all spices sounded plausible, so I tried using the LF356, that they used in the tutorial with exactly the model that they used. And whatd'ya know, this was the result:



Noice! I assume that the second phase shift starting just below 20kHz is due to the properties of the opamp, yes?

So now that I have one working opamp model, I can just use that for the simulations even though the actual circuits will have different opams, can I not? Assuming the circuit is designed such that input and output impedance, bandwidth and open loop gain do not have any serious effect on the performance. In other words, most audio circuits. For high-frequency stuff or anything demanding in terms of opamp performance, I'd still have to hunt down an actual model for the right opamp.

Thanks everyone! I'll try to work with what I got now and be sure to go on everyone's nerves again when the next problem arises.
Cheers,
Andy
My dry, sweaty foot had become the source of one of the most disturbing cases of chemical-based crime within my home country.

A cider a day keeps the lobster away, bucko!

Rob Strand

QuoteSo now that I have one working opamp model, I can just use that for the simulations even though the actual circuits will have different opams, can I not? Assuming the circuit is designed such that input and output impedance, bandwidth and open loop gain do not have any serious effect on the performance. In other words, most audio circuits. For high-frequency stuff or anything demanding in terms of opamp performance, I'd still have to hunt down an actual model for the right opamp.
Definitely!

To tell you the truth I rarely use opamp models.   I have a configurable small signal model that I can program gain, gain bandwidth, noise, and impedances.  It's far less problematic than finding models and I *know* what the small signal behaviour will be.   I can also tweak it to see the effect.  With a model you get what you get, and you don't know what you get until you check it!

When you start clipping then you need a model with a power supply.  Overall the modelling behaviour is pretty arbitrary from large signal models.  I've got LM741 models that clip anything from 0.2V to 2V from each rail.  In other words picking any old thing in the ball-park is often just as good as an unproven "real" model for a given opamp!  Sad but true  :(
Send:     . .- .-. - .... / - --- / --. --- .-. -
According to the water analogy of electricity, transistor leakage is caused by holes.

PRR

> I assume that the second phase shift starting just below 20kHz is due to the properties of the opamp,

Never believe SPICE!! Work it out on your thumbs and see if it is in the ballpark.

The open-loop output impedance of the BiFET opamp is hundreds of Ohms. GBW is a few MHz. So at 100KHz it is dozens of Ohms.

You have 10K and a cap from source to output. 10K leaking into 10r is 1/1000 or -60dB. But at 100kHz there's still 30:1 of NFB. So call it 90dB.

THIS time SPICE did NOT lie (much). The non-ideal error echos the non-ideal parts to within a thumb-width.
  • SUPPORTER