Friday, February 26

Adding Spice to Your Workbench

Most of us didn’t fight in World War II, drive a race car, or fly the Space Shuttle. But with simulation, you can experience at least some of what it would be like to do those things. Granted, playing Call of Duty isn’t really the same as going to war. No matter what you are simulating, it only goes so far. However, you can get a lot of value from a simulation. I’d bet the average kid who has played Call of Duty knows more about WWII locales and weapons than my high school history teacher.

When it comes to electronics, simulation is an excellent way to get insight into a circuit’s operation. After all, most circuits operate in the abstract–you can’t look at an audio amplifier and see how it works without a tool like a scope. So simulation, when done well, can be very satisfying. You just have to be careful to remember that it isn’t always as good as the real thing.

That’s Spicy

One of the best-known electronics simulators is Spice, which Berkeley created in 19XX. In its original form, you had to punch cards that described your circuit and the analysis you wanted to perform. Modern PC versions sometimes replace the deck of cards with a text file. The best modern versions, though, give you a GUI that allows you to draw a schematic and then probe it to see the results.

There are several paid and free versions of Spice (and other simulators) that include a GUI. One of the best for a casual user is the free offering from Linear Technology called LTSpice.

Linear makes LTSpice available and populates it with models for their devices in the hopes you’ll buy components from them. However, the software is entirely usable for anything, and it has a powerful set of features. Linear produces the software for Windows, but I can attest that it runs just fine under Wine on Linux. The Web site will invite you to register, but you don’t have to if you don’t want to.

Spice Quartet

There are four parts to working with Spice: The schematic capture (that is, getting the schematic in digital form), the simulation engine, the models, and the output. LTSpice is very easy to use for schematic capture, although some of it is a bit non-standard. For example, F9 is the undo key. There is no paste command. Instead, you select the copy tool (or hit F6) and then select the items you want to copy and drag them around. If you’ve used Eagle for PCB layout, it will remind you of that.

There’s a getting started guide (which is a bunch of PowerPoint slides) on the Linear site that you should probably read. You can also watch my quick demo below. The basic operation is pretty simple once you get used to the copy and paste or how to drag and drop existing components (hint: the open hand doesn’t move wires; the closed hand does).

DC Circuits

schem1The video below shows how to build and analyze a very simple DC circuit (see right). You’ll see that once you get used to how LTSpice does commands, it is very easy to draw a circuit like that. One thing to note that I didn’t mention in the video: While I entered 1000 and 1100 for the resistor values, I could have also said 1k or 1.1k. Spice is smart enough to know about most prefixes like k (kilo), u (micro), and so on. The only confusing one is m. This is milli, not mega. If you want to put in, for example, a 5 megaohm resistor, try 5000k or 5meg. Using 5m, however, will give you .005 ohms, which is going to throw off your simulation quite a bit. The letters are not case-sensitive, so m and M are both milli.

Another tip: In the video, all the components are oriented in their default direction. However, when you are dragging an outline (either because you are adding a new component or dragging an existing one) you can press Control+R to rotate it by 90 degrees.

Just for point of comparison, the actual Spice “card deck” for this simple circuit looks like this:

R1 N001 A 1000
R2 N001 B 1000
R3 A 0 1000
R4 B 0 1100
V1 N001 0 10
.op
.backanno
.end

Not so hard to understand, but that’s because this is a very simple circuit. Imagine something with dozens or hundreds of components, and you’ll appreciate the LTSpice schematic capture. The R1 and V1 parts of the schematic are simulation models for resistors and power sources. These simple models always exist in Spice but for some more sophisticated components, you might need to select a specific model. Also, LTSpice and read models you might get from another vendor or on the Internet.

The simulation engine reads the lines that start with a period. The .op command on the schematic (and in the card deck) tell Spice just to output the DC operating point of the circuit. For this simple circuit, that’s all you need. Here’s the output from running the simulation:

--- Operating Point ---
V(n001): 10 voltage
V(a): 5 voltage
V(b): 5.2381 voltage
I(R4): 0.0047619 device_current
I(R3): 0.005 device_current
I(R2): 0.0047619 device_current
I(R1): 0.005 device_current
I(V1): -0.0097619 device_current

You can see the value of using the label command (as I did in the video). Without it, you are stuck wondering which voltage is V(n238).

The real fun, though, is the Transient simulation (.trans). It creates (more or less) a virtual oscilloscope that can display voltage or current over time at any point in the circuit. The only problem is that with a DC circuit, the trace isn’t very interesting because it doesn’t change. Here’s a transient plot of V(a), V(b), and V(b)-V(a):

Notice the red trace is a math expression. You can use math to find a lot of information about your circuit. For example, the power in R3 would be V(a)*I(R3) and the power through R1 would be (V(n001)-V(a))*I(R1).

Here’s a quick trick. If you don’t want to set up the expression V(b)-V(a), there is another way to do it. Normally, to measure V(b), you’d click on the wire. However, if you click on the wire and drag, you’ll see the probe appear and you’ll drag a black probe. Move the black probe to the ground reference you want (V(a), in this case) and let go. Now the plot will show V(b,a) with no math required.

This scope-like output is more impressive, of course, when you have an AC circuit or, at least, a circuit that changes state over time for some reason. In the next post, I’ll show you an example of that.

schem2However, this gives you a basic feel for the four parts of LTSpice: the schematic capture, the models, the simulation engine, and the output. There’s plenty more to learn, but you can do some pretty interesting DC analysis with just the tools we’ve discussed. If you want to experiment, you can try simulating an LED and the effect of different dropping resistor values. Another classic Spice “experiment” is showing that maximum power transfer occurs when the source’s resistance equals the load resistance (see the schematic to the left). Next time, I’ll show you more about that schematic and AC circuits, too.

 


Filed under: Hackaday Columns, how-to

No comments:

Post a Comment