Simulating Circuits – More Examples!
September 21, 2010 30 Comments
Hope you liked my previous tutorials on SPICE simulation in Fedora Electronic Lab. Well this post comes a little late because I had a lot of circuits in mind. So take a deep breadth and get ready for Part 3! :)
I’m assuming that you’ve read through the previous two tutorials and that you are now comfortable with using gschem. After this tutorial, you must go through the ngspice manual atleast once. I’ll be moving a bit fast from now on because there is a lot to cover. If something is not clear, feel free to post a comment. I also assume that you’ve completed a course on analog electronic circuits. If you are currently in such a course, go through the circuits you are familiar with and come back later.
Remember to plan a directory structure before each example. Also, the models for the transistors are available from Fairchild Semiconductors and diodes from Diodes.com. I’m not posting these here due to copyright restrictions.
A quick recap:
- You draw you circuits in gschem.
- Generate the netlist by running: gnetlist -g spice-sdb -o <output_netlist.net> <input_schematic.sch>
- Always check the netlist once yourself. Also see which are the input and output nodes.
- Simulate the circuit using: ngspice -b -r <raw_file.raw> -o <log_file.log> <netlist.net>
- Finally open the waveform viewer by running gwave <raw_file.raw> and dragging the output you want to see to the panel.
Example 1 – Diode Half Wave Rectifier
A very simple circuit. Here is the schematic:
V2: value = SIN(0 5 60)
C2: value = 10u
R2: value = 10k
A2: model-name = 1N4007
file = ../models/1N4007.mod
A6: file = ./hwr.cmd
hwr.cmd: .TRAN 1m 100m
Note that hwr.cmd is the filename (you can use any name you like). Don’t use this name inside the file. All you need to enter is .TRAN 1m 100m.
This is a transient response. The first number is the step size (in seconds) and the second is the stop time. Check the manual for more info.
Get the netlist and simulate. Open the netlist and see which nodes are the input and output nodes. Run gwave and plot these nodes. This is what you should get:
Simple as always. Lets move on. From now on I will tell you the circuit attributes only for components we haven’t seen yet. You can figure out the rest easily by seeing the schematic. I’m sure you can keep up :)
Example 2 – Diode Bridge Rectifier
The circuit doesn’t contain a capacitive filter however. Use the same .cmd file as the halfwave rectifier. Generate the netlist and run the simulation. Now run gwave. This time plot only the ouput node. The input is a difference of two nodes and this is not available on the list. (If some one knows how to get this in batch mode please post a comment). This is the output:
Increase the step size if you like. This does seem distorted a bit.
Enough diodes! Lets move on to transistors (BJTs) for the rest of the tutorial! :)
Example 3 – BJT Amplifier – AC Analysis
We’ll look at a simple BJT (bi-polar junction transistor) amplifier now. This time we’ll see the AC response, i.e. the output of the amplifier over a range of input frequencies (sinusoidal input). Here is the circuit:
We have a transistor this time. Choose npn-3.sym from basic devices for the transistor. Give it the following attribute:
Q2: model-name = 2N3904
The model is available from Fairchild. Also, our input voltage source is a little different.
V2: value = AC SIN(0 1m 10k 0 0)
We need to add the word AC before it to tell ngspice that we will be using this source for the AC analysis. The rest of the circuit is straightforward. Use the following line in your cmd file:
bjt-amp.cmd = .AC DEC 10 1 1Meg
This tells ngspice that we want to perform an AC analysis, we need a decade frequency variation from 1Hz to 1MHz with 10 points per decade. Again, read the manual for more info. Generate the netlist, run the simulation and plot the voltage variation at the ouput node i.e. at R10. In gwave, go to Options > X Axis Scale > Log. This is what the output should look like:
So far so good? Lets now look at two oscillators.
Example 4 – Transistor Phase Shift Oscillator
You know this one. Simplest oscillator to construct. Lets look at the circuit:
Nothing really new in this circuit. I’ve used a different transistor this time. The model for this is also available at Fairchild’s website. Once you’re done constructing the circuit, use the following cmd file:
bjt-phase-shift-osc.cmd: .TRAN 100u 10m 8m
The third number is the start time. The oscillator needs time to stabilize so we don’t start at 0s.
You know what to do, get the netlist then simulate and run gwave. Plot the output i.e. the voltage at the collector of the transistor. Hope you got this one right too.
Getting the hang of it? Time for inductors and mutual inductance. :)
Example 5 – The Colpitts Oscillator
Two new things to learn here. Lets check out the schematic first. Draw the circuit first and give values to the components you are familiar (i.e. everything except the inductors).
Okay. Inductors are pretty straightforward. You’ll find inductors in the same section as the resistors, capacitors and transistors. Just set an inductor’s value attribute:
L2: value = 10m
L4: value = 1m
L6: value = 1m
Now for the mutual inductance. You’ll find this one in the SPICE Simulation Elements section. Select and place kmutual-1.sym (where you place it doesn’t matter) on the schematic. Double click it. You’ll see the attributes you need on the table already. Here are the settings:
K2: value = 1
inductors = L6 L4
The value attribute sets the coefficient of coupling. The next, clearly lets you select the inductors. Now that the circuit is done, make the cmd file:
bjt-colpitts-osc.cmd: .TRAN 1u 21m 20m
As always, generate the netlist, run the simulations and start gwave. Plot the output at R8 and no red waveforms this time! :-|
Too much for ya? Lets end with one last very simple circuit. :)
Example 6 – The Transistor Switch
After all those circuits we’ve gone through, this should take you no time. Lets go over it quickly.
Lets go back to DC analysis:
bjt-switch.cmd: .DC V4 0 10 0.1
Get the netlist, simulate and plot the input and output voltages and we’re done with this tutorial!
Whew! That was a long one! Well I’m sure that after this, you should be a lot more comfortable with SPICE in FEL. Go over this again slowly. Next, we will look at sub circuits (SUBCKTS) which allow us to use op-amps, or even break our circuits into more fundamental modules. Till then try some other circuits on your own and do post a comment if you have any questions. Finally, any feedback is appreciated. Hope you liked this tutorial and I hope this helps. Thanks for reading! :)