bekwam courses

EE Stuff - 002 Ngspice Hello World

November 5, 2017

This article shows you how to get started with Ngspice. Ngspice is a circuit simulator program. There are many adaptations of the Spice simulation libraries. This particular demonstration was done on an Ubuntu command line utility. However, the circuit files will work in other installations.

To install the ngspice program on Ubuntu, run apt-get.

$ sudo apt-get install ngspice

"Hello, World" is a term used in computer science to present a simplified program that shows a concept. Other parts of a program that you might find in the real world are ommitted so as not to distract. To start working with Ngspice, this article focuses on a basic circuit.

The following resistive circuit shows a 5V voltage source feeding a 1kΩ resistor.

Schematic of a simple resistive circuit
Schematic of Hello World Circuit Circuit

From Ohm's Law, this will produce a current of 5mA.

5V / 1kΩ = 0.005 A = 5mA

We're going to run ngspice in batch mode (rather than interactive mode) which means that we'll prepare a file containing the circuit definition and processing directives. These are the contents of a file called hw.cir. ngspice is not particular about the suffix of the file; you can use .txt or .dat if you want.

Hello World Circuit

* $ ngspice -b hw.cir -o hw-output.dat

v1 0 1 dc 5
r1 1 0 1k

.options NOACCT


Meta Information

The first line "Hello World Circuit" is a title used in the output of ngspice. * denotes a comment. Although the "$ ngspice" wording looks technical, it's just there as a convenient location. You can put anything you want into a comment.

The ngspice file is not case sensitive.


The next non-blank lines define the voltage source, the resistor, and their wiring. Referring back to the schematic, v1 is the name I used for my voltage source. In ngspice, a voltage source name must start with the letter "V". Similarly, the resistor r1 is listed. Resistors must start with the letter "R". Here is a mixture of valid and invalid voltage source and resistor names.

  1. V100
  2. VOUT
  3. VEXPO
  4. R56
  5. E1 (bad)

Walking through the v1 definition, there is a 0 followed by a 1. There is always a Node 0 in ngspice and that is ground for our voltage source. The other end of the voltage source is 1. The token "dc" indicates that this is a DC voltage source. "5" refers to the 5V value.

In the r1 definition, there is a 1 followed by a 0. The 1 is the common connection between the voltage source's positive terminal and the resistor. The 0 links the resistor to the negative terminal of the voltage source. The 1k indicates 1,000 Ω.

Referring back to the schematic, Node 0 and Node 1 are identified. The node identification process and important pre-ngspice step that you will need to do before forming the ngspice model.


The .op is an operating point analysis. This is a simply steady state analysis with the voltage providing exactly v1.

.options is a directive to specify the runtime behavior. In this case, NOACCT is omitting some of the output about ngspice itself that isn't needed for understanding the circuit.

Finally, there is an .end token to conclude the model and its directives.


To run the program, I enter the following command.

$ ngspice -b hw.cir -o hw-output.dat

The contents of hw-output.dat are

Circuit: hello world circuit

Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

No. of Data Rows : 1
	Node                                  Voltage
	----                                  -------
	----	-------
	V(1)                             -5.00000e+00

	Source	Current
	------	-------

	v1#branch                        -5.00000e-03

 Resistor models (Simple linear resistor)
      model                     R

        rsh                     0
     narrow                     0
      short                     0
        tc1                     0
        tc2                     0
       defw                 1e-05
          l                 1e-05
         kf                     0
         af                     0
          r                     0
     bv_max                 1e+99

 Resistor: Simple linear resistor
     device                    r1
      model                     R
 resistance                  1000
         ac                  1000
      dtemp                     0
     bv_max                 1e+99
      noisy                     1
          i                -0.005
          p                 0.025

 Vsource: Independent voltage source
     device                    v1
         dc                     5
      acmag                     0
      pulse         -
       sine         -
        sin         -
        exp         -
        pwl         -
       sffm         -
         am         -
    trnoise         -
   trrandom         -
          i                -0.005
          p                 0.025

The resulting i appears at the end of the input (-0.005) as well as the power. Additionally, at the top of the output there is a current associated with v1#branch. The current appears negative since, according to the Ngspice documentation, the current flows from the positive terminal of the voltage source, through the voltage source and out the negative terminal.

If you have any questions or found an error, feel free to send email to

For more on Ngspice, check out their documentation.

Headshot of Carl Walker

By Carl Walker

President and Principal Consultant of Bekwam, Inc