Discussion about this page on: Wiki page discuss: Intro to opamps with ngspice/gEDA - Ubuntu Forums


Going back to simulation with gEDA tools and ngspice is not always necessarily straightforward, especially in the case of operational amplifiers. To adress that, instead of an in-depth tutorial - here is a bash script,

... which will use (from the script):

  • (gschem (implicitly) to generate appropriate circuit schematic)

  • gnetlist to create netlist for ngspice

  • ngspice to simulate the circuit

  • gnuplot to generate plots from ngspice data

The setup consists of a piece-wise linear (PWL) source, which first makes a sweep from -1V to 1V; and then a sweep from -5V to 5V. That signal is used as input for four opamp voltage followers, based on four operational amplifier models - from:


The schematic looks something like this:

Since gschem does not have an option / attribute to show the actual symbol file (which is the source of a given graphic in the schematic), I simply "invented my own" - added a new attribute, arbitrarily named symname (which is not previously used), simply to show which symbol files are used on the screenshot above. (the schematic was created in gschem, and then the .sch file contents were pasted in the script). This is useful, since when you use the find facility in the 'add component', these filenames are searched against. A few notes:

  • The SPICE opamp model files are for the most part 'subcircuits'; as such they typically carry extensions like .CIR or (as here) .sub (however, their format is exactly the same as for a .net netlist)

  • the E_LIMIT indeed works as a comparator only - it is in-fact a two-terminal voltage device; and as such it doesn't have an inverting input (through which to implement a negative feedback for a follower) - note in the schematic, the "input wire" doesn't connect to anything (the relationship is set in the "vol='LIMIT(...)'" expression, which is ngspice specific; this statement also implicitly defines the "power supply"). To show this, I had to edit the built-in symbol vcvs-1.sym; and remove two of its pins in order to have gnetlist output a correct ngspice stanza.

  • The SPICE-opamp-1.sym and ad8009-1.sym schematic symbol files come from the respective sources of the SPICE opamp models (OP07.sub and AD8009an.sub). Note that in them, the pinnumber attribute is the one shown on the schematic (which also corresponds to the IC chip pinout in the PCB layout); while the pinseq attribute makes sure that a given pinnumber appears, say, second - and as such, matches the second input in the SPICE model statement like '.SUBCKT AD8009an 1 2 99 50 28', where "1 2 99" are the names of the nets used internally in the .subckt! Both of these models have five pins, and power supply needs to be brought to them from the voltage source elements in the schematic.

  • The amp-diff.sym is built-in in gschem; however, while it has drawings of pins - it has no attributes whatsoever by default; all attributes visible are added manually in gschem. So the entire actual connectivity of this part is defined by the value attribute entry - the wires drawn in the schematic are, so to speak, "fake" (they are just placeholders, and used for error checking). This symbol was chosen, because like the NGopamp.sub SPICE model, it has two input pins and one output (the "power supply" is set through parameters in the SPICE model). Note also that the NGopamp.sub is slightly changed from the version in the current ngspice manual (see ngspice: Slight problems with opamp model in manual).

  • Therefore - always name your nets (attribute netname) - and have it shown on the schematic (for error checking purposes).

  • All power supplies - implicit or not - in this example have been set to +/- 5V.

See the bash script for actual order of execution of commands, and their format.

When the simulation finishes, the simulated waveforms look something like this:

If you'd like to "split" and "stack" the signals one over another graphically, you'd have to resort to gnuplot - and that part in the example script results with this diagram:

We can see that E_LIMIT behaves as a comparator and nothing else; NGopamp behaves like an ideal opamp follower; while OP07 and AD8009 behave more like realistic opamps (the input signal is overlaid as a thin line on each graph).

See the comments in the script to see what to change in order to get a single-supply simulation setup. In that case, the following results are rendered:

... where it is even more clear that OP07 and AD8009 show a realistic behavior of typical opamps (low input voltages barely cross the turn-on voltages in single supply mode).

Intro_to_opamps_with_ngspice-gEDA (last edited 2011-11-28 01:49:12 by sd-imi)