NGspice Basics
NGspice Basics
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 1/17
Introduction
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 2/17
What is NGSPICE?
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 3/17
NGSPICE provides you with....
Semiconductor Devices
I Pre-defined circuit elements such as diodes and transistors
I Allows you to define or include models of specific devices e.g.
specialized transistors and op-amps
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 4/17
Getting Started with NGSPICE (Linux)
I 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.
I The NGSPICE file comprises of the circuit netlist followed by the details
of the analysis the user wishes to do.
I NGSPICE files are usually saved with the extension ‘.cir’ or ‘.spice’.
I All circuit nodes are named/numbered. The netlist requires one ground
node (zero potential).
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 5/17
Inside the NGSPICE Shell
I Once any netlist is run by NGSPICE, the terminal is hooked to an NGSPICE
shell, with a prompt such as ngspice 1 ->. You can exit the NGSPICE shell
anytime by typing exit or quit.
I It is not always necessary to quit NGSPICE every time to run a new netlist. You
can use the command source <filename>.cir to simulate a new netlist file
using the NGSPICE prompt.
I The commands specified between .control and .endc in the netlist file may be
used in the NGSPICE prompt separately.
I The waveforms may be saved as a postscript file by clicking on the hardcopy icon
on the waveform window. However, this saves it as a default filename in the root
directory. A better way to save the waveform in the working directory would be to
use the following commands (say for the R-C circuit discussed above)
set hcopydevtype=postscript
hardcopy rcPlot.ps v(1) v(2)
I You can even save the plots using different colours. Read the NGSPICE manual
for that, and much more!
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 6/17
Example I- Transient Analysis of an RC Circuit
A simple RC circuit, excited by a user-defined signal Vin . We want to find the capacitor voltage i.e. Vc (t). The
netlist is as shown.
1 1k 2
vin +
5V
vin + 1u vc
−
0 -
10ms 11ms 20ms t
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 8/17
Example II- AC Analysis of RC Circuit
For the same R-C circuit discussed in Example I, let us do the small-signal AC
analysis, i.e. find its frequency response.After running this, you should be able
to see two plot windows- a magnitude (dB) plot and a phase (degrees) plot.
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 9/17
Subcircuits
I We have seen how to use electronic devices to build a circuit and test it
using NGSPICE.
I Various electronic devices have their own existing model files that
represent the electrical behaviour of that device, which we can use in a
netlist. What if we now have an existing circuit, and want to use it to build
bigger circuits?
I A typical example is using an op-amp (operational amplifier) to design a
simple amplifier or a filter. Note that, an op-amp is a pre-existing circuit
and not a device. It is made of many transistors.
I NGSPICE allows us to define an op-amp as a subcircuit. A subcircuit is
much like an IC- we know its pins to interface with the outside world, but
we need not be familiar with the inside circuit!
I A subcircuit is a collection of devices familiar to SPICE. A subcircuit is
identified by the prefix x. The usage is very similar to that of a model file.
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 10/17
Example - Subcircuits
See the circuit shown below.
IN R1 1 R2 2 R3 OUT
1 kΩ 1 kΩ 1 kΩ
Vin C1 C2 C3
5 Vp-p 1 uF 1 uF 1 uF
0 Voffset
1 kHz
I The netlist of the circuit will have redundant initiations of resistors and
capacitors.
I Since all resistors and capacitors are in the same orientation, a subcircuit
can be created with these components as shown below.
In R Out
1 kΩ
C
resistor 1 uF
com
I The above subcircuit has 3 terminals, In, Out and com. It can be invoked
multiple times to form the complete circuit.
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 11/17
Example - Subcircuits (cont’d)
Let us do a transient analysis for the circuit discussed previously.
RC Circuit Transient Analysis using Subcircuits
*Defining the Subcircuit ’RC subcircuit’
.subckt RC subcircuit In Out com
r In Out 1K
c Out com 1u
.ends
*Subcircuit definition ends here
vsin IN gnd sin(0 2.5 1K 0 0)
Invoking RC subcircuit
xrc 1 IN 1 gnd RC subcircuit
xrc 2 1 2 gnd RC subcircuit
xrc 3 2 OUT gnd RC subcircuit
.control
tran 0.02m 40m
plot v(IN)
plot v(OUT)
.endc
.end
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 12/17
Example - Subcircuits (cont’d)
Thus, a subcircuit can be invoked multiple times to form the complete circuit.
We can also initiate the components of the subcircuit with desired values,
while its being invoked.
RC Circuit Transient Analysis using Subcircuits
.subckt RC subcircuit In Out com r 1=1K c 1=1u
r In Out r 1
c Out com c 1
.ends
vsin IN gnd sin(0 2.5 1K 0 0)
xrc 1 IN 1 gnd RC subcircuit
xrc 2 1 2 gnd RC subcircuit r 1=100 c 1=0.1u
xrc 3 2 OUT gnd RC subcircuit
.control
tran 0.02m 5m
plot v(IN) v(OUT)
.endc
.end
Here, r 1 and c 1 are the parameters of the subcircuit, with default values of 1KΩ and
1µF respctively. The circuit for the above netlist will have R2 and C2 as 100Ω and
0.1µF respectively. All other resistors and capacitors will have the default values of
subcircuit parameters.
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 13/17
Example- Integrator using op-amp (741)
1 MΩ
1 kΩ 0.1 µF
5
2 + 15 V
Vin 1 −
0 Vout
+ 4
0 3 - 15 V
+ 3
Vin − Vout
2V
0
Id
+ Vds
− 1.8 V
+ Vgs
− 1.8 V
To generate the family of curves, we change one of the voltages, say Vgs in
steps, while the other, Vds is varied its entire range for each step of Vgs.
Thus, we can plot the MOSFET current, Id (for various step values of Vgs)
versus Vds.
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 16/17
MOSFET Characterization- Family of Curves (cont’d)
Let us generate the id v/s vds characteristics for various values of vgs. You
should be getting the I-V characteristics as shown below.
Family of Curves
Debapratim Ghosh & Mohamed Jabir Dept. of EE, IIT Bombay 17/17