Circuit Simulation in GNU/Linux – Lets begin!
September 10, 2010 57 Comments
Welcome back! :)
As promised, here is the first of my tutorials on EDA in GNU/Linux. In this post we will start with SPICE simulations. Please remember that I’m a total beginner to these tools as well so I could be wrong at places (post a comment and I’d be happy to correct) and that my examples will remain very simple.
For this tutorial you’ll need these tools installed.
- gEDA gschem – A schematic capture program.
- gnetlist – a program that converts your schematic to a netlist.
- ngspice and gnucap – circuit simulators.
- gspiceUI – A graphical circuit simulator.
- gwave – a waveform viewer.
This tutorial assumes that you have taken up a course on basic electronic circuits. It would help if you know how to write a spice netlist but its not a necessity.
I wouldn’t be able to write this on my own without the right documentation. So here is the stuff I referred to learn myself. If you have already used similar software before, you could just jump to the documentation directly and skip the tutorial.
- gschem Warmup for Beginners by Bill Willson
- gschem to pcb Tutorial by Bill Wilson
- Circuit simulation using gEDA and SPICE — HOWTO by Stuart Brorson
- ngspice and gschem
- ngspice manual
- gnucap manual
Before We Begin
I use a small setup I found on the gschem FAQ which lets gschem automatically assign reference designators to components. We’ll see what reference designators, or refdes, are later. Here’s what you do: First open your favorite text editor and copy the following text in it:
(load (string-append geda-data-path "/scheme/auto-uref.scm")) ; load the autonumbering script (add-hook! add-component-hook auto-uref) ; autonumber when adding a component (add-hook! copy-component-hook auto-uref) ; autonumber when copying a component
Now save this file to ~/.gEDA/ with the filename gschemrc. If this file already exists, then just copy and paste the above lines to that file itself.
Before you begin with any project, you have to decide on a directory structure. Here’s how we’ll organize our work for this tutorial. Create a directory and name it projects. In this create another directory called models. We will store all our SPICE models here. We’ll see what SPICE models are later.
All examples in the SPICE tutorials are available for download. Check the sidebar on the right. There is a file called gEDA-examples-1.0.zip.
Example – 1 Diode V-I Characteristics
We all know this one. Its just a diode and resistance in series connected to a voltage source:
Ignore the two boxes (SPICE model and SPICE directive for now).
For this example, create a directory called diode-vi inside the projects directory. We’ll save our schematic here.
Here’s what you do. First open gschem. You’ll have an empty schematic window opened for you. Right-click and select Add Component. Next go to the libraries tab. From the Basic Devices section, select resistor-1.sym. Click on the drawing area to place it. Next select voltage-3.sym and click on the drawing area to place it. From the Diodes section, select diode-1.sym and place in the drawing area.. Finally, from the Power rails section, select and place gnd-1.sym. You can use the Filter box to make your search easier. With the components placed, move the components to where you want them and rotate them accordingly. To move a component, first click it then click and drag (the simple click and drag doesn’t work here). To rotate, select the component, go to the Edit menu and select Rotate mode and then click on the component. Use the keyboard short cuts (look at the menu items) to speed things up. You should now have this on your drawing area:
Now we need to connect each of the components. Do do this, press n on your keyboard to go to the Add Net mode. Click on a terminal of a component, say the positive terminal of the voltage source and then click on the terminal of the component you want to connect to. Next right-click to finish that connection. Again click on another component’s terminal and connect it accordingly. Your schematic should now look something like this:
Next we need to give values to the resistor and the voltage source. We also need to define the behavior of the diode. For the first two, we need to look at two main properties:
- refdes: A reference designator. This is a unique name that tells the simulation program what kind of component we have placed. A refdes starting with R tells the simulator that the component is a resistor while a refdes starting with V tells the simulator that it is a voltage source. Similarly Capacitors are represented by C and Diodes by D. You can see the entire list on any reference on SPICE like the ngspice manual (check the prerequisites section).
- value: this tells the simulator the resistance of the resistor, the output voltage of the voltage source etc. It is a number which can be optionally followed by a multiplier character (like K for kilo, M for mega).
Here’s how you do this. Double click the resistor. Next, in the Name drop-down menu, select value. In the value box type 100 (implying 100 ohms). Click Add. In the Visible drop-down box select how you want to see this value. I use the Show Value only option. Next (do not click close) click the voltage source and set value to 5 (i.e. 5V) in the same way. Finally select the diode. This time select model-name from the Name menu and set its value to 1N4007. Notice that the refdes is already set – R2 for the resistor, V2 for the voltage source and D2 for the diode. This is because of the initial setup I mentioned. If you didn’t use those instructions, you’ll have to set the refdes yourself. Make sure you follow the rules for refdes – the starting letter is important (refer the ngspice manual). You should now have a schematic that looks like this:
We’re almost done with the schematic capture (i.e. defining the circuit). We need to now define the diodes characteristics. For this, right-click, select Add Component and go to the SPICE simulation elements section. In this, select and place spice-model-1.sym and spice-directive-1.sym. Close the Select Component Box.
Double click on SPICE model and set model-name to 1N4007. Its already on the list so to set it, click the value column for model-name for the table, enter 1N4007 and press Enter. Next select file and set it to ../models/1N4007.mod. We’ll see what this file is later.
Next click on SPICE directive. Delete the file attribute on the table. Set value to “.options TEMP=25” (diode-vi.schwithout quotes). Now save the schematic (File -> Save As…) to the diode-vi directory as diode-vi.sch. Our circuit should now look like it did initially. Here is the pic again:
That’s all as far as the schematic is concerned. Next we move on to simulation. But before that don’t forget that we have to define the Diode’s model. Create a text file and paste the following in it:
.MODEL 1N4007 D(IS=76.9p RS=42.0m BV=1.00k IBV=5.00u CJO=26.5p M=0.333 N=1.45 TT=4.32u)
Save this as 1N4007.mod in the models directory we created in the beginning. You’ll have to search models online. You should be able to get it from the manufacturer’s website. I got this one from http://www.diodes.com
Getting the Netlist
Before we move on to actually simulating this circuit, lets see how we can create a netlist from this schematic as anyone who has used SPICE before would be interested in the netlist. Here is what you need to do. Open the terminal and go to the directory where the schematic was stored i.e. diode-vi. Now run the following command:
gnetlist -g spice-sdb -o diode-vi.net diode-vi.sch
You’re netlist will be created with the filename diode-vi.net. Open it in a text editor. This is what you will see:
* gnetlist -g spice-sdb -o diode-vi.net diode-vi.sch ********************************************************* * Spice file generated by gnetlist * * spice-sdb version 4.28.2007 by SDB -- * * provides advanced spice netlisting capability. * * Documentation at http://www.brorson.com/gEDA/SPICE/ * ********************************************************* *vvvvvvvv Included SPICE model from ../models/1N4007.mod vvvvvvvv .MODEL 1N4007 D(IS=76.9p RS=42.0m BV=1.00k IBV=5.00u CJO=26.5p M=0.333 N=1.45 TT=4.32u) *^^^^^^^^ End of included SPICE model from ../models/1N4007.mod ^^^^^^^^ * *============== Begin SPICE netlist of main design ============ .options TEMP=25 V2 2 0 5 R2 2 1 100 D2 1 0 1N4007 .end
Getting the Results (DC Analysis) – Simulation
Alright. Its finally simulation time! Open gSpiceUI. Go to the File menu and click Import. Select the diode-vi.sch, the schematic we just created. Go to the Settings menu and select GNU-Cap as the simulator. We want to obtain the V-I characteristics of the diode so select the DC tab (DC-Analysis). In the Signal Source Section, select V2 (or the refdes of your voltage source). Set the Start Voltage to 0V, Stop Voltage to 5V and Step Increment to 10mV. In the parameters section select Current and de-select Voltage. Now in the Components list, select R2 (the refdes of the resistor) by clicking it. Finally, on the Simulation menu click Run and wait for the message “Simulation ran successfully” on the status bar. Now go to the Simulate menu again and select View Results. gwave will now open. Click and drag the button named as I(R2) to the waveform viewer (the black area). You should now see the VI characteristics of the diode. Right click on the lower black area and select Delete this panel. Here is a screenshot:
Pretty simple isn’t it? :)
Now I would suggest that you play around a little bit and get used to the keyboard shortcuts. You could also check out the links I put at the beginning. These describe all this and tons more in great detail. In my next tutorial we will perform a transient analysis on another simple diode circuit – A half wave rectifier. Before that, I will also show you how to simulate this tutorial’s circuit using the command line interface. Hope you liked this tutorial, and more importantly, I hope it helped. Stay tuned for the next one. Thanks for reading! :)