Linux Format

Circuit simulation

Mats Tage Axelsson shows you the possibilit­ies available to design and simulate your own circuits for fun and profit. Mostly fun, to be honest…

-

Mats Tage Axelsson shows you the possibilit­ies available to design and simulate your own circuits for fun and profit. Mostly fun, to be honest…

When you want to start a new project using electronic components, the first thing that comes to mind is a schematic. You imagine what components you need and how to draw the schematic. Once you’ve managed to come up with a design or perhaps tweaked an existing design, you’ll want to make a breadboard. But wait – maybe you want to test it without the physical circuits.

This is useful so you can catch simple mistakes and avoid building circuits that will just break your components. This way, you’ll save yourself the trouble of fixing things that should be clear for the more experience­d. There are several ways you can insure the viability of your current design. Simulation is one.

To show how to do this, you’ll create a circuit that you can then simulate. You’ll create a few circuits to see what the workflow is that you need to achieve a reliable result. There are many methods you can choose: write your spice files manually, use an editor, or even write the test as they go using ngspice.

The GEDA suite of tools are suitable for creating schematics, testing the viability of a circuit and creating lists of components – a so-called BOM

(Bill Of Materials). The developers have also included ways to check the way the circuit behaves, with many different ways to simulate the same circuit.

To test circuits, you first need to have a circuit. For this purpose, the GEDA package provides a series of programs: gschem, gattrib, gnetlist, gsch2pcb, gsymcheck and finally libgeda which contains functions for manipulati­ng the files. Each of these handle different steps of a project and can be used individual­ly, although they’re designed to be part of a chain. The steps when designing an electronic circuit starts with a schematic. The obvious name of this program is gschem, and provides a way to add all components and connection­s you need for the circuit to run.

With a schematic ready, you then want to make sure it works the way you expected. This is what you’ll learn in this tutorial. The tool is called ngspice and is the next generation spice. SPICE means Simulation Program with Integrated Circuit Emphasis and is a command linebased tool. You can use it interactiv­ely or in batch mode. Most commonly, you’ll use batch mode.

When you’re ready with a circuit, you need to make a PCB. To do this, you have several steps to execute, the first being to set references. The refdes_renum command will check your circuit and number all components and reference points according to where they’re in the circuit. Next, you can create a pcb list, so you can draw the circuits for production. The software gsch2pcb will create all files you need to run pcb, where you create the board itself.

Simulate me

For the simulation aspect, you need a netlist that has all the components, connection­s and models. The best way to get started is to install Kicad and use its help system. There are many tools to choose from, thanks to SPICE having a very simple standard for describing components and circuits.

There are also more programs added by external developers that complete the suite. Several waveform viewers are available, including GTKWAVE for the GTK graphics toolkit; and gaw, an analog waveform viewer that works with gspiceui. The Icarus Verilog simulator shows circuits using the IEEE 1364 standard – useful for more advanced projects. The gattrib program acts like a spreadshee­t where you can easily add components and change their attribute and values. When you do, you have a direct view of the components, which is useful for changing many components at the same time.

The whole suite of tools comes with most distributi­ons. You can also stay up to date with GIT. It’s available on sourceforg­e for download and as a git repository. There’s a Github repository but the developers don’t maintain it. Another important part of simulating circuits is to find data about components. Many manufactur­ers will have spice files available for all components they sell. If they don’t, you need to make those yourself.

It’s common that chip manufactur­ers provide PSPICE models for their chips. To make sure that you can handle these, just add a settings file for ngspice. The filename is .spiceinit, and you can have it in your home directory. You can also create one for each project directory you use. This is a rule for all the tools you encounter here: you can override settings from the working directory while using the standard settings from your home directory.

* Use PSPICE files set ngbehaviou­r=ps

This file makes ngspice read PSPICE files correctly. Much of the time, you’ll look for and create components. To save yourself time, there are many websites where you can find files that describe the physical shape (footprint files). You also have files that describe all the characteri­stics of a component. The files are called model files and are available from manufactur­ers, and many users also share them on their pages and forums. Without the files, you can’t decide how they work.

The suite is designed to cover the entire workflow to create a new circuit. This starts with a schematic, continues with creating the supporting files for a PCB and ends with a list of files that support production. You’ll want to simulate a circuit before you start designing the PCB. Use ngspice to simulate it. ngspice needs input files to operate, and one of these is the netlist file. You can create this using the gschem program, or any other package, as described earlier. It’s good to know what the file contains, in case you need to troublesho­ot. Many component manufactur­ers have one available for your use. You may have to create one yourself, though.

If you have a simple circuit, you can write this manually. You need to learn the format which is not particular­ly difficult, but the connection to component files can be confusing. You’re also calling a spice file which needs to be in your library. Until you’re comfortabl­e with it, you need to put in some effort to use components that aren’t already available.

To demonstrat­e the netlist, you need to start with a simple circuit – the simplest approach is a battery and a resistor. In a circuit like that, you have the two components and two nodes. A node is where the potential is a certain level, most commonly a wire or lead from one component to another. The nodes are numbered, with 0 being ground and the other values starting at the plus side of the power source.

The numbering is important in relation to each other, but they can also be set to relate to other boards. This gives you the possibilit­y to simulate an entire system, where the AC/DC converter feeds your device, for example. You’ll seldom use spice commands, but they’re handy to know when you have new components or if something goes wrong. Below is a simple code that describes a circuit:

Example netlist; Bat and resistor.

*Sources

I1 0 1 15m; dc 5

*Resistors r1 1 2 10k r2

*Capacitors

*Directives

.dc

.op

.end

As you can see in the example above, there are asterisks for commenting in your code. This is useful for making sure you understand what the components are. In the example, each component has a section. It’s your choice how to comment.

At first glance, this seems simple; you create a circuit and get the voltages you get from the circuit. It’s simple, but when you need more features, you can define these with many characteri­stics. How they act in different

temperatur­es is an example most common with resistors. This is where the .model statement comes in.

Using the model statements, you set the values for this device. In a resistor, two values – tc1 and tc2 – describe the thermal coefficien­t for the component.

To run ngspice on the command line is possible and not as hard as it may seem. With that said, the graphical tools and circuit design tools makes the task easier. When your tools are creating problems for you, basic knowledge is useful. You can also use this to create new circuit model files.

The better use of the command line is to try out a circuit when you have small changes to make. To do this, start ngspice on the command line and use the internal source command with a circuit file. Use the earlier file to see how it works.

$ ngspice Batresist.net

Circuit: example netlist; bat and resistor. ngspice 1 -> run

Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

No. of Data Rows : 1 ngspice 1 -> plot r1

Error: no such vector r1 ngspice 1 -> plot all

This plot appears in a separate window, but will not show much., because the circuit has a resistor and a source, and there’s just a steady voltage over the circuit. You also have no operations in the file so you must add those. You can also run interactiv­ely. The command line is more powerful than it seems at first glance.

You can run regular shell commands, and there are also many built-in commands in ngspice itself. The ones you’ll use the most are the ones that correspond to the directives. These are listed above. You also have listing which shows your netlist; and edit which opens your current netlist for editing. When you’ve finished editing, it brings you back to the ngspice prompt with a question: whether you want to run the circuit. This is a fast way to troublesho­ot minor mistakes.

A handwritte­n netlist is usually slow and unnecessar­y. The times you can use them is when you learn writing on paper and trying it out with a command-line tool. In the GEDA suite, you can use gschem to draw schematics. Start that program and make a new schematic. gschem is a simple program, which makes it easy to start. It can do the tasks you want, but doesn’t have many bells and whistles.

A very common circuit is a Full Wave Rectifier. It’s simple and has an AC wave form going in and takes out DC. Just use the standard components that comes with gschem to place all the components. There are many keyboard shortcuts, but you also have drop-down menus for all features. Now that you’ve a schematic, name it Fwr.sch, you can continue with making a netlist file. The command to use for this is gnetlist.

$ gnetlist Fwr.sch -o Fwr.net -g spice-sdb

Now, check the file. The contents will be sparse and have only a few components. This means that you can’t simulate anything yet. What’s missing is both the model statements and parameters for the simulation itself. You can figure these out and fill them up in the list, but you have other options, too. You can then fill them up on the schematic.

To add a model of a component, you use the component list and choose spice-model-1.sym . This is a standard way to describe a component. The file can also be a circuit file. This doesn’t describe the multitude

of characteri­stics that a component has, though. For that, you need a model statement.

As the name suggests, the model statement describes a model of a component. It contains the characteri­stics of the component, so you can calculate how it’ll behave. In the code, you can see the values of a diode:

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

The values are available in the data sheet of each component. Most of the manufactur­ers have their own files, though, which you can add when you design your own circuit. In the example, you have IS which means saturation current, RS which means Ohmic resistance. BV is reverse breakdown voltage, and the list goes on. To add this value to your circuit, you need to add it. A useful way to do it is to have it on the schematic. You can also add it to your file. The most common way is to have a /models directory where you put all the models you need.

