Circuit Simulation in GNU/Linux – Lets begin!


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.

Prerequisites

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.

References

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.

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.

Update:

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

Schematic Capture

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

About these ads

52 Responses to Circuit Simulation in GNU/Linux – Lets begin!

  1. manojkumars says:

    Is this similar to multisim???

  2. manojkumars says:

    Hey why dont u make sure your tutorials are also available in PDF format!!!

    • Ashwith says:

      Because a blog can be seen and shared more easily. If people discover it they just need to visit it. If they get the PDF through mail from someone, they’ll not know when knew tutorials are posted because the person who e-mailed may not do so again. Secondly, PDFs take memory. I have limit to how much I can store online.

  3. george says:

    sir i am new to ngspice….I am not interested to use schematic….i am all characteristics through gedit and saving it as .spice file…..can u show me more examples or launching it through ngspice…

  4. srinivas says:

    Hi Sir,

    I want to use ngspice through windows XP OS. Can you please guide me for useful things?
    Thanks in advance.

    Regards,
    Srinu

    • Ashwith says:

      Hi Srinivas,

      There is a binary available here http://sourceforge.net/projects/ngspice-bin/. However, it won’t be of much use if you need a schematic editor. This is just the simulator program. However, if you have the netlist and need just a simulator, then I think it should be fine.

      Also you don’t have to refer to me (or anyone on the internet) as sir. Hope this helps :)

      Regards
      Ashwith

      • srinivas says:

        Hi,
        Thank you very much for your suggestion. Frankly speaking my knowledge in computers very less. I gone through all the information on ngspice. I thought it might be more useful if i learn Linux os. I installed Ubuntu right now. in future for further assistance i will contact u. Thank you very much

      • Ashwith says:

        I would suggested using Fedora’s electronic spin instead of ubuntu. It’s available here:

        http://spins.fedoraproject.org/fel/

        This contains all the EDA software plus gives you the latest updates. Plus it is maintained by people who work in the electronics and semiconductor industry.

  5. idom says:

    Hi Ashwith,

    Thanks for the great post. It is very useful. I was able to get started on basic ckt simulation within 10 min of reading your tutorial. Installation was smooth over 12.04 LTS ubuntu via ubuntu software centre. And after that your instrucations were exact.

    However now I want to try ADMS with ngspice for verilog AMS simulator.
    http://ngspice.sourceforge.net/adms.html And I am not able to install it.
    I filed a thread in ubuntu forums and wanted to know if you have tried or you know any solution for the same. I also mailed the developer of ADMS.

    http://ubuntuforums.org/showthread.php?t=2011473

    regards,
    idom

    • Ashwith says:

      Hi idom,

      No I haven’t tired ADMS (heard about it for the first time from you). It looks interesting so I will give it a shot this weekend and see if I can install it. However, I use Fedora on my computer so I won’t be able to check this on Ubuntu. I’m hoping it shouldn’t make much of a difference.

      • idom says:

        Yeah sounds good !! I am assuming that you are based out of Banglaore. Even I am based out of bangalore. Please send me your email id so that if require we can take conversation off-line from this blog.

    • Ashwith says:

      Hey Idom,

      I had to use the following instructions to install it. It was pretty straightforward:

      ./configure
      make
      sudo make install

      I saw that you were able to go through this step but couldn’t use admsXml. The error says that the program cannot find libadmsElement.so.0. The file should be located in /usr/local/lib. Can you check this and let me know?

      • idom says:

        Hey Ashwith,

        Thanks for trying out.

        sorry however now
        admsXml -v command is showing output :)
        modi@ub:~$ pwd
        /home/modi
        modi@ub:~$ admsXml -v
        [usage..]
        modi@ub:~$

        Probably a reboot was required.

        I am going to explore ADMS now and see how is it ?

        thanks for your help.

        idom

  6. idom says:

    Hi Ashwith

    one slightly non related question. I am looking for across Process ( strong/weak or fast/slow ) models for some industry standard technology. It need not be very high end. I will be ok with 0.18u or 0.35u technology. Do you have any idea where i can find them ? Mosis does not seem to have fast/slow models they only have models for one nmos one pmos for different process flavors.

    http://www.mosis.com/pages/Technical/Testdata/index

    idom

  7. kimmi aggarwal says:

    hey ashwith
    i am currently working with window 7(64bit)… i am having a live DVD of Fedora Electronic Lab 17…as a beginner with this project i am feeling uncomfortable with the live DVD ….want to install this OS on my laptop along with win7 without loosing any preloaded data on my hard disk….i am currently having 170GB of free memory on C:/ drive…would it be enough for installing this OS….

    can you plz guide me for the installation…i shall be very thankful to you for this….

    • Ashwith says:

      Hi kimmi,

      Unfortunately, I have never tried to install Fedora with your kind of configuration. I have a separate hard disk for Fedora and another one for Windows 7.

      Normally, you’ll need to create a new partition on your computer and then install Fedora on that.

      Alternatively you could use virtualbox and install Fedora on a virtual machine.

      • kimmi says:

        can i repartition the c: / drive without any data loss?

      • Ashwith says:

        Yes it’s possible. Right click on My Computer. Select Manage.

        On the left hand side, select Disk Management. Right click on C: drive and select shrink. You can then create a new partition by setting the size in the dialog box which appears.

  8. kimmi says:

    OK Thanku ashwith…i will try and will contact you for any further assistance

  9. kimmi says:

    one thing more i want to know is that can i use different technology files for the mosfet like tech. file for 0.13um, 45nm, etc…i had not found any provision for that in fedora yet….as i am working with low power CMOS vlsi design…

  10. kimmi says:

    okay

  11. kimmi says:

    i am having some BSIM model files for 180nm,130nm,45nm 22nm etc..can mail you if required in the evening….

    • idom says:

      Can you please mail it to modi[dot]samarth[at]gmail[dot]com ?

      • kimmi says:

        the tech. files i sent….was that working for you?

      • idom says:

        I haven’t got time to check it out. They anyway did not look like across PVT corner.
        I doubt if I will get any time in near future also to check as I am held up in something else.
        idom

  12. kimmi says:

    i am not having that right now….can mail u within 8 hours….

  13. Uwe Dippel says:

    Thanks for the nice tutorial! – I just happen to find it while searching for a simulation tutorial (exactly what KiCAD is missing :( ).

    Please, be aware that it is not quite up-to-date any longer, though. Some commands have changed slightly, and the line
    (load (string-append geda-data-path “/scheme/auto-uref.scm”)) ; load the autonumbering script
    comes back with an error ‘unbound variable’.
    I could get the autonumbering through a menu option. which will number everything starting from 1 instead of 2 in your tutorial.

    In case someone hits the same snags.

    • Ashwith says:

      I’m happy you liked my tutorial! :-)

      I’m not sure I understood what problem you are facing. My gschemrc still works. Is this a newer version of gschem?

      • Uwe Dippel says:

        I don’t know. I only know that the elements were not numbered, contrary to what the script was supposed to do, automatically.
        This is Ubuntu 12.04, and gschem 1.6.2-3

      • Pavan pandey says:

        sir how can we show the current value without using GNU-cap model only through ngspice plz explain

      • Ashwith says:

        Connect a voltage source in series with the component you’re interested in and set the value of the voltage source to zero.

        Ngspice calculated the current leaving the positive terminal of a voltage source. So the only way to get the current through a component (say a diode) is to put a voltage source in series with it. But you don’t want this voltage source to affect your circuit. Therefore you set the value of this “dummy” voltage source to zero.

  14. Mandeep says:

    Hi Ashwith,
    I am beginner in Mixed signal circuit design world and found that Gspice and gschem are the best free tools available to do some projects by myself. I am trying to use Nmos and Pmos from ASIC componets in gschem add component window, do you know what voltages are they made to operate on? I have a project which has vdd specification of 1.8v, so can you point me to the active devices i can use? this would a great help.

    thanks,
    Mandeep

    • Ashwith says:

      I’m not sure what devices you can use (I have almost no experience in mixed signal design. I wrote these tutorials to help a few of my friends who had problems using it). What I do know is that you need to download SPICE models from a manufacturer (Fairchild & TI/National Semiconductors have SPICE models on their website). These will be discrete components so they may not help for ASIC design. What I would suggest is looking at http://www.mosis.com. They provide technology files for free. This should help you get the SPICE models you need.

      • idom says:

        Mandeep,
        Typically model files will have maximum ratings. 0.18u in TSMC will definitely have 1.8V transisitors.

        if you copy paste you model here I can probably look it up and see what is its voltage ratings.
        A good simulator(along with a model) will also give warnings that you have exceeded its rated voltage.

        idom

  15. Anto K Davis says:

    Hi,
    Have u used QUCS?

  16. Vivek says:

    hi,

    Your tutorial is great!

    A much better diode spice model is available here http://www.uncg.edu/phy/hellen/spice_stuff.html

    Do check it out.

  17. Keith Ostertag says:

    My browser somehow doesn’t show a link to your gEDA-examples-1.0.zip in any sidebar that I can see. Could you please clarify or post a link in a comment? Thanks.

  18. Pingback: Debian GNU/Linux Wheezy packages | Freedom, World peace in unity!

  19. This was tremendously helpful in getting started. Thanks for posting.
    Peter

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: