Simulating Circuits – More Examples!
September 21, 2010 22 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.
Ready? Go!
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
D2: model-name=1N4007
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
An improvement over the halfwave circuit. Here you go:
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!












Today analog, tomorrow what ??
Patience is a virtue. No revealing surprises
cmon…
hey the whole cs depy has fed..if we can install fel el pkg then we can have a hands on tutorial…
sorry cs dept lab
Well I’ll be glad to
. But if you’re planning to have this for sure keep it next year during your even sem so that the second years can understand too.
Hi Ashwith,
I really appreciate the examples that you made for the readers. I really love linux, but simulating on it was a nightmare and i did not understand how to go forward. Your tutorials helps alot!!!
Thanks
Jeff
Hi Jeff!
Glad you liked them
. Thanks for commenting
Cheers!
Ashwith
Excellent examples, Ashwith. Thank you for your efforts!
-Dave
Glad you liked them! Thanks
Hi Ashwith,
Excellent (series of) tutorial.
Wonder if we can download the examples just to be able to speed-up a bit the learning curve.
Hi Marius!
I’m glad you liked them
. The examples are available for download. Check the box.net flash widget on the right sidebar. There is file called gEDA-examples-1.0.zip which contains all the schematics and other related files. I had mentioned this only in the last tutorial. I guess I’ll add a line to the first tutorial as well.
@Aswith
Great work bro.
Why dont you include some circuits of MOSFET like a common source,common drain,common gate amplifiers as technology is now running on MOSFET.
Unfortunately I don’t have the time. Otherwise I would have a lot of new posts by now. I’ll be back to posting frequently only after Feb next year.
Anyway, the purpose of these tutorials was to show how to use these tools. Not explain the circuits. I think it should be quite easy to do a MOSFET circuit with the information given here.
ok fine….thnk uuu
Hi Ashwith- I am learning a lot through your tutorials, thanks very much.
You may have a typo in example #3 BJT Amplifier AC Analysis – I believe the “model-name” in the A2 Spice model statement should be 2n3904, not PN2222A. This will confuse some people.
That’s as far as I have gotten, so far.
Keith Ostertag
Also, in example #3 BJT Amplifier AC Analysis, the schematics shows “AC SIN(0 1m 10k 0 0)” but a few lines down you have “V2: value = AC SIN(1 1m 10k 0 0)”
Hi Keith,
Thanks for pointing this out! I don’t know how I missed that
I’ve fixed both errors now.
Hi Ashwith,
Can you help me with mosfet analog circuits, when I try to simulate then ‘ no model found , default BJT assumed’ is what I get, can you please help me out with the same….
Can you give me your schematic? Post them on a file hosting site (maybe Dropbox or Google Drive) and give me the link.
https://www.dropbox.com/s/6p2z0flpziejga7/test.sch
Did not know about dropbox, Please find the link below. Thanks….
I am sorry for late reply, was a bit occupied. Can I send it to you by mail?