You also need a simulation directive. This is available as a component named spice-directive-1.sym. Place this attribute on the drawing and fill in the value attribute with .options TEMP=25 . Now you can run gnetlist again. In the result, you’ll now see the options, the component and the circuit. You can add more options. In particular, you’ll need more for simulating alternatin­g-current circuits. One way is to create a separate file that contains the directive. For an AC circuit, you need to have a TRAN statement.

.TRAN 1m 100m

With a single line like that, you can put it in the netlist

file yourself. However, it’s better to have that from the beginning and when you’ve big projects, you’ll need more structure in your workflow.

With the file ready, you can now run the simulation. As mentioned earlier, you can run this manually with ngspice . To do that, you need the netlist file you created and possibly model files for any components. You also need to add control lines. These lines start with a dot and are a few standard commands. These are .tran, .plot, .print and a few others.

For those to work correctly, you need to have defined what they’re supposed to plot. You can also run it interactiv­ely by typing the command without parameters. You’ll get a prompt where you put the name of your netlist file and that includes the file. A more efficient way to do the job is to put all the values in your

netlist file and run ngspice in batch mode.

$ ngspice -b -r Fwr.raw -o Fwr.log Fwr.net

This will create the Fwr.raw file, it contains the raw data that gwave can handle. Now it’s simply a case of running that command and you’ve a simulation:

$ gwave Fwr.raw

The simulation shows up in a two new windows. You need to drag the values you want to see from the listing window into your view window for the values to show up. You have optional waveform viewers – this is just the original viewer.

When your design is ready for reality, you need to add a footprint value to each component. The footprint file shows the shape and pins of your components. There are a large collection of footprints available from the beginning, However, you’ll always be on the lookout for your specific component.

When that’s finished, you can create a pcb file with gsch2pcb and then run the PCB program to make a layout. Initially, the lines aren’t correctly run, you’ll need to run them yourself. Your next step is to create a

Gerber file for manufactur­ing. There are many more details to the process, but that’s a tutorial for another time and another issue of Linux Format.

Any developmen­t job requires planning, design and testing. To carry out as few design iterations as possible, use tools that can tell you what will happen when you power your project on. Finding faults as soon as possible is something of an art and a very important one. There are many tools available for this job but we would recommend the GEDA suite of tools. It requires more work upfront, but you keep control and the suite remains efficient on resources. Use these tools both to get the feel for how to do it and to stay in control of your destiny (a little overdramat­ic, no? – Ed).

When you come to a profession­al shop, you may need to learn other systems for designing a circuit. Even if this will be a learning curve, having used GEDA, you’ll know the basics and the standards required to understand how circuits work and how to tweak them for best performanc­e.

 ??  ??
 ??  ?? A four-bit NAND gate as visualised by ngspice. Ngspice calls the graph program, which is a simple X utility.
A four-bit NAND gate as visualised by ngspice. Ngspice calls the graph program, which is a simple X utility.
 ??  ?? OUR EXPERT Mats Tage Axelsson enjoys showing you how to make your life and work using the Linux operating system a better and more pleasant experience.
OUR EXPERT Mats Tage Axelsson enjoys showing you how to make your life and work using the Linux operating system a better and more pleasant experience.
 ??  ?? The most efficient way to do electronic design from the command line is to split the screen in two, with ngspice on one half and the graphs on the other.
The most efficient way to do electronic design from the command line is to split the screen in two, with ngspice on one half and the graphs on the other.
 ??  ?? To add SPICE directives in gschem, you can find them as components under a special section in the file list of the program.
To add SPICE directives in gschem, you can find them as components under a special section in the file list of the program.
 ??  ?? You can also run simulation­s from the gspiceui. It makes it easier to visualise what your circuit consists of and how you want to analyse it.
You can also run simulation­s from the gspiceui. It makes it easier to visualise what your circuit consists of and how you want to analyse it.
 ??  ?? When you use KICAD, you have more informatio­n available for picking components, It’s also easier to create and make use of many directorie­s.
When you use KICAD, you have more informatio­n available for picking components, It’s also easier to create and make use of many directorie­s.
 ??  ?? Drawing circuits in gschem supports many common components, including amplifiers of many types.
Drawing circuits in gschem supports many common components, including amplifiers of many types.

Newspapers in English

Newspapers from Australia