How to best get started simulating circuits with SPICE?

So we all know about Paul Falstad’s CircuitJS right? Great way to simulate little circuits and see how they behave, without barbecuing real chips to gain knowledge.

How to take it further? I’d love if my KiCad designs could be at the same time human-readable, machine-simulable, and usable as the base for PCBs.

I know the answer will be spicy, but there are so many seasonings available it’s hard to navigate! There’s half a century of SPICE forks and variants, and rigging it up seems full of pitfalls. I don’t want to accidentally waste time learning outdated processes or use less than ideal tools. For those of you who use simulation, what’s your software setup?

There is some spice simulation that can be used directly from KiCad, I guess that makes the most sense, because you don’t need to repeat the circuit, but I have not yet tried it. So far I have been using LTSpice, which is for windows, but it works with wine as well. It works very well and you can easily analyze your filters etc. you can also output wav files and then listen to your simulated module! I can share some common parts if there is interest. In future I would like to use the kicad internal, but so far I am too lazy to learn it :wink:

1 Like

KiCad provides an interface to ngspice, which is related to but different from LTSpice. I’ve used LTSpice, and I’ve played a bit with KiCad/ngspice but not used it much. One thing that struck me is it seems there’s no way in ngspice to have it vary parameters? In LTSpice for instance you can use a directive like .step param r 500 1000 100 to have it vary (maybe) a resistance from 500R to 1k in steps of 100R and have it plot the six results. Apparently you can’t do that in ngspice? Which seems a strange omission. Or maybe I just don’t understand ngspice well enough.

I hate using LTSpice because of its crude, antiquated user interface and complete non adherence to what I think of as normal ways of doing things, like using F9 to mean undo, but I don’t think ngspice has the capabilities I want.

3 Likes

I managed to get the KiCad integrated ngspice thing running when I was working on my diode ladder filter - it was useful and I should have taken better notes on how I made it all work:

Imgur: The magic of the Internet

I remember having to install these, plus some other models floating around from different manufacturers:

GitHub - kicad-spice-library/KiCad-Spice-Library: Centralized repo to store KiCad/Spice modules for simulations

I also remember having to take care when lining up pin order vs spice subckt pin order.

As far as outdated or less than ideal tools, even enterprise spice solutions with fancy front-end integration all boil down to command line tools that take text file netlists as inputs. Using ngspice to learn what the different types of analysis are (AC, DC, TRAN etc) and how all the .MODEL, .LIB and .SUBCKT pieces all fit together will put you in good stead for most spice simulators.

1 Like

I recently spent a while wrestling with KiCad’s ngspice integration in order to get a simulation of (part of) a through-zero FM circuit simulated. I had a few challenges, some of them related to how to handle interfaces to other modules within a circuit, but some related to KiCad and ngspice. A long time ago I also learned and used PSpice pretty heavily, but I’m not going to pay for it.

I think the biggest challenge is models. KiCad ships with no 3rd party models, so if you want them you’ll have to add them yourself. You can download e.g. a LM742 model from TI, or get one from the collection that @jradical linked, but you’ll have to manually link it to a symbol and set up pin connections either way. You could in theory make your own library with models included for the parts you care about, and I think that’s probably where I’ll wind up. But if you wanted to share the designs you’d also have to publish the library. Likely fine, though do take care to note the different licenses on the models vs your own contributions.

A bunch of commonly used chips don’t have models at all. I mean, for one thing you can’t SPICE simulate an ATmega chip (Arduino Nano) or ESP32 as the code doesn’t really translate into the SPICE approach to the world. You can sorta emulate it with XSPICE stuff (part of ngspice). But also, none of the chips from CEM, SSI, SSM, or Alfa Rpar have models. You might be able to make a sufficient one off the block design but it’ll require a better understanding of their workings than what’s shown on the spec sheets. The CEM3340 design, for instance, has several blocks that are not sufficiently explained to simulate.

Of course, an approximate model is good enough, most of the time. You probably don’t need a perfect VCO model. But you’ll have to build them yourself if you want the spec to be complete.

So far as simulator options go, I think the popular free ones are ngspice and LTspice. There’s a couple others like SpiceOpus, Gnucap, and Xyce but they’re not as commonly used. LTspice comes with more models but is closed source. Except for Gnucap, they’re all SPICE at their core and work pretty similarly.

You can definitely do this in ngspice, but I don’t think it’s a single control line like that, unless you’re doing a DC analysis in which cas you could do .dc R1 500 1000 100 to vary R1 from 500 to 1000. But if you want to, say, measure the level at a specific frequency as you vary one resistance then I think you’ll need to do that within a .control block. Here’s one example of doinog that. https://www.sobremesalabs.com/parameter-sweeps-in-ngspice/

3 Likes

Might also mention the web based https://www.circuitlab.com/ which I used to use — compared to LTSpice or KiCad/ngspice I think it’s much more user friendly (and compared to Falstad it doesn’t give me motion sickness just looking at it). But I think it’s got more limitations than the Spices; also the free student version and entry level “Micro” version are extremely limited — maximum of 10 components per schematic!! — and the next level up at $79/year was more than I could justify spending. And you have to solve CAPTCHAs to log in.