Exploring circuit simulation using SPICE.
For this blog post, I was originally going to write a survey of some of my favorite tools - things that my workbench would be incomplete without. As I wrote more, I found that I’d written a whole lot about one specific tool. Rather than deleting it and starting over, I decided to change the subject.
If SFUptownMaker <3 Python, I .LT.3 SPICE.
SPICE is circuit simulation software that allows you to quickly and easily see how a circuit performs. It’s a virtual workbench with electronic components and test equipment that helps me jumpstart the design process.
SPICE (Simulation Program with Integrated Circuit Emphasis) originated at UC Berkeley in the 1970s. It is a circuit simulation program that was initially intended to simulate the internal details of integrated circuits, but it’s also useful for more general circuit design applications.
The original Berkeley versions were a UNIX command-line based application. Schematics were entered in a text form, in a file called a netlist. It described all of the components and the electrical connections between them. I found them to be really cumbersome - I had to draw the schematic on paper, and then meticulously translate it into a netlist. If I did it successfully, I could hope to get an ASCII plot of the output.
V1 0 v- 12 V2 v+ 0 12 V3 Vgen 0 PULSE(-1 1 .5 1u 1u .5m 1m 1000) AC ac 1. XU1 N002 Vout v+ v- Vout level.2 Avol=1Meg GBW=10Meg Slew=10Meg ilimit=25m rail=0 Vos=0 phimargin=45 en=0 enk=0 in=0 ink=0 Rin=500Meg R1 N001 Vgen 10k R2 N002 N001 10k C1 Vout N001 10n C2 N002 0 10n .tran 2 ;ac oct 12 10 20k .lib UniversalOpamps2.sub
Sample ASCII Netlist
Newer SPICE packages take advantage of graphical user interfaces. You can draw a schematic, then interact with it in a virtual test bench environment. There are many circuit simulators around, ranging from free to extremely expensive. A few of the free ones I’ve used include.
I’m most familiar with LTspice, so I’m going to base this brief example on it. LTspice was originally known as SwitcherCAD, intended as an aid for switch-mode power supply design. It proved to be useful enough in other areas that it has been renamed, and the library of components expanded to cover more general parts.
If you’d like to follow along, begin by downloading and installing LTspice from Linear Tech’s downloads page.
First, forget everything you know about editing schematics in your PCB software. The schematic drawing tool is almost entirely different, and involves a bit of a learning curve - if I really stepped through creating a circuit, there wouldn’t be much room left to discuss what you can do with it.
Please download the sample, and open the file
sallen-key.asc in LTspice.
Sample SPICE schematic
This schematic contains a simulation of an equal-value Sallen-Key lowpass filter. One the left are voltage sources forming a bipolar 12VDC supply. In the middle is a voltage source acting as an AC signal generator, configured to produce a one-second long burst of a 1 kHz square wave. On the right is a single opamp, with resistors and capacitors to configure it as a lowpass filter.
SPICE operates in several different modes, depending on how we want to approach a problem. Let’s explore a couple of those modes.
The simplest mode of operation for LTspice is as a virtual oscilloscope, known as transient analysis.
If you hit the “run” button on the toolbar, the simulation will run, and a new window will open…but it’s empty!
This is where the simulation acts like a vitrual oscilloscope. If you go back to the schematic window and hover over a wire in the simulation, you’ll see that the cursor becomes a little scope probe.
If you click that probe on a wire, the voltage on that wire gets added to the waveform window. For this simulation, click on the wires labeled Vgen and Vout. You’ll see something like this.
This is zoomed out significantly. You can use the magnifying glass tools to zoom in, and discover that those colored rectangles are actually waveforms.
Input versus Output waveforms
Here we can see that Vgen is a square wave, and Vout is a rounded-off version of that wave, as the high frequency harmonics have been removed from the square.
A second mode of operation is as a spectrum analyzer, which is useful to show our filters frequency response.
Leaving the waveform window open, right-click on the schematic, and select the Edit Simulation Cmd. menu option. Select the AC Analysis tab, and close the dialog.
Now run the simulation again. The waveform display is replaced by plot of amplitude versus frequency.
Here, the AC voltage source has been reconfigured as a reference signal generator. The simulation steps the generator through a frequency range, records the node voltages at each frequency. We see confirmation that the high frequency of the output is reduced relative to the input.
By clicking on the V[vout] trace name at the top of the plot, we get a set of crosshairs that track the plot, allowing us to make more detailed measurements. The crosshairs show us that the filter output is 3 dB down at 1,023 Hz.
Rather than performing an extensive tutorial of all of the workings and capabilities of LTspice, I’ll leave that as an exsecise for the reader.
Instead, I’ll share some of the things I wish I’d known when I was getting started.
There’s an old quote about circuit simulation, usually attributed to Robert Pease - “My favorite programming language is solder.”
Simulation in general (not just limited to SPICE) runs the risk that the simulation doesn’t correspond to real-world behavior. It doesn’t simulate every aspect of a circuit, and the accuracy of the simulated components can vary quite a bit.
One of the classic tests for a SPICE simulation is to build an opamp circuit with a lot of gain, then feed it a large signal. Older opamp models would drive the output outside the rails, but this seems to be solved in modern models. Similarly, simulated resistors have no power ratings - they’ll happily dissipate ludicrous amounts of power, where real resistors would smolder or scorch.
It helps to stay sensible. Double check datasheets, use the virtual ammeter probes, and apply downloaded models with a degree of suspicion - there could be a mistake in the model.
I don’t jump right from the simulation to a completed design - it’s a stepping stone to help understand circuit behavior. I can experiment with parts that may be expensive, unavailable, or hard to casually prototype with. I can evaluate whethera part of solution is appropriate, before moving to a real breadboard and prototype PCBs. Sometimes it takes work to figure out why the prototype behaves nothing like the model. I’m currently troubleshooting problems with a Schottky diode that seems to conduct backwards in the sim.
Ultimately, you have to actually build the circuit from real components to prove that it works.
For the recent analog designs I’ve done for SparkFun, I used LTspice to help tune them. I’ve included the LTspice schematics in the respective GitHub repositries, usually in a
You’ll have to wait until next time to find out why I keep 6 different kinds of wire strippers around, and which of them is my favorite.