Introduction

This tutorial has been developed for the ENGI 4136 course at Lakehead University. It covers basic concepts needed to properly use circuit simulators, and in particular it is focused in Spice-like simulators.

A linux version of NGspice (http://ngspice.sourceforge.net/) was used in examples, but the same examples can be run with the Windows version of NGspice or even with LTspice (with small modifications). For more details about NGspice refer to the official program documentation on its website.

Please treat this tutorial as work in progress.

Circuit Description

Circuits are described to the simulator in a text file called netlist file. You’ll need a text editor to create and modify these files. Sample text editors are gedit or nano (also vi and emacs) in linux systems or notepad on Windows. Do not use MS Word or other word processing programs as those add extra formatting information that may confuse the simulator.

A very brief description of the netlist syntax is provided here. In most Spice implementations netlists are case-insensitive. Each line specifies one circuit element, an analysis to perform or another command. Most lines can be entered in any order. Order is important only for the title line (first line in file), the .end keyword and to define subcircuit blocks. Subcircuits will be discussed in the next section.

Consider the following circuit:

RF diode switch schematic

Nodes have been named, either with names or numbers and are shown in red in the figure. Node 0 (or gnd) is considered to be the reference node in Spice (‘ground’). These names will be used in the following description:

*** RF switch circuit ***

* Input source
vs 1 0 dc 0V ac 1V
Rs in 1 50ohm

* Switch
Ci in 4 1.6nF
Rb 4 3 2.1k
Lc1 3 2 100uH
D1 4 5 mydiode
Lc2 5 0 100e-6
cout 5 out 1.6n

* Load
Rload out 0 1000

* DC biasing
vcc 2 0 5V

.model mydiode d (is=1e-15A n=1)

.end

The first line in the netlist is the title line and does not follow any particular syntax. Lines that start with * are considered comments and ignored by the simulator. Each of the other lines except .model and .end correspond to a component in the circuit. For component lines, the first letter identifies the component and the format is the following:

Resistor: R<name> <node 1> <node 2> value

Inductor: L<name> <node 1> <node 2> value

Capacitor: C<name> <node 1> <node 2> value

Diode: D<name> <anode> <cathode> <model name>

Bipolar transistor: Q<name> <collector> <base> <emitter> <model name>

Voltage source: V<name> <pos> <neg> DC <value> AC <value>

Values can be given as a floating-point number (1e-5) or using the following modifiers:

      12         9            6         3                    -6
T = 10     G = 10     Meg = 10    K = 10      mil = 25.4 x 10
      -3        -6         -9          -12         -15
m = 10     u = 10     n = 10      p = 10      f = 10

For example: 3.4p = 3.4 pF = 3.4e-12. Units after the multipliers have no effect. Be careful with the Meg multiplier, 1MHz means 1e-3Hz for Spice, 1MegHz should be used instead for 1e6Hz.

Semiconductor devices such as diodes and BJTs usually require a .model line. Model parameters are listed there. The general format is the following:

.model <name> <type> (par1=value1 par2=value2 ...)

Some possible``<type>`` values are d (diode), npn, pnp, nmos and pmos.

The .end line is optional. If present indicated that any additional lines in the netlist should be ignored.

Subcircuits

Subcircuits are very useful to lump a portion of a circuit into a single block. One of the most common uses is to define a subcircuit with a model of a device or integrated circuit.

Subcircuits can be defined as shown in the following example (extracted from National Semiconductor’s lm741 model):

*//////////////////////////////////////////////////////////
*LM741 OPERATIONAL AMPLIFIER MACRO-MODEL
*//////////////////////////////////////////////////////////
*
* connections:      non-inverting input
*                   |   inverting input
*                   |   |   positive power supply
*                   |   |   |   negative power supply
*                   |   |   |   |   output
*                   |   |   |   |   |
*                   |   |   |   |   |
.subckt lm741/ns    1   2  99  50  28
* (subcircuit description goes here)
.ends

The .subckt keyword starts the definition. It is followed by the subcircuit definition name (lm741/ns) and the external connections (1 2 99 50 28). The subcircuit is described below that line as usual and the definition ends with the .ends keyword.

To use this subcircuit definition in the main circuit, we create subcircuit instances using the x element. A netlist for an invering amplifier could be implemented as follows:

* Inverting amplifier + supply + input source
vcc 1 0 dc 10V
vee 0 2 dc 10V
vin 3 0 ac 1
r1 3 4 1k
r2 out 4 10k
xamp1 0 4 1 2 out lm741/ns
.include lm741.mod
.end

The .include line can be used to include other files in the nelist description. In this case a file named lm741.mod contains the subcircuit definition.

Plotting Format (linux version only)

To make (the linux version of) ngspice to produce postscript figures of the plots when the “hardcopy” button is pressed, set the hcopydevtype variable:

ngspice 372 -> set hcopydevtype=postscript

Loading the circuit into the simulator

NGspice on Linux

First, save the netlist in a plain text file. We’ll assume that the netlist is saved in a file named rfswitch.cir. Then run NGSpice from the directory where the netlist is saved as shown below:

******
** ngspice-20 : Circuit level simulation program
** The U. C. Berkeley CAD Group
** Copyright 1985-1994, Regents of the University of California.
** Please submit bug-reports to: ngspice-bugs@lists.sourceforge.net
** Creation Date: Fri Nov 20 10:19:22 GMT 2009
******
ngspice 1 -> source rfswitch.cir

Circuit: *** rf switch circuit ***

ngspice 2 -> listing
    *** rf switch circuit ***

     2 : .global gnd
     4 : vs 1 0 dc 0v ac 1v
     5 : rs in 1 50ohm
     7 : ci in 4 1.6nf
     8 : rb 4 3 2.1k
     9 : lc1 3 2 100uh
    10 : d1 4 5 mydiode
    11 : lc2 5 0 100e-6
    12 : cout 5 out 1.6n
    14 : vcc 2 0 5v
    16 : rload out 0 1k
    17 : .model mydiode d (is=1e-15a n=1)
    19 : .end
ngspice 3 ->

NGspice on Windows

On the windows version of ngspice, run ngspice.exe which is usually located in c:\spice\bin (but this depends on where the zip file was decompressed. Assume that the full path to the saved netlist is d:\netlist\rfswitch.cir. The netlist can be loaded by specifying the full path as shown below:

Windows version of NGspice

There are other ways to work with NGspice on windows. Check the the official website http://ngspice.sourceforge.net/index.html for more information.

Using LTspice with this tutorial

The netlists presented in this tutorial can be used with LTspice. To load netlists in LTspice, launch the program and load the netlist from the File menu. LTspice does not have the interactive prompt. Instead, it has a menu system. Most of the commands discussed here can be entered in the netlist or have equivalent commands in the graphical interface. In cases where the syntax is not exactly the same, consult the LTspice manual to find equivalences.