Getting Started with Simulation Using TinyCad

JD Linsley

This document and the accompanying materials Copyright © 2010 - 2011 JD Linsley Bsc MIET C.Eng*.

Having said that please feel free to copy this document in its entirety, together with the accompanying information, and distribute them as you see fit provided you keep the copyright notice in place.

*All the letters look a bit stuffy but I worked hard for them and pay for the privilege of being allowed to use the last two sets so it would be silly to omit them.

1 Introduction 4

2 Prerequisites 4

3 Sources 5

3.1 Voltage Sources 5

3.1.1 DC Voltage (V dc) 5

3.1.2 Exponential voltage (V Exp) 6

3.1.3 Voltage pulse (V Pulse) 8

3.1.4 AC Voltage (V ac) 10

3.2 Current Sources 12

3.2.1 I DC 12

3.2.2 Exponential Pulse (I Exp) 12

3.2.3 Current Pulse (I Pulse) 12

3.2.4 AC Current (I Sin) 12

4 Devices 14

4.1 Resistor (R) 14

4.2 Capacitor (C) 14

4.3 JFET (J) 14

5 Analyses 14

5.1 AC Small Signal Analysis 14

5.2 Transient Analysis 16

5.3 Noise Analysis 17

6 Other SPICE Statements 18

6.1 The .NODESET Statement 18

7 The Spice Trail 19

7.1 Spice Variants 19

7.2.1 A Simple Resistor Model 20

7.2.2 A Diode Model 21

7.2.3 A JFET Model 23

7.2.4 A Multi-part Op-Amp 23

7.2.5 The Noise Analysis 25

7.3 Connecting to NGSPICE 25

7.4 Simulation Problems 25

8 Acknowledgments 25

Preface

This text is aimed mainly at those who have used commercial CAD and simulation packages and now, venturing into the open source world, are finding the lower level of integration of the tools a little bewildering. That is not to say that anything presented here should be beyond a newcomer to the art of simulation. On the other hand, if you are already well versed in a variety of tools, this should help smooth the bumps of transition into a new working environment.

# Introduction

This document describes the simulation symbols I created for TinyCad and how to use them to allow TinyCad to export SPICE netlists, primarily for use with ngspice. The symbols include four voltage and four current sources as well as directives for performing a simulation. The sources are what provide power and input signals to the circuit you are simulating, the analyses define the type of simulation to be performed. I've also included some basic components to get your simulation started. The sources I've provided are:

1. DC Voltage (V dc);

2. Exponential voltage (V Exp);

3. Voltage pulse (V Pulse);

4. AC Voltage (V ac);

5. DC Current (I dc);

6. Exponential current (I Exp);

7. Current pulse (I Pulse); and

8. AC Current (I ac).

The simulation analyses are:

9. AC Analysis (.AC);

10. Noise Analysis (.NOISE); and

11. Transient Analysis (.TRAN).

The simulation components are:

12. Capacitor (C);

13. JFET (J); and

14. Resistor (R).

# Prerequisites

In this document I will assume that you have a copy of TinyCad and know how to use it to draw circuits correctly, including adding and modifying libraries. You also need a SPICE or PSPICE compatible simulator; this document is primarily aimed at ngspice but I know that LTSpice IV works too. Ngspice can be obtained from:

http://sourceforge.net/projects/ngspice/files/

LTSpice IV can be obtained from:

