Computers can be powerful tools if used properly, especially in the realms of science and engineering. Software exists for the simulation of electric circuits by computer, and these programs can be very useful in helping circuit designers test ideas before actually building real circuits, saving much time and money.
These same programs can be fantastic aids to the beginning student of electronics, allowing the exploration of ideas quickly and easily with no assembly of real circuits required. Of course, there is no substitute for actually building and testing real circuits, but computer simulations certainly assist in the learning process by allowing the student to experiment with changes and see the effects they have on circuits. Throughout this book, I’ll be incorporating computer printouts from circuit simulation frequently in order to illustrate important concepts. By observing the results of a computer simulation, a student can gain an intuitive grasp of circuit behavior without the intimidation of abstract mathematical analysis.
To simulate circuits on computer, I make use of a particular program called SPICE, which works by describing a circuit to the computer by means of a listing of text. In essence, this listing is a kind of computer program in itself, and must adhere to the syntactical rules of the SPICE language. The computer is then used to process, or “run,” the SPICE program, which interprets the text listing describing the circuit and outputs the results of its detailed mathematical analysis, also in text form. Many details of using SPICE are described in volume 5 (“Reference”) of this book series for those wanting more information. Here, I’ll just introduce the basic concepts and then apply SPICE to the analysis of these simple circuits we’ve been reading about.
First, we need to have SPICE installed on our computer. As a free program, it is commonly available on the internet for download, and in formats appropriate for many different operating systems. In this book, I use one of the earlier versions of SPICE: version 2G6, for its simplicity of use.
Next, we need a circuit for SPICE to analyze. Let’s try one of the circuits illustrated earlier in the chapter. Here is its schematic diagram:
This simple circuit consists of a battery and a resistor connected directly together. We know the voltage of the battery (10 volts) and the resistance of the resistor (5 Ω), but nothing else about the circuit. If we describe this circuit to SPICE, it should be able to tell us (at the very least), how much current we have in the circuit by using Ohm’s Law (I=E/R).
SPICE cannot directly understand a schematic diagram or any other form of graphical description. SPICE is a text-based computer program, and demands that a circuit be described in terms of its constituent components and connection points. Each unique connection point in a circuit is described for SPICE by a “node” number. Points that are electrically common to each other in the circuit to be simulated are designated as such by sharing the same number. It might be helpful to think of these numbers as “wire” numbers rather than “node numbers, following the definition given in the previous section. This is how the computer knows what’s connected to what: by the sharing of common wire, or node, numbers. In our example circuit, we only have two “nodes,” the top wire and the bottom wire. SPICE demands there be a node 0 somewhere in the circuit, so we’ll label our wires 0 and 1:
In the above illustration, I’ve shown multiple “1” and “0” labels around each respective wire to emphasize the concept of common points sharing common node numbers, but still this is a graphic image, not a text description. SPICE needs to have the component values and node numbers given to it in text form before any analysis may proceed.
Creating a text file in a computer involves the use of a program called a text editor. Similar to a word processor, a text editor allows you to type text and record what you’ve typed in the form of a file stored on the computer’s hard disk. Text editors lack the formatting ability of word processors (no italic, bold, or underlined characters), and this is a good thing, since programs such as SPICE wouldn’t know what to do with this extra information. If we want to create a plain-text file, with absolutely nothing recorded except the keyboard characters we select, a text editor is the tool to use.
If using a Microsoft operating system such as DOS or Windows, a couple of text editors are readily available with the system. In DOS, there is the old Edit text editing program, which may be invoked by typing edit at the command prompt. In Windows (3.x/95/98/NT/Me/2k/XP), the Notepad text editor is your stock choice. Many other text editing programs are available, and some are even free. I happen to use a free text editor called Vim, and run it under both Windows 95 and Linux operating systems. It matters little which editor you use, so don’t worry if the screenshots in this section don’t look like yours; the important information here is what you type, not which editor you happen to use.
To describe this simple, two-component circuit to SPICE, I will begin by invoking my text editor program and typing in a “title” line for the circuit:
We can describe the battery to the computer by typing in a line of text starting with the letter “v” (for “Voltage source”), identifying which wire each terminal of the battery connects to (the node numbers), and the battery’s voltage, like this:
This line of text tells SPICE that we have a voltage source connected between nodes 1 and 0, direct current (DC), 10 volts. That’s all the computer needs to know regarding the battery. Now we turn to the resistor: SPICE requires that resistors be described with a letter “r,” the numbers of the two nodes (connection points), and the resistance in ohms. Since this is a computer simulation, there is no need to specify a power rating for the resistor. That’s one nice thing about “virtual” components: they can’t be harmed by excessive voltages or currents!
Now, SPICE will know there is a resistor connected between nodes 1 and 0 with a value of 5 Ω. This very brief line of text tells the computer we have a resistor (“r“) connected between the same two nodes as the battery (1 and 0), with a resistance value of 5 Ω.
If we add an .end statement to this collection of SPICE commands to indicate the end of the circuit description, we will have all the information SPICE needs, collected in one file and ready for processing. This circuit description, comprised of lines of text in a computer file, is technically known as a netlist, or deck:
Once we have finished typing all the necessary SPICE commands, we need to “save” them to a file on the computer’s hard disk so that SPICE has something to refere ce to when invoked. Since this is my first SPICE netlist, I’ll save it under the filename “circuit1.cir” (the actual name being arbitrary). You may elect to name your first SPICE netlist something completely different, just as long as you don’t violate any filename rules for your operating system, such as using no more than 8+3 characters (eight characters in the name, and three characters in the extension: 12345678.123) in DOS.
To invoke SPICE (tell it to process the contents of the circuit1.cir netlist file), we have to exit from the text editor and access a command prompt (the “DOS prompt” for Microsoft users) where we can enter text commands for the computer’s operating system to obey. This “primitive” way of invoking a program may seem archaic to computer users accustomed to a “point-and-click” graphical environment, but it is a very powerful and flexible way of doing things. Remember, what you’re doing here by using SPICE is a simple form of computer programming, and the more comfortable you become in giving the computer text-form commands to follow — as opposed to simply clicking on icon images using a mouse — the more mastery you will have over your computer.
Once at a command prompt, type in this command, followed by an [Enter] keystroke (this example uses the filename circuit1.cir; if you have chosen a different filename for your netlist file, substitute it):
spice < circuit1.cir
Here is how this looks on my computer (running the Linux operating system), just before I press the [Enter] key:
As soon as you press the [Enter] key to issue this command, text from SPICE’s output should scroll by on the computer screen. Here is a screenshot showing what SPICE outputs on my computer (I’ve lengthened the “terminal” window to show you the full text. With a normal-size terminal, the text easily exceeds one page length):
SPICE begins with a reiteration of the netlist, complete with title line and .end statement. About halfway through the simulation it displays the voltage at all nodes with reference to node 0. In this example, we only have one node other than node 0, so it displays the voltage there: 10.0000 volts. Then it displays the current through each voltage source. Since we only have one voltage source in the entire circuit, it only displays the current through that one. In this case, the source current is 2 amps. Due to a quirk in the way SPICE analyzes current, the value of 2 amps is output as a negative (-) 2 amps.
The last line of text in the computer’s analysis report is “total power dissipation,” which in this case is given as “2.00E+01” watts: 2.00 x 101, or 20 watts. SPICE outputs most figures in scientific notation rather than normal (fixed-point) notation. While this may seem to be more confusing at first, it is actually less confusing when very large or very small numbers are involved. The details of scientific notation will be covered in the next chapter of this book.
One of the benefits of using a “primitive” text-based program such as SPICE is that the text files dealt with are extremely small compared to other file formats, especially graphical formats used in other circuit simulation software. Also, the fact that SPICE’s output is plain text means you can direct SPICE’s output to another text file where it may be further manipulated. To do this, we re-issue a command to the computer’s operating system to invoke SPICE, this time redirecting the output to a file I’ll call “output.txt“:
SPICE will run “silently” this time, without the stream of text output to the computer screen as before. A new file, output1.txt, will be created, which you may open and change using a text editor or word processor. For this illustration, I’ll use the same text editor (Vim) to open this file: