Ngspice Tutorial

From Microlab Classes
Jump to navigation Jump to search

Predicting the behavior of an electronic circuit using simulation tools enables us to run experiments, explore a larger set of alternatives, and verify results quickly and inexpensively.

Most modern circuit simulators are based on the SPICE (Simulation Program with Integrated Circuit Emphasis) simulator, developed at UC Berkeley, and first presented in 1973. In this tutorial, we will use ngspice, and just like the original Berkeley SPICE, it is open source.

Classes Using this Tutorial

  • CoE 197U Introduction to Analog and Digital Integrated Circuit Design
  • EE 220 Analog Integrated Circuits

Installing ngspice

ngspice is available for Linux, MacOS, and Windows machines. Visit the ngspice download page for specific installation instructions. The user manual is available here.

Our First Circuit

Let's simulate the circuit in Fig. 1 using ngspice. Note that we labeled every device (Q1, Vbe, and Vce), as well as labeled every unique node (b1, c1, and 0).

Figure 1: Our first circuit.

First, we need to describe the circuit using a "SPICE deck". In the 1970's, punched cards were used, and hence the reference to a "deck" of cards. Now, this just refers to a text file containing a description of our circuit. Since our description will contain circuit elements and nodes (or "nets"), this file is also commonly known as a "netlist".

This is the netlist for the circuit in Figure 1:

 1 * Transistor Characteristic Curves
 2 * LPA 2020-04-16
 3  
 4 .include 2N2222A.lib
 5 .options savecurrents
 6  
 7 Q1  c1 b1 0     Q2n2222a
 8 Vbe b1 0        dc 0
 9 Vce c1 0        dc 0.2
10  
11 .control
12  
13 dc Vbe 500m 750m 1m
14  
15 wrdata circuit1.dat @Q1[ic]
16  
17 .endc
18  
19 .end

Parts of a Netlist

The first line of a SPICE netlist is treated as the circuit title or description, and is always ignored by the simulator. Comments, like the one in line 2, are preceeded by an asterisk, (*).

SPICE “dot commands” are commands that provide directives to the simulator. These are the commands in lines 4, 5, 11, 17, and 19.

  • The .include <filename> command includes other files, such as those that contain model information. In this case, .include 2N2222A.lib (line 4) adds the model file for a transistor, the 2N2222A NPN BJT. When you download the model file, make sure you rename it correctly[fn 1].
  • The .options savecurrents (line 5) command tells the simulator to save the current data, since by default, only voltage data is saved.
  • The .control and .endc encapsulate the control statements, which specify which analyses will be performed by the simulator.
  • The .end command signifies the end of the netlist file.

The main part of the netlist is the list of devices together with the nodes where the terminals of these devices are connected to.

  • Line 7 instantiates a BJT with format:
    • Qxxx <collector node> <base node> <emitter node> <model name>
    • The instance name of a BJT always starts with a Q, and is followed by the names of the nodes connected to its collector, base, and emitter. You can choose the names of the nodes. Note that, by convention, node 0 is the ground node. The model name is the name given in the model file.
  • Lines 8 and 9 instantiate the voltage sources Vbe and Vce using:
    • Vxxx <pos> <neg> <parameters>
    • In this case, Vbe and Vce are both ideal DC voltage sources with values 0.0V and 0.2V respectively.

Analysis Statements are placed inside the control block, and specify which analysis needs to be performed. Line 13 tells the simulator to perform a DC Analysis, i.e. sweep a source voltage or current and determine the behavior of the circuit. In this case, the simulator will vary Vbe from 500mV to 750mV in steps of 1mV. A DC analysis can be invoked by:

  • dc <independent source> <start> <stop> <step>

Line 15 instructs the simulator to write the collector current data of transistor Q1 (using the '@' directive) to a file named circuit1.dat.

Save this netlist to a directory as circuit1.sp. Go to this directory and we will run ngspice from there.

Running the Simulation

The simulator can be invoked by running ngspice from the command line. This brings up the simulator in interactive mode, and you are presented with a prompt:

******
** ngspice-31 : Circuit level simulation program
** The U. C. Berkeley CAD Group
** Copyright 1985-1994, Regents of the University of California.
** Please get your ngspice manual from http://ngspice.sourceforge.net/docs.html
** Please file your bug-reports at http://ngspice.sourceforge.net/bugrep.html
******
ngspice 1 ->

To run the simulator, type in the command source circuit1.sp:

ngspice 1 -> source circuit1.sp

Circuit: * transistor characteristic curves

Doing analysis at TEMP = 27.000000 and TNOM = 27.000000


No. of Data Rows : 251
ngspice 2 ->

As expected, we should generate 251 points (500mV to 750mV in 1mV steps). You should also see a file named circuit1.dat in your directory.

Viewing the Output

Aside from the output file, we can view the results graphically via the ngspice interactive shell. To plot the collector current of transistor Q1, type in plot @Q1[ic] at the prompt.

ngspice 2 -> plot @Q1[ic]
ngspice 3 ->

A graph of the collector current vs. the base-emitter voltage should appear:

Figure 2: The collector current of transistor Q1 as the base-emitter voltage is varied from 500mV to 750mV in 1mV steps.

As expected, the graph is exponential since, ideally, .

End of the ngspice Tutorial

Congratulations! You have successfully installed ngspice on your computer, and simulated the effects of varying the base-emitter voltage of an NPN BJT on its collector current.

Notes

  1. The original model file name has a .TXT extension that, depending on your system, could be hidden.