These are both free but ngspice is open source while LTSpice IV is proprietary. Ng spice requires more input from the user to achieve results (but you know what it is you've done in some detail) while LTSpice has a much more user friendly point and click user interface.

It would be really handy if you had a SPICE manual of some sort available too; each of the above programs has a pretty good one.

# Sources

For this basic guide I'm only covering the independent sources. Dependent sources allow you to make a signal control a voltage or current according to some defined relationship. The independent sources are divided into two basic groups, voltage and current sources. I'll describe the voltage sources in some depth, the current sources only briefly because they differ so little from the voltage sources. When you add a source to your circuit it has default values for the commonly used parameters while the more exotic ones are left blank and, therefore, default to zero.

There are at least two other types of source available that I'm not covering here; the PWL (Piece-wise Linear) source and the SFFM (Single Frequency, Frequency Modulated) source. If there is demand I'll add them to the next edition.

## Voltage Sources

Illustration 1: The Four Voltage Sources

These come in two flavours, DC and signal. The DC source is used mainly to provide simulated power to your circuit and also to provide bias voltages where required. The signal sources are what make your simulations useful by providing varying input voltages to allow you to see what is most likely to happen when you actually build the real thing. Used carefully and judiciously it really is possible to design a circuit, simulate it (and correct it), build it and expect it to work correctly first time.

### DC Voltage (V dc)

The symbol for this item looks like the VS1 in Illustration 1. It has very few options, only one in fact, see Illustration 2. The name is V DC. The package is 'spice'. It doesn't really have or need a package, but in all these symbols it is given as 'spice' to make it easy to spot if one has sneaked into the PCB netlist by mistake. Remember these are for simulation only, they have no physical existence. The only variable that matters is the value, here it is simply '5'. It is quite acceptable to make this '5V' or '5v' but it makes no difference to the function. The numerical value can be followed by any of the standard SPICE unit prefixes (a, f, p, n, u, m, k, meg, g, t – either upper or lower case) with or without the 'V' following it.

Illustration 2: Vdc Tool Options in TinyCad

### Exponential voltage (V Exp)

The symbol for this item looks like the VS2 in Illustration 1 and is used to stimulate your circuit with a double exponential pulse – only one! It is much more interesting than Vdc, in that it has several parameters that must be set to suit your purpose. Looking this time at the library window for editing parameter we can see that it has eight numbers to play with, see Illustration 3. To reach this view click on the 'Libraries' button, double click on the 'simulation' library, right click on the 'V Exp' entry and select 'Symbol Properties' from the drop-down list. The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. V1 – the starting voltage for the exponential from time zero until trise-delay is over;

3. V2 – the target voltage for the first exponential (we all know it will never get there);

4. trise-delay – the delay from the start of simulation until the start of the pulse;

5. tau-rise – the time constant for the first exponential (just like RC);

6. tfall-delay – the time from the start of simulation until the start of the second exponential;

7. tau-fall - the time constant for the second exponential; and

8. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

These should be pretty self explanatory although item f, tfall-delay, can cause confusion. If you make this smaller than 'trise-delay', because you thought it was like a pulse width for example, the results are a bit strange and depend on the relationship between the two delays.

The result of the test case provided with this library is shown in Illustration 4, taken from the plot output of Ngspice. Note that the second exponent decays back down towards V1.

Illustration 3: V Exp Parameters – TinyCad Update Library Symbol View

The observant amongst you will have noticed a tantalising glimpse of the other tab in Update Library Symbol window: SPICE. If you must find out about it right now then please feel free to jump to Paragraph , The Spice Trail.

Illustration 4: Example Exponential Pulse with V1 = 1 Plotted from Ngspice

### Voltage pulse (V Pulse)

The symbol for this item looks like the VS3 in Illustration 1 and is used to stimulate your circuit with a trapezoidal pulse train or just a single pulse. This source has nine parameters that must be set to suit your purpose. Looking this time at the library window for editing parameter we can see that it has eight numbers to play with, see Illustration 5. To reach this view click on the 'Libraries' button, double click on the 'simulation' library, right click on the 'V Pulse' entry and select 'Symbol Properties' from the drop-down list. The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. V1 – the starting or offset voltage for the pulse train;

3. V2 – the voltage for the pulses;

4. t-delay – the delay from the start of simulation until the start of the first pulse;

5. t-rise – the time for the rising edges to go from V1 to V2;

6. t-fall – the time for the falling edges to go from V2 to V1;

7. width - the length of time the pulse remains at V2;

8. period – the time between successive rising (or falling) edges; and

9. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

There is nothing mysterious about any of these parameters but there are a couple of useful, if banal, details worth pointing out. V2 can be lower than V1 to make negative pulses. If you only need a single pulse simply make the period longer than your simulation time.

The result of the test case provided with this library is shown in Illustration 6, taken from the plot output of Ngspice.

Illustration 5: V Pulse Parameters – TinyCad Update Library Symbol View

Illustration 6: Example Trapezoidal Pulses with V1 = 1 Plotted from Ngspice

### AC Voltage (V ac)

The symbol for this item looks like the VS4 in Illustration 1 and is used to stimulate your circuit with a sine wave. In fact it does more than that; not shown on VS 4 are the 'exotic' parameter that allow you phase-shift, delay, and decay your sine wave, see Illustration 8. This source has eight parameters that must be set to suit your purpose. Looking this time at the library window for editing parameter we can see that it has eight numbers to play with, see Illustration 7. To reach this view click on the 'Libraries' button, double click on the 'simulation' library, right click on the 'V Pulse' entry and select 'Symbol Properties' from the drop-down list. The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. Offset – the DC offset voltage for the sine-wave;

3. Amplitude – the 'A' in Asinθ for the sine-wave (half the peak-to-peak voltage);

4. Frequency – the frequency in Hertz;

5. t-delay - the delay from the start of simulation until the start of the first cycle;

6. damping factor – the coefficient determining the rate of decay, leave blank for no decay;

7. Phase – the angle, in degrees, where the first cycle will commence, blank for zero; and

8. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

Here, items f and g can be interesting. The equation containing the damping factor can be found in the Ngspice manual at section 4.2.2 and is more detail than I want to include here; the example had a damping factor of 4000s-1. The phase angle causes a sharp discontinuity at the point where the sine wave begins, this might not be quite what you really wanted to simulate.

Illustration 7: V SIN Parameters – TinyCad Update Library Symbol View

Illustration 8: Example Decaying Sinusoid, Plotted from Ngspice

## Current Sources

These follow the exact pattern of the voltage sources and are included here more-or-less as a reminder of their existence. The only major item of note is that conventional current flows into the positive terminal so I tend to think of them as current sinks.

### I DC

The units are in amps and, as for the voltage source, the unit can be present or absent. There is no danger of confusion with the abbreviation for atto; SPICE will not let you define a current that small.

### Exponential Pulse (I Exp)

The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. I1 – the starting current for the exponential from time zero until trise-delay is over;

3. I2 – the target current for the first exponential (we all know it will never get there);

4. trise-delay – the delay from the start of simulation until the start of the pulse;

5. tau-rise – the time constant for the first exponential (just like RC);

6. tfall-delay – the time from the start of simulation until the start of the second exponential;

7. tau-fall - the time constant for the second exponential; and

8. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

### Current Pulse (I Pulse)

The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. I1 – the starting or offset current for the pulse train;

3. I2 – the current for the pulses;

4. t-delay – the delay from the start of simulation until the start of the first pulse;

5. t-rise – the time for the rising edges to go from I1 toI2;

6. t-fall – the time for the falling edges to go from I2 to I1;

7. width - the length of time the pulse remains at I2;

8. period – the time between successive rising (or falling) edges; and

9. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

### AC Current (I Sin)

The parameters are:

1. DC – used for noise and AC small signal analysis, leave it as 0 almost always;

2. Offset – the DC offset current for the sine-wave;

3. Amplitude – the 'A' in Asinθ for the sine-wave (half the peak-to-peak current);

4. Frequency – the frequency in Hertz;

5. t-delay - the delay from the start of simulation until the start of the first cycle;

6. damping factor – the coefficient determining the rate of decay, leave blank for no decay;

7. Phase – the angle, in degrees, where the first cycle will commence, blank for zero; and

8. AC - used for noise and AC small signal analysis, leave it as 1 almost always.

# Devices

The devices I have included are very basic and cannot really be used in a real circuit intended for production without modification, especially to the part-number and footprint parameters. They are provided as a starting point for your own libraries and to enable simple experiments with the simulation process. There are too many devices on the market to make any attempt of mine to provide a library that suits your applications and production methods practical. Do take the process of building a consistent library of symbols, models and footprints seriously from the outset; a simple false assumption early on can propagate through your entire library before it is spotted leading to lots of rework and frustration. If you make a new 'part' that will be the basis of many others it is wise to use it in every step of the development process to make sure it really works before duplicating it. In the process of writing this guide I found a couple of minor inexactitudes that had sneaked through and were present in more than one of the signal sources – one can never be too careful, especially if you don't have a reviewer on hand.

## Resistor (R)

This is just a simple 1k resistor, 2 pins and a basic SPICE implementation.

## Capacitor (C)

This is just a simple 10nF capacitor, 2 pins and a basic SPICE implementation.

## JFET (J)

This is a J112, n-channel junction FET with a spice model from Philips (NXP these days).

# Analyses

Included in the library are three out of seven analyses supported by Ngspice; AC small signal, Transient and Noise. One of those not included, DC operating point analysis, is performed as a preliminary to the AC and transient analyses, I've never found cause to run one on its own. The remaining three orphans; small signal Distortion, Pole-zero and Sensitivity analyses are not included because of the limitations of the last two and the fact that the first has never been very relevant in my work.

## AC Small Signal Analysis

This is useful to find the frequency response of your circuit and can produce Bode plots to help visualise what your amplifier or filter is doing. To use this analysis there must be one source with an AC small signal parameter as the input. Illustration 9 Shows the 4 parameters of the AC analysis which are mostly self explanatory:

1. LinOctDec – type of sweep: linear, octave or decade;

2. Points per interval – how many points per sweep, octave or decade;

3. Start frequency – start sweeping from here; and

4. Stop frequency – stop sweeping here.

Illustration 9: AC Analysis Parameters – TinyCad Update Library Symbol View

Illustration 10: Frequency Response, Plotted from LTSpiceIV

## Transient Analysis

This is the analysis that was used to produce the plots in the section about sources. It is useful to see how an amplifier, filter or control system behaves when a pulse or step is applied. It has five parameters of interest:

1. Print Step – display resolution;

2. Stop Time – stop simulation after this amount of time;

3. Save Data After – don't save anything before this time;

4. Max Time Step – prevent the simulator from using steps longer than this; and

5. Use Init[ial] Conditions – enter "uic" if required, see SPICE manual for details.

If you do not specify a value for item c, zero is assumed. Item d is useful if you feel that the simulation has missed something interesting. It can do this, in an effort to be efficient, by using longer time steps to speed the simulation process. Item e is usually only required for multi-stable circuits, e.g. comparators, if needed enter 'uic' but often a .NODESET statement will work as well.

Illustration 11: Transient Analysis Parameters – TinyCad Update Library Symbol View

## Noise Analysis

If you are designing measurement circuits or amplifiers for audio or photo-detectors then a noise plot is essential to evaluate the performance of your circuit. In use it is similar to the AC analysis in that it requires a single AC small signal source but the noise from all the components is added and plotted rather than the signal itself. This has seven parameters, some of which are the same as the AC Analysis. It has seven parameter to set its behaviour:

1. Vout – the node where the noise is to be measured;

2. Source – the signal source from which noise will be summed;

3. LinOctDec – type of sweep: linear, octave or decade;

4. Points per interval – how many points per sweep, octave or decade;

5. Start frequency – start sweeping from here;

6. Stop frequency – stop sweeping here; and

7. Pts per summary – number of frequency points to be used for each noise summation point.

Item g is an optional parameter that I usually omit.

Illustration 12: Noise Analysis Parameters – TinyCad Update Library Symbol View

Illustration 13: Noise Spectrum (V2: Hz), Plotted from Ngspice

# Other SPICE Statements

There are many other statements in SPICE, but here I will include only those that I have used more than once.

## The .NODESET Statement

This is used to guide the simulator to converge on an initial bias point. It is most often useful if a circuit contains non-linear elements such as a comparator. Use this if, for example, the iteration limit is exceeded and the simulator halts before running an analysis. One can set as many nodes as there are in the circuit, but usually only a few need this treatment to guide the convergence process.

Illustration 14: NodeSet Parameters – TinyCad Update Library Symbol View

Illustration 15: NodeSet SPICE – TinyCad Update Library Symbol View

# The Spice Trail

A note on symbols and labels. I use the IEC symbols and labels which means that my resistors are rectangles with pins on the ends, crystals are labelled G-something and my discrete semiconductors are all labelled V-something as well as several other differences that may bother some people. My advice is don't worry about it, use the convention that makes you and your system happy. I mention this because the IEC labels do not agree with SPICE in many instances so I find it necessary to 'tweak' the SPICE implementation to make both me and SPICE reasonably content.

## Spice Variants

It is important to realise that PSPICE is not exactly the same as SPICE. The P stands for Personal Computer, dating back to the days when SPICE only ran on UNIX mainframes. I prefer to think of it as representing Pay because it is not free. While many PSPICE models will work under SPICE there are some incompatibilities, especially with regard to sub-circuits where a PWL (Piecewise Linear) function has been used. Apart from this there is a guide for converting PSPICE models to SPICE called "Pspice to IsSpice4 Conversion" available at: http://www.intusoft.com/Pspice2IsSpice.htm.

The section on SPICE models in TinyCad's help is barely enough to enable one to link a model to a circuit symbol so I am providing some example here to improve understanding. Please read the TinyCad help in conjunction with this section. It is not actually necessary to have a "run" symbol on your circuit, this can be typed on the simulator's command line once your circuit is loaded. What I do find useful is to include the symbol for the type of analysis on the circuit to save typing all the parameters and remembering their corrrect order and syntax.

### A Simple Resistor Model

Shown below are the two tabs for a resistor's parameters.

Illustration 16: Resistor Attributes – TinyCad Update Library Symbol View

Illustration 17: Resistor SPICE – TinyCad Update Library Symbol View

This is about the simplest model you can have, all the information required for the simulation is included in a single statement.

### A Diode Model

Here is the same information for a 1N4001 diode.

Illustration 18: Diode Attributes – TinyCad Update Library Symbol View

Illustration 19: Diode SPICE – TinyCad Update Library Symbol View

Note that in this case there is a simulation model named in the attributes and that this model is present in the epilogue of the SPICE tab. It is invoked in the model statment by the \$(SimModel) parameter. Note that the names in the two tabs must match otherwise the simulator will complain when it loads the circuit.

### A JFET Model

Here is a JFET with model information from NXP (Phillips).

Illustration 20: JFET Attributes – TinyCad Update Library Symbol View

Illustration 21: JFET SPICE – TinyCad Update Library Symbol View

### A Multi-part Op-Amp

This shows how to apply SPICE models to multi-part ICs. In this case there is an non-homgeneous symbol with the power pins on the 'A' part only.

Illustration 22: OpAmp Attributes – TinyCad Update Library Symbol View

Illustration 23: OpAmp SPICE – TinyCad Update Library Symbol View

Illustration 24: OpAmp Symbol

This example also shows the use of a sub-circuit as a model. Notice that there are four lines in the model pane, one for each op-amp in the IC. The pin parameters must be in the same order as the 'pins' of the sub-circuit. The power pins are the same for each line, just as in the real world.

### The Noise Analysis

Just to illustrate the use of the priority option in the epilogue of the SPICE tab of the Symbol Properties window, here is that view of the noise analysis symbol. Notice it is set to 0 to make this go at the end of the netlist file.

Illustration 25: Noise Analysis SPICE – TinyCad Update Library Symbol View

## Connecting to NGSPICE

Once your circuit is finished use the "Special" menu to perform a design rules check, just to be sure that you don't have two components with the same reference designator. If you do, TinyCad will happily generate a SPICE netlist and report no errors but the simulation will not be what you expect (if it will run at all). Generate the SPICE netlist, also from the "Special" menu, and name it something meaningful (e.g. photo-amp_transient).

Read the How to Use NGSPICE document. Start NGSPICE and use the "cd <directory>" command to point to the the folder where your netlist lives. Next use the "source <netlist.net>" command to load your design. At this point if there are any mismatches between model names or non-SPICE commands in the file you will see error messages letting you know and you must go back and fix them. If it loads correctly simply type "run" and, hopefully SPICE will find an operating point and perform the simulation. If not, it's time to study the SPICE manual and work out how to get convergence. To view the results use the "plot net" command to draw graphs of signal names of interest – it is useful to put easy to remember, short net labels on everything on the circuit for this to be useable.

## Simulation Problems

This section has been added "after the fact" to address some common problems encountered when attempting to simulate a circuit. Problems can arise in even apparently simple circuits so it is best to have some tools to deal with them.

At some point you will see a message like "Failed to Converge" or "Iteration limit Exceeded" before the simulation even starts, This is because the SPICE engine failed to find a DC operating point from which to begin the analysis. Frequently this is because there is a non-linear element in the circuit such as a comparator or some digital component. Sometimes it is because the circuit is simply too complex for a unique solution to the underlying set of equations. The usual solution is the use a .NODESET statement (see 6.1 The .NODESET Statement) or, less often setting initial conditions in the analysis statement (see 5.2 Transient Analysis). These two approaches differ subtly and you should read the appropriate sections of a SPICE manual to gain a proper understanding of the differences. The ngspice manual is very good.

Other things to try include changing some of the simulation parameters, in a netlist they would appear as a .OPTION statment eg., ".OPTION ITL1=200". However, these are not provided in a symbol so they will have to be typed on the simulator's command line eg., "SET ITL1=200". Once again, refer to a SPICE manual for full information on each of these. In fact, it is more convenient to use these on the command line because it is quicker than editing the circuit, generating a new netlist and then re-importing this into ngspice. Some of these parameters affect both the DC solution and the transient analysis; increasing iterations may make the simulation run more slowly. Options from GMIN onwards affect simulation accuracy so use them thoughtfully - changing the values too much may make the simulation results meaningless. They are listed in the order I would try them.
ITL1=x The default is 100; try 200, 500, even 1000 (if you are desperate).
ITL2=x The default is 50; try values up to 250.
GMIN=x The default is 1.0e-12; try changing this in decades (1.0e-11, etc.).
ABSTOL=x The default is 1pA; try changing this in decades (10pA, etc.).
VNTOL=x The default is 1uV; try changing this in decades (10uV, etc.).
RELTOL=x; The default is 0.001; change this cautiously.

Once a transient analysis is running the most common problem to arise is "Time step too small". This means that the SPICE engine cannot find a solution to the equations at that point in time no matter how finely it divides time. The only real answers here are to change the iteration limits or the accuracy as listed above and then try the following .options parameters. The ngspice documentation does not give default values for these so I've shown defaults from other implementations such as OPUS.
CONVSTEP=x; The default is 0.25; reduce this gradually, values up to one are allowed.
CONVABSSTEP=x; The default is 0.1; reduce this gradually, values up to one are allowed.
GMINSTEP=x; The default is 10; try values up to 100.

There are many more options, but these are the most commonly used (at least by me). For more information try the SPICE3 Version 3f3 User’s Manual from Berkeley.

# Acknowledgments

This document was created and converted to PDF using the following application:

Open Office (3.2.0) Writer

Input to and support of the creation process by:

ngspice 20

LTspice IV (4.10a)

Microsoft® Snipping Tool 6.1.7600

Microsoft® Windows 7®

Any registered trade marks belong to their respective owners.

Sources of information include:

SPICE - A Brief Overview (URL: http://www.seas.upenn.edu/~jan/spice/spice.overview.html)

SPICE Basics (URL: http://www.ecircuitcenter.com/Basics.htm)

Ngspice Users Manual Version 20

Berkeley Spice Interactive Front-end (URL: http://bwrc.eecs.berkeley.edu/classes/icbook/SPICE/UserGuide/interactive_fr.html)