Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ngspice Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

NGSPICE Tutorial

EEE 52 1516s2

What is NGSPICE
mixed-level/mixed-signal circuit simulator based
on three open source software packages
Spice3f5, Cider1b1, Xspice

SPICE (Simulation Program with Integrated Circuit


Emphasis) from UC Berkeley is one of the most
used circuit simulators around the world and can
be considered the father of modern circuit
simulators.
For verifying circuit operation at the transistor level
instead of board level (composed of discrete parts)

Why study SPICE


It is free!
Modern (commercial) simulators have a
similar syntax.
Might be useful when encountering bugs or weird
results
At the end of the day, we must not rely purely on
simulations, there must always be some hypothesis or
educated guess.

Familiarize how circuit simulators work


Aids in scripting simulations

How to get NGSPICE


Download only. No need to install.

Running NGSPICE
After extracting, run the executable file
ngspice under the bin folder.

ngspice terminal

However, we need to prepare first the


code before we use this terminal.

NGSPICE Code General Structure


First line: title
No need for special characters (e.g. My RC Filter)

Last line: .end


See examples later

Each circuit element is specified by an instance line


which contains the following:
1. Element instance name
2. Circuit nodes to which the element is connected
3. Parameters that determine the characteristic of the
circuit element
Example: Vsupply 1 0 10
Instance Name

Nodes

Parameter

Sample Netlisting Process


Draw schematic on a paper

Sample Netlisting Process


Draw schematic on a paper
Assign node names and
instance names

Sample Netlisting Process


Draw schematic on a paper
Assign node names and
instance names
Write the netlist, one line per
instance, following the
format:
<name><nodes><parameter/s>
instance definition can be made
in any order

* CE AMPLIFIER *
V1
I1
D1
R1
Q1

1
1
2
1
0

0
2
0
3
2

5V
50uA
50kOhms
3

Important NGSPICE Element Types


First Letter of
Instance Name

Element Description

Capacitor

Diode

I
L
M
Q
R
V

Current Source

A name field must start with a


letter (A to Z) without any
delimiter

Inductor
MOSFET
BJT
Resistor

Voltage Source

* CE AMPLIFIER *
V1
I1
D1
R1
Q1

1
1
2
1
0

0
2
0
3
2

5V
50uA
50kOhms
3

NGSPICE Numbers & Scale Factors


Suffix

Name

Factor

Tera

1012

Giga

109

Meg

Mega

106

Kilo

103

mil

Mil

25.4x10-6

milli

10-3

Micro

10-6

Nano

10-9

Pico

10-12

femto

10-15

A number field can follow any of the


following formats:

13 (integer)
-1 (negative integer)
3.14159 (floating point)
1e-16, 1.23e6 (integer exponent)
1Meg (scaling factor)

Non-scale factor letters are ignored


1V = 1Volt = 1

Letters after the scale factor are


ignored
1mV = 1mVolt = 1munpf = 1m = 0.001

Either M or m will denote milli. Use


meg for 106. Same with K or k for
kilo.

NGSPICE Node Names


Node names can be any arbitrary string
First letter can be that of a valid element type
Valid node names: node1, 1, v1

Ground node must be named 0 (zero).


gnd is also accepted as the ground node.

Node names are considered as strings, not


numbers, and are case sensitive:
02 and 2 are considered as different nodes
v1 and V1 are considered as different nodes too

Defining variables
Variables can be defined using .param
Easier to change variable definition than to edit
numbers at different sections of the code
Helpful especially for long codes.
Used for parametric analysis (later)
* Resistors *

* Resistors *
.param Rx=1kohm

R1 1 2 1kohm
R2 3 4 1kohm
R3 5 6 1kohm

R1 1 2 Rx
R2 3 4 Rx
R3 5 6 Rx

Other Coding Conventions


Comments are started with an asterisk (*)
Need to start with * every line if spanning more
than one line
There is no ending symbol for a comment

Common Analyses/Simulations Used


DC Analysis (.op .dc .tf)
Operating point, DC sweep, transfer function
Inductors are shorted; capacitors are open

AC Small Signal Analysis (.ac)


Includes frequency dependence of reactive
components

Transient Analysis (.tr)

Setting Up Simulations
.DC
Format:
.dc src1 start1 stop1 incr1 [src2
start2 stop2 incr2]
src can be an independent voltage/current source, a
resistor, or temperature

Examples:
.dc R1 10k 100k 2.5k
.dc VDS 0 10 0.5 VGS 0 5 1

Setting Up Simulations
.AC AC small signal analysis
.OP operating point
.TF transfer function
Like AC analysis but evaluated only at 0Hz or DC

.TR transient analysis

Read these up on the ngspice manual which is


easy to understand :D

Running the simulation


Create netlist using your favorite text editor.
Optional: save the file with a .sp extension (e.g.
circuit.sp) to indicate that it is a spice file

Open ngspice
Go to the directory that contains your netlist
through the terminal (cd [directory])
Load the spice file (source [filename])
Run the simulation (run)
View the results (plot/meas/print)

Plotting Voltages and Currents


Plotting voltages is straightforward
All node voltages are saved by the simulator by
default
plot dc v(node1) v(node2,0)
plot dc v(node1,node2)

Plotting currents is a bit more tricky


Only currents through voltage sources are saved
Place dummy voltage sources (0V) at branches of
concern to save and measure the current
plot i(vsource1) vsource2#branch

SPICE demo

You might also like