Simulating Circuits – More Examples!


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:

  1. You draw you circuits in gschem.
  2. Generate the netlist by running: gnetlist -g spice-sdb -o <output_netlist.net>  <input_schematic.sch>
  3. Always check the netlist once yourself. Also see which are the input and output nodes.
  4. Simulate the circuit using: ngspice -b -r <raw_file.raw> -o <log_file.log> <netlist.net>
  5. 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:

Component values:

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:

bjt-amp

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! :)

About these ads

28 Responses to Simulating Circuits – More Examples!

  1. Jagdish says:

    Today analog, tomorrow what ??

  2. suraj says:

    cmon… :-)

    hey the whole cs depy has fed..if we can install fel el pkg then we can have a hands on tutorial… :-)

  3. suraj says:

    sorry cs dept lab

  4. Ashwith says:

    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.

  5. Jeff Shemmans says:

    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

  6. Dave McGuire says:

    Excellent examples, Ashwith. Thank you for your efforts!

    -Dave

  7. mariusg says:

    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.

    • Ashwith says:

      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.

  8. Anoop S says:

    @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.

    • Ashwith says:

      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.

  9. Anoop S says:

    ok fine….thnk uuu

  10. Keith Ostertag says:

    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

  11. Keith Ostertag says:

    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)”

  12. Pruthwish says:

    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….

  13. Pruthwish says:

    Hi Ashwith,
    I am having my project which has mosfet as its basic component. I took mosfet model from the site recommended by you on to someone’http://www.mosis.com/files/test_data/t14y_tsmc_025_level3.txt’. Somehow model is not being accepted and it assumes default model. Can you help me out with the same as its very important for me. Analog circuits of BJT having model 2N3904 are running properly. Can you please help me with this problem. This is what I get in terminal.
    “pruthwishpatel@ubuntu:~$ gnetlist -g spice-sdb -o nmoslevel3.net nmoslevel3.sch
    Command line passed = gnetlist -g spice-sdb -o nmoslevel3.net nmoslevel3.sch
    Loading schematic [/home/pruthwishpatel/nmoslevel3.sch]
    Using SPICE backend by SDB — Version of 4.28.2007
    schematic-type = normal schematic
    pruthwishpatel@ubuntu:~$ ngspice
    ******
    ** ngspice-24 : Circuit level simulation program
    ** The U. C. Berkeley CAD Group
    ** Copyright 1985-1994, Regents of the University of California.
    ** Please get your ngspice manual from http://ngspice.sourceforge.net/docs.html
    ** Please file your bug-reports at http://ngspice.sourceforge.net/bugrep.html
    ** Creation Date: Mon Sep 3 02:44:29 UTC 2012
    ******
    ngspice 2 -> source nmoslevel3.net

    Circuit: * gnetlist -g spice-sdb -o nmoslevel3.net nmoslevel3.sch

    Error on line 27 : mq2 1 2 0 chintoo
    Unable to find definition of model – default assumed
    ngspice 2 ->

  14. Pruthwish says:

    This is my netlist for the above problem.
    * gnetlist -g spice-sdb -o nmoslevel3.net nmoslevel3.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 /home/pruthwishpatel/chintoo.mod vvvvvvvv
    *
    * DATE: Jun 11/01
    * LOT: T14Y WAF: 03
    * DIE: N_Area_Fring DEV: N3740/10
    * Temp= 27
    .MODEL NMOS ( LEVEL = 3
    + TOX = 5.7E-9 NSUB = 1E17 GAMMA = 0.4317311
    + PHI = 0.7 VTO = 0.4238252 DELTA = 0
    + UO = 425.6466519 ETA = 0 THETA = 0.1754054
    + KP = 2.501048E-4 VMAX = 8.287851E4 KAPPA = 0.1686779
    + RSH = 4.062439E-3 NFS = 1E12 TPG = 1
    + XJ = 3E-7 LD = 3.162278E-11 WD = 1.232881E-8
    + CGDO = 6.2E-10 CGSO = 6.2E-10 CGBO = 1E-10
    + CJ = 1.81211E-3 PB = 0.5 MJ = 0.3282553
    + CJSW = 5.341337E-10 MJSW = 0.5 )
    *^^^^^^^^ End of included SPICE model from /home/pruthwishpatel/chintoo.mod ^^^^^^^^
    *
    *============== Begin SPICE netlist of main design ============
    MQ2 1 2 0 chintoo
    .MODEL chintoo NMOS (chintoo)
    C2 0 1 100uF
    .options TEMP=25
    V2 2 0 1 sin(0 1m 1kHz)
    R2 1 +3.3V
    .end

    • Ashwith says:

      The problem is in the way you used model. See if this works for you:

      * gnetlist -g spice-sdb -o nmoslevel3.net nmoslevel3.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 /home/pruthwishpatel/chintoo.mod vvvvvvvv
      *
      * DATE: Jun 11/01
      * LOT: T14Y WAF: 03
      * DIE: N_Area_Fring DEV: N3740/10
      * Temp= 27
      .MODEL chintoo NMOS ( LEVEL = 3
      + TOX = 5.7E-9 NSUB = 1E17 GAMMA = 0.4317311
      + PHI = 0.7 VTO = 0.4238252 DELTA = 0
      + UO = 425.6466519 ETA = 0 THETA = 0.1754054
      + KP = 2.501048E-4 VMAX = 8.287851E4 KAPPA = 0.1686779
      + RSH = 4.062439E-3 NFS = 1E12 TPG = 1
      + XJ = 3E-7 LD = 3.162278E-11 WD = 1.232881E-8
      + CGDO = 6.2E-10 CGSO = 6.2E-10 CGBO = 1E-10
      + CJ = 1.81211E-3 PB = 0.5 MJ = 0.3282553
      + CJSW = 5.341337E-10 MJSW = 0.5 )
      *^^^^^^^^ End of included SPICE model from /home/pruthwishpatel/chintoo.mod ^^^^^^^^
      *
      *============== Begin SPICE netlist of main design ============
      MQ2 1 2 0 0 chintoo
      C2 0 1 100uF
      .options TEMP=25
      V2 2 0 1 sin(0 1m 1kHz)
      R2 1 +3.3V
      .end

      Here are the problems I fixed:

      1. The .MODEL part should be followed by a name. You’ll need to edit your .mod file for that. I fixed it in the netlist.
      2. The MOSFET has a minimum of 4 terminals (not 3). These are gate, drain, source and bulk. I guess you didn’t connect the bulk in your schematic. Usually the bulk and source are shorted.

      Did you write the netlist yourself or did you first use gschem and then gnetlist? If you used gschem, I wonder why you got a netlist like that. If you could share that file (upload the .sch file and share the link), I can take a look.

      Sorry for the really late reply. I’ve been really busy (I still am).

  15. Pruthwish says:

    I am sorry for late reply, was a bit occupied. Can I send it to you by mail?

  16. Pingback: Обзорный доклад о состоянии инженерного ПО в Linux(Install Fest 2013) | Crafting.be

  17. kavyamanohar says:

    Hi ashwith,

    You have made my day. Started experimenting spice and gEDA on my own. Thanks a lot.

    Kavya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 74 other followers

%d bloggers like this: