You are on page 1of 15

NGSPICE

LAB MANUAL

Electronics Devices Lab

(EE 212)
Introduction

Ngspice is a general-purpose circuit simulation program for nonlinear and linear analyses.
Circuits may contain resistors, capacitors, inductors, mutual inductors, independent or dependent
voltage and current sources, lossless and lossy transmission lines, switches, uniform distributed
RC lines, and the five most common semiconductor devices: diodes, BJTs, JFETs, MESFETs and
MOSFETs.

Ngspice supports mixed-level simulation and provides a direct link between technology
parameters and circuit performance. A mixed-level circuit and device simulator can provide greater
simulation accuracy than a stand-alone circuit or device simulator by numerically modeling the
critical devices in a circuit. Compact models can be used for noncritical devices. Both these are
mixed-level extensions to ngspice with TCAD:

• CIDER: a mixed-level circuit and device simulator integrated into ngspice code. CIDER
was originally the name of the mixed-level extension made to spice3f5.

• GSS: GSS (now called GENIUS) TCAD is a 2D simulator developed independently from
ngspice.The device simulator itself is free and not included into ngspice, but a socket interface is
provided.

Ngspice supports mixed-signal simulation through the integration of XSPICE code into
it.XSPICE software, developed as an extension to Spice3C1 from GeorgiaTech, has been ported
to ngspice to provide “board” level and mixed-signal simulation.New devices can be added to
ngspice by two means: the xspice old code-model interface and the new ADMS interface based on
Verilog-A and XML.Finally, numerous small bugs have been discovered and fixed, and the
program has been ported to a wider variety of computing platforms.

Supported Analyses

The ngspice simulator supports the following different types of analysis:

1. DC Analysis (Operating Point and DC Sweep)


2. AC Small- Signal Analysis
3. Transient Analysis
4. Pole-Zero Analysis
5. Small-Signal Distortion Analysis
6. Sensitivity Analysis
7. Noise Analysis

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 1


1. DC Analysis

The dc analysis portion of ngspice determines the dc operating point of the circuit with inductors
shorted and capacitors opened. The dc analysis options are specified on the .DC, .TF, and .OP
control lines.There is assumed to be no time dependence on any of the sources within the system
description.The simulator algorithm subdivides the circuit into those portions which require the
analog simulator algorithm and those which require the event-driven algorithm. Each subsystem
block is then iterated to solution, with the interfaces between analog nodes and event-driven nodes
iterated for consistency across the entire system.

Once stable values are obtained for all nodes in the system, the analysis halts and the results
may be displayed or printed out as you request them. A dc analysis is automatically performed
prior to a transient analysis to determine the transient initial conditions, and prior to an ac small-
signal analysis to determine the linearized, small-signal models for nonlinear devices. If requested,
the dc small-signal value of a transfer function (ratio of output variable to input source), input
resistance, and output resistance is also computed as a part of the dc solution. The dc analysis can
also be used to generate dc transfer curves: a specified independent voltage, current source, resistor
or temperature is stepped over a user-specified range and the dc output variables are stored for
each sequential source value.

2. AC Small-Signal Analysis

AC analysis is limited to analog nodes and represents the small signal, sinusoidal solution of the
analog system described at a particular frequency or set of frequencies. This analysis is similar to
the DC analysis in that it represents the steady-state behavior of the described system with a single
input node at a given set of stimulus frequencies.
The program first computes the dc operating point of the circuit and determines linearized,
small-signal models for all of the nonlinear devices in the circuit. The resultant linear circuit is
then analyzed over a user-specified range of frequencies. The desired output of an ac small- signal
analysis is usually a transfer function (voltage gain, trans-impedance, etc). If the circuit has only
one ac input, it is convenient to set that input to unity and zero phase, so that output variables have
the same value as the transfer function of the output variable with respect to the input.

3. Transient Analysis

Transient analysis is an extension of DC analysis to the time domain. A transient analysis begins
by obtaining a DC solution to provide a point of departure for simulating time-varying behavior.
Once the DC solution is obtained, the time-dependent aspects of the system are reintroduced, and
the two simulator algorithms incrementally solve for the time varying behavior of the entire
system. Inconsistencies in node values are resolved by the two simulation algorithms such that the
time-dependent waveforms created by the analysis are consistent across the entire simulated time
interval. Resulting time-varying descriptions of node behavior for the specified time interval are
DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 2
accessible to you. All sources which are not time dependent (for example, power supplies) are set
to their dc value.The transient time interval is specified on a .TRAN control line.

4. Pole-Zero Analysis

The pole-zero analysis portion of Ngspice computes the poles and/or zeros in the small-signal ac
transfer function. The program first computes the dc operating point and then determines the
linearized, small-signal models for all the nonlinear devices in the circuit. This circuit is then used
to find the poles and zeros of the transfer function. Two types of transfer functions are allowed:
one of the form (output voltage)/(input voltage) and the other of the form (output voltage)/(input
current). These two types of transfer functions cover all the cases and one can find the poles/zeros
of functions like input/output impedance and voltage gain. The input and output ports are specified
as two pairs of nodes. The pole-zero analysis works with resistors,capacitors, inductors, linear-
controlled sources, independent sources, BJTs, MOSFETs, JFETs and diodes. Transmission lines
are not supported. The method used in the analysis is a sub-optimal numerical search. For large
circuits it may take a considerable time or fail to find all poles and zeros. For some circuits, the
method becomes "lost" and finds an excessive number of poles or zeros.

5. Small-Signal Distortion Analysis

The distortion analysis portion of Ngspice computes steady-state harmonic and intermodulation
products for small input signal magnitudes. If signals of a single frequency are specified as the
input to the circuit, the complex values of the second and third harmonics are determined at every
point in the circuit. If there are signals of two frequencies input to the circuit, the analysis finds out
the complex values of the circuit variables at the sum and difference of the input frequencies, and
at the difference of the smaller frequency from the second harmonic of the larger frequency.
Distortion analysis is supported for the following nonlinear devices:

● Diodes (DIO),
● BJT,
● JFET,
● MOSFETs (levels 1, 2, 3, 6, 9, BSIM1, BSIM2, BSIM3, BSIM4 and BSIMSOI), ●
MESFETS.
All linear devices are automatically supported by distortion analysis. If there are switches
present in the circuit, the analysis continues to be accurate provided the switches do not change
state under the small excitations used for distortion calculations.

6. Sensitivity Analysis

Ngspice will calculate either the DC operating-point sensitivity or the AC small-signal sensitivity
of an output variable with respect to all circuit variables, including model parameters. Ngspice
calculates the difference in an output variable (either a node voltage or a branch current) by
DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 3
perturbing each parameter of each device independently. Since the method is a numerical
approximation, the results may demonstrate second order effects in highly sensitive parameters, or
may fail to show very low but non-zero sensitivity. Further, since each variable is perturb by a
small fraction of its value, zero-valued parameters are not analyzed (this has the benefit of reducing
what is usually a very large amount of data).

7. Noise Analysis

The noise analysis portion of Ngspice does analysis device-generated noise for the given circuit.
When provided with an input source and an output port, the analysis calculates the noise
contributions of each device (and each noise generator within the device) to the output port voltage.
It also calculates the input noise to the circuit, equivalent to the output noise referred to the
specified input source. This is done for every frequency point in a specified range - the calculated
value of the noise corresponds to the spectral density of the circuit variable viewed as a stationary
Gaussian stochastic process. After calculating the spectral densities, noise analysis integrates these
values over the specified frequency range to arrive at the total noise voltage/current (over this
frequency range). This calculated value corresponds to the variance of the circuit variable viewed
as a stationary Gaussian process.

Circuit Description

General Structure and Conventions


The circuit to be analyzed is described to ngspice by a set of element lines, which define
the circuit topology and element values, and a set of control lines, which define the model
parameters and the run controls. The first line in the input file must be the title, and the last line
must be ".END". The order of the remaining lines is arbitrary (except, of course, that continuation
lines must immediately follow the line being continued).Each element in the circuit is specified by
an element line that contains the element name, the circuit nodes to which the element is connected,
and the values of the parameters that determine the electrical characteristics of the element. The
first letter of the element name specifies the element type. The format for the NGSPICE element
types is given in what follows. The strings XXXXXXX , YYYYYYY , and ZZZZZZZ denote
arbitrary alphanumeric strings. For example, a resistor name must begin with the letter R and can
contain one or more characters. Hence, R , R1, RSE, ROUT, and R3AC2ZY are valid resistor
names. Details of each type of device are supplied in a following section. Fields on a line are
separated by one or more blanks, a comma, an equal (‘ = ’) sign, or a left or right parenthesis; extra
spaces are ignored. A line may be continued by entering a ‘ + ’ (plus) in column 1 of the following
line; NGSPICE continues reading beginning with column 2. A name field must begin with a letter
(A through Z) and cannot contain any delimiters. A number field may be an integer field (12, -44),
a floating point field (3.14159), either an integer or floating point number followed by an integer
exponent (1e-14, 2.65e3), or either an integer or a floating point number followed by one of the
following scale factors:

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 4


Suffix Name Factor

T Tera 1012

G Giga 109

Meg Mega 106

K Kilo 103

mil mil 25.4 x 10-6

m milli 10-3

u micro 10-6

n nano 10-9

p pico 10-12

f femto 10-15

Letters immediately following a number that are not scale factors are ignored, and letters
immediately following a scale factor are ignored. Hence, 10, 10V, 10 Volts, and 10Hz all represent
the same number, and M, MA, MSec, and MMhos all represent the same scale factor. Note that
1000, 1000.0, 1000Hz, 1e3, 1.0e3, 1kHz, and 1k all represent the same number. Nodes names may
be arbitrary character strings. The datum (ground) node must be named ‘0’ (zero). Note the
difference in NGSPICE where the nodes are treated as character strings and not evaluated as
numbers, thus ‘0’ and ‘00’ are distinct nodes in NGSPICE but not in SPICE2. NGSPICE needs
that the following topological constraints are satisfied:

● The circuit cannot contain a loop of voltage sources and/or inductors and cannot contain a
cut-set of current sources and/or capacitors.
● Each node in the circuit must have a dc path to ground.
● Every node must have at least two connections except for transmission line nodes (to permit
unterminated transmission lines) and MOSFET substrate nodes (which have two internal
connections anyway).

Basics: Title Line, Comment Lines and .END Line

Title Line
Examples:
POWER AMPLIFIER CIRCUIT
TEST OF CAM CELL

The title line must be the first in the input file. Its contents are printed verbatim as the heading
for each section of output.
DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 5
.END Line Examples:
.END

The"End" line must always be the last in the input file. Note that the period is an integral part of
the name.

Comments
General Form:
* <any comment> Examples:
* RF=1K Gain should be 100
* Check open-loop gain and phase margin

The asterisk in the first column indicates that this line is a comment line. Comment lines may be
placed anywhere in the circuit description. Note that NGSPICE also considers any line with leading
white space to be a comment.

Device Models

General form:
.MODEL MNAME TYPE(PNAME1=PVAL1 PNAME2=PVAL2 ... )
Examples:
.MODEL MOD1 NPN (BF=50 IS=1E-13 VBF=50)

Most simple circuit elements typically require only a few parameter values. However, some
devices (semiconductor devices in particular) that are included in NGSPICE require many
parameter values. Often, many devices in a circuit are defined by the same set of device model
parameters. For these reasons, a set of device model parameters is defined on a separate .MODEL
line and assigned a unique model name. The device element lines in NGSPICE then refer to the
model name.
For these more complex device types, each device element line contains the device name,
the nodes to which the device is connected, and the device model name. In addition, other optional
parameters may be specified for some devices: geometric factors and an initial condition (see the
following section on Transistors and Diodes for more details).
MNAME in the above is the model name, and type is one of the following fifteen types:

Code Model Type

R Resistor model

C Capacitor model

L Inductor model

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 6


SW Voltage controlled switch

CSW Current controlled switch

URC Uniform distributed RC mode

LTRA Lossy transmission line model

D Diode model

NPN NPN BJT model

PNP PNP BJT model

NJF N-channel JFET model

PJF P-channel JFET model

NMOS N-channel MOSFET model

PMOS P-channel MOSFET model

NMF N-channel MESFET model

PMF P-channel MESFET model

VDMOS Power MOS model

Parameter values are defined by appending the parameter name followed by an equal sign and the
parameter value. Model parameters that are not given a value are assigned the default values given
below for each model type. Models, model parameters, and default values are listed in the next
section along with the description of device element lines.

Subcircuits
A subcircuit that consists of NGSPICE elements can be defined and referenced in a fashion similar
to device models. The subcircuit is defined in the input file by a grouping of element lines; the
program then automatically inserts the group of elements wherever the subcircuit is referenced.
There is no limit on the size or complexity of subcircuits, and subcircuits may contain other
subcircuits.

.SUBCKT Line

General form:
.SUBCKT subnam N1 <N2 N3 ...> Examples:
.SUBCKT OPAMP 1 2 3 4

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 7


A circuit definition is begun with a .SUBCKT line. “subnam” is the subcircuit name, and N1, N2,
... are the external nodes, which cannot be zero. The group of element lines which immediately
follow the .SUBCKT line define the subcircuit. The last line in a subcircuit definition is the .ENDS
line (see below). Control lines may not appear within a subcircuit definition; however, subcircuit
definitions may contain anything else, including other subcircuit definitions, device models, and
subcircuit calls (see below). Note that any device models or subcircuit definitions included as part
of a subcircuit definition are strictly local (i.e., such models and definitions are not known outside
the subcircuit definition). Also, any element nodes not included on the .SUBCKT line are strictly
local, with the exception of 0 (ground) which is always global.

ENDS Line
General form:
.ENDS <SUBNAM>
Examples:
.ENDS OPAMP
The"Ends" line must be the last one for any subcircuit definition. The subcircuit name, if included,
indicates which subcircuit definition is being terminated; if omitted, all subcircuits being defined
are terminated. The name is needed only when nested subcircuit definitions are being made.

Syntax Reference

This is the same syntax reference supplied in the HSPICE tutorial. Any bracketed labels must be
replaced entirely (i.e. if you want a value of 5 V, you should replace <value> with 5V).

● Independent voltage source v<name> <+ terminal> <- terminal> <value>


Eg: V1 5 0 10

● Independent current source i<name> <+ terminal> <- terminal> <value>


Eg: I1 8 0 5

● Voltage-controlled voltage source


E<name> <+ terminal> <- terminal> <+ control> <- control> <gain> Eg:
E2 3 4 0.5 5 0.005

● Current-controlled voltage source (vcontrol refers to the voltage source which the
controlling current flows through)
H<name> <+ terminal> <- terminal> <vcontrol> <gain>
Eg: H3 6 4 V1 2

● Voltage-controlled current source


G<name> <+ terminal> <- terminal> <+ control> <- control> <gain>
Eg: G3 7 9 0.05 0.5 0.01
DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 8
● Current-controlled current source ( vcontrol refers to the voltage source which the
controlling current flows through)
F<name> <+ terminal> <- terminal> <vcontrol> <gain>
Eg: F5 8 4 V2 2

● Sinusoidal source (used as a <value>) sin(<offset> <amplitude> <frequency> <delay>


<damping> <phase>)
Eg: Sin(0 5 50 0.1 0 30)

● Square wave source (used as a <value>) pulse(<vmin> <vmax> <delay> <rise time> <fall
time> <pulse width> <period>) Eg: Pulse(0 5 0.01 0.2 0.6 2 5)

● Piecewise linear source (used as a<value>) pwl(<t0> <v0> <t1> <v1> <t2> <v2> ...)
Eg: Pwl(0 0 5 10 10 20)

● Resistor r<name> <terminal 1> <terminal 2> <value>


Eg: R1 5 4 100

● Capacitor c<name> <terminal 1> <terminal 2> <value> Eg: C2 8 9 100u


● Inductor l<name> <terminal 1> <terminal 2> <value>
Eg: L4 6 7 100m

● Model (type can be nmos , pmos , NPN , PNP , or D for diode)


.model <name> <type> (<parameter list>)
Eg: .MODEL LED3 D(IS=93.1P RS=42M N=7.47 BV=5 IBV=30U CJO=2.97P VJ=.75
M=.333 TT=4.32U)

● MOSFET (you can specify additional parameters, such as W=<value> L=<value>, in the
parameter list)
m<name> <drain> <gate> <source> <body> <model> <parameter list>
Eg: M1 4 6 0 0 mymos w=80u l=1u

● BJT
q<name> <collector> <base> <emitter> <model> <parameter list> Eg:
Q1 2 5 0 mybjt

● Diode d<name> <+ terminal> <- terminal> <model> <parameter list>


Eg: d1 1 2 D1N750

● AC analysis (pick either lin , dec , or oct )


DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 9
.ac <lin|dec|oct|> <number of samples> <freq start> <freq stop>
Eg: .ac dec 10 0.01 1Meg

● DC analysis
.dc <source> <start> <stop> <step>
Eg: .dc v1 -10 5 0.05

● Nested DC analysis (source1 is swept, source2 is stepped)


.dc <source1> <start1> <stop1> <step1> <source2> <start2><stop2> <step2>
Eg: .dc vcc 0 10 1 vin 0.7 0.8 0.02

● Transient analysis
.tran <t step> <t stop>
Eg: tran 0.001ms 0.2ms

● TF analysis
.tf v(<node>) <source>
Eg: .tf v(3) vin

● PZ analysis
.pz v(<node>) <source> Eg:
.pz I1(5) vin

Ngspice Commands

Commands used in Ngspice-

1. help - It opens ngspice manual and gives information about all ngspice commands.
2. listing - It prints the current netlist (source code in ngspice is known as netlist) of the circuit.
3. print v(out) or print v(3) - It prints the voltage values of corresponding node name or node
number.
4. plot v(out) or plot v(3) - It plots the voltage values of corresponding node name or node
number.
5. plot v(in1,in2) or print v(2,1) - It plots the voltage difference of two nodes.
6. plot v(in1) - v(in2) or print v(2) - v(1) - It plots or prints the voltage difference of two
nodes.
7. plot v(in1) + v(in2) or print v(2) + v(1) - It plots or prints the voltage addition of two nodes.
8. plot v(in1) * v(in2) or print v(2) * v(1) - It plots or prints the voltage multiplication of two
nodes.
9. plot v(in1) / v(in2) or print v(2) / v(1) - It plots or prints the voltage division of two nodes.
10. plot i(vs) or print i(v1)- It prints the current values of corresponding branch. Here we have
connect 0v voltage source in series(vs or v1),to find current in particular branch.
DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 10
11. plot i(v2) or plot i(vout) - It plots the current values of corresponding branch. Here we have
connect 0v voltage source in series(vs or v1),to find current in particular branch.
12. plot mag(v(2)) or print mag(v(2)) - It plots or prints magnitude values of corresponding
node.
13. plot db(v(2)) or print db(v(2)) - It plots or prints voltage values of corresponding node in
decibel.
14. plot imag(v(2)) or print imag(v(2)) - It plots or prints imaginary part of voltage values of
corresponding node.
15. plot phase(v(2)) or print phase(v(2))- It plots or prints phase values of corresponding node
in radian.
16. plot 180/PI*phase(v(2)) or print 180/PI*phase(v(2)) - It plots or prints phase values of
corresponding node in degree.

PROCEDURE:

● You can use any text editor (say, gedit) to write your circuit netlist. The first line in an
NGSPICE file is not executed. It is used to describe the aim of the circuit being simulated.
● All NGSPICE comments start with an asterix, i.e. ‘*’
● The NGSPICE file comprises of the circuit netlist followed by the details of the analysis
the user wishes to do.
● NGSPICE files are usually saved with the extension ‘.cir’ or ‘ .spice’.
● Circuit components are identified by their first letter of naming, called prefix , i.e. resistors
begin with r , capacitors with c, bipolar transistors with q, MOSFETs with m, voltage
sources with v and so on.
● All circuit nodes are named/numbered. The netlist requires one ground node (zero
potential).
● Open the Linux terminal, and change the working directory to the folder where your netlist
file is saved. e.g. if the file is in the Documents folder, type cd ∼ /Documents in the
command prompt.
● Run the netlist file using the command ngspice <filename.cir> Eg: ngspice rccircuit.cir
● Instead of the above two steps, you can just give the command ngspice first, then you can
give the location of the .cir file to be executed.
● Then the file will be executed and the results will be shown.
● To come out of the ngspice, just give a command exit or quit.

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 11


EXPERIMENT 1A

RC Circuit frequency response

Program:

r1 1 2 1k c1
2 0 1u
*Specifying an AC source with zero dc
vin 1 0 dc 0 ac 1
*AC analysis for 1hz to 1MHz, 10 points per decade
.ac dec 10 1 1Meg
.control run
*white background set
color0=white
* black grid and text (only needed with X11, automatic with MS Win)
set color1=black * wider grid and plot lines set xbrushwidth=2
*Magnitude dB plot for v(2) on log scale plot
vdb(2) xlog
*Phase degrees plot for v(2)on log scale plot
{57.29*vp(2)} xlog
.endc
.end

Frequency response:

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 12


EXPERIMENT 1B

RC Circuit Transient Response

Program:

* resistor connected between nodes 1 and 2 r1


1 2 1k
*capacitor connected between nodes 2 and 0
c1 2 0 1u
*piecewise linear input voltage vin 1 0 pwl(0
0 10ms 0 11ms 5v 20ms 5v) *transient
analysis for 20ms, step size 0.02ms
.tran 0.02ms 20ms
*defining the run-time control functions
.control run
*white background set
color0=white
* black grid and text (only needed with X11, automatic with MS Win)
set color1=black * wider grid and plot lines set xbrushwidth=2
*plotting input and output voltages plot
v(1) v(2)
.endc
.end

Transient response:

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 13


EXPERIMENT 1C

DIODE Characteristics

Circuit diagram

v1 1 0 dc=5 d1
1 2 D1N 750
r1 2 0 1k
.model d1n750 D(Vj=.75 Cjo=175p Rs=.25 Eg=1.11 M=.5516 Nbv=1.6989 N=1 Bv=8.1 Fc=.5
Ikf=0 Ibv=20.245m Is=880.5E-18 Xti=3)
.save all @d1[id]
.dc v1 -10 5 0.05
* Control Statements
.control
run
*white background set
color0=white
* black grid and text (only needed with X11, automatic with MS Win) set
color1=black
* wider grid and plot lines set xbrushwidth=2 plot @d1[id] vs v(1)
.endc
.end

V-I Characteristics:

DEPARTMENT OF ELECTRICAL ENGINEERING, INDIAN INSTITUTE OF TECHNOLOGY- DHARWAD 14

You might also like