Simulating Kicad Schematics With Spice
Simulating Kicad Schematics With Spice
Simulating Kicad Schematics With Spice
I’ve been using Tina TI to do simulation of electronic circuits for a long time. Tina can be kind of
frustrating though since it’s a GUI application so for almost as long I’ve had this idea about finding
some other simulator that can be scripted in some way to run a batch of simulations where I can do
variants of each simulation, maybe with different component values and with different stimuli. But most
other simulators I’ve looked at such as qucs are just as “bad” in that they are hard to script.
Most of the things I want to be able to do can be done with one of the free spice simulators such as
Ngspice, but the raw Spice language is kind of painful to use. It would be nice if I could just draw a
schematic in Kicad and then export a netlist which can be used with Ngspice, but I have not been able
to get that to work the way I want it to. The netlists lack some information that is present in the
A few weeks ago I got so frustrated that I started writing my own parser which can read a Kicad
schematic file and extract the components and connectivity from those. And for some strange reason I
also wrote a renderer which can render the schematic using matplotlib as a backend. This turned out to
Today I finally put together the missing parts, a converter which translates the Kicad schematic
(including any spice model directives) to a Ngspice compatible circuit, runs ngspice in batch mode
and then retrieves all the resulting values from the rawfile that Ngspice outputs.
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 1/6
5/19/2020 Simulating Kicad schematics with Spice
First I have to import the usual modules to use matplotlib and numpy:
and render the kicad schematic in a matplotlib plot. This is a normal simple transimpedance amplifier
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 2/6
5/19/2020 Simulating Kicad schematics with Spice
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 3/6
5/19/2020 Simulating Kicad schematics with Spice
spice = NgSpice()
And here comes the fun part. I can now run multiple simulations, and then show them in one plot. For
example this is an AC simulation where I plot the frequency response for multiple different values of
capacitor C101:
values = [ 1 * p, 10 * p, 100 * p, 1 * n, 10 * n ]
for v in values:
circuit.devices['C101'].value = v
data = spice.ac('hello world', circuit, 'dec', 10, 1, 1 * G)
plt.plot(data['frequency'].values.real,
20 * np.log10(np.abs(data['v(vout)'].values)),
label = str(v))
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 4/6
5/19/2020 Simulating Kicad schematics with Spice
plt.xscale('log')
plt.legend()
plt.show()
Even nicer, since matplotlib has lots of backends this works just about anywhere, including the
rendering of the schematics. I can run the above commands with Python on Linux and show the
schematics and plots in a normal X window. And since matplotlib can produce beautiful plots in a
bunch of different formats (PNG, SVG, PDF) I can generate nice plots in all of them.
Originally I played around with wxPython and displayed my matplotlib plots in an embedded frame. My
idea was to write a GUI (yeah, yet another GUI) that would let me set up and arrange multiple
simulations and plots in a Window. But then I realised that since matplotlib is supported in Jupyter and
that the easiest way to play around with and test things is to just use an interactive notebook. A big part
of this blog post was actually generated by Jupyter and translated to Markdown with jupyter-nbconvert.
And it really is simple and convenient. If I make a change in the Kicad schematics, I can just reload it
and rerun all my simulations with one command. If I just want try out some component values I don’t
even have to leave the notebook since I can easily change them with some code.
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 5/6
5/19/2020 Simulating Kicad schematics with Spice
Of course, this is just a quick hack so far and the code to do all this is ugly as sin. It’s nowhere near
complete either. I’ve only added support for the R, C, L, I and X devices so far, enough to verify that my
first simple schematic can be simulated. Only AC and DC simulations work so far, but it should be trivial
Even as incomplete as this all is, I must say that it feels really promising.
Christer's Ramblings
christer@weinigel.se Weinigel.
blog.weinigel.se/2018/08/14/kicad-spice-simulation.html 6/6