This afternoon I wet my feet in the field of SPICE (Simulation Program with Integrated Circuit Emphasis.)
I started with a basic non-inverting unity-gain amplifier circuit based on the LM324 which is a BJT (Bipolar Junction Transistor) Op-Amp (Operational Amplifier.) Currently KiCAD does not have SPICE style voltage or current sources so I made do with generic two pin connectors. V1 will be a DC voltage source (initially set to 0V) used as the op-amp input. V2 will be the DC voltage source (set to 5V) used as the supply rails for the op-amp. “outvolts” is a global net label used to identify the output.
I downloaded a LM324 SPICE model from the Texas Instruments “WEBENCH SpiceRack.” The nice thing about SPICE models is that they’re in plain text. The important part to note is the model input and outputs, especially the order they appear in. The file was renamed from “LM324.5_1″ to “LM324.mod”
An additional field is sometimes required by KiCAD to export the netlist pins in the correct order (the same as they appear in the SPICE model.)
When exporting to a SPICE netlist, ‘U’ and ‘IC’ components such as the LM324 must be prefixed with the letter ‘X.’
This was the output from KiCAD. A few changes had to made to KiCAD’s raw output…
The third-party LM324 module is included using the “.inc LM324.mod” directive (the file “LM324.mod” must be located in the same directory if a full pathname is not used.) It wasn’t necessary to change such references as “XU1″ to “X1″ (I did that on a whim.) For simplicity the default net names were changed to something more meaningful. I recommend using your text editor’s “find and replace all” feature if it has one.
The references to net name “GND” were changed to “0″ (0 being a special net synonymous to ground.) ngspice apparently malfunctions if this is not done. The SPICE directive for a DC analysis was added (DC voltage sweep, using V1 as the signal source, from 0V to 5V in 10mV increments.)
ngspice malfunctioned running the analysis. An errant character was found and removed from the end of the LM324 module.
The environment variable “SPICE_ASCIIRAWFILE” must be set to “1″ if we want the output of the ngspice simulation to be in plain text.
ngspice was executed with options “-b” for batch mode, and “-r filename.raw” to specify the analysis output type (raw) and location. “filename.cir” corresponds to the SPICE netlist filename. ngspice is quite memory and processor efficient; perhaps hinting at the meager computing power SPICE once had available in yesteryear. The SPICE netlist format originates from punch-cards!
ngnutmeg is an interactive post-processing application included with the ngspice installation. It can be used (amongst other things) to plot data.
Three variables were plotted: involts (connected to the analysis voltage-sweep source, V1), outvolts (the output from the amplifier) and vcc (the amplifier positive supply rail.)
We can see the characteristic output voltage cutoff of the LM324 op-amp as it nears the supply rail voltage. It is specified in the data sheet as only having a linear response up to 2V below the positive supply rail (5V in this example.) We can also see that the LM324 loses linearity close to the negative supply rail (0V or ground in this example.)
I wasn’t aware of this before so I’ll keep it in mind, Vss + 1V to Vcc – 2 is the ideal region for linear operation with the LM324!