Simulating circuits
Many software packages and online tools can simulate the behavior of circuits. Here we summarize LTspice, a free download for MacOS and Windows from Analog Devices. Download and install it. If the link is broken, just search for "LTspice" to find the downloadable software.
The Windows interface is fairly intuitive to use, but the MacOS interface is not. Below is a brief tutorial to get you started on the Mac; it should be easy to replicate these steps on Windows. The Mac instructions below refer to "ctl-click" and "two-finger click." "ctl-click" means pressing the control key then clicking, and "two-finger click" means clicking with two fingers on the trackpad.
Below is an example circuit with a square wave voltage source (changes from 0V to 5V at t = 0 and from 5V to 0V at t = 1 ms) and the result of the simulation. You can also download EA3-relevant sample circuits to test.
Experiment with a Provided Circuit
The LTspice installation comes with many example circuits. Look for the files in your installation. For example, on a Mac, you probably have a new folder called "LTspice" under "Documents." Within this folder is the folder "examples" with subfolders "Applications" and "Educational." You can go into a subfolder and open, with a text editor, any ASCII file (ending with .asc) to see the information describing a circuit. Below is some sample code from one of the examples. This code defines a sinusoidal voltage source, a capacitor C1 with capacitance 62 nF, and a resistor Rload with resistance 135 Ohms.
SYMBOL voltage -32 96 R0 SYMATTR InstName V1 SYMATTR Value SINE(0 166 60) SYMBOL cap 160 224 R0 SYMATTR InstName C1 SYMATTR Value .062µ SYMBOL res 352 16 R0 SYMATTR InstName Rload SYMATTR Value 135
The provided example circuits are much more complicated than anything we will study in EA3! Still, try one. Do File > Open and navigate to the LTspice directory and choose a circuit (any file ending in .asc) from the Educational folder. Then click the icon that looks like a running man at the top left to run the simulation and see the results.
Draw Your Own Schematic
We will create the circuit with a resistor, a capacitor, an inductor, and a variable voltage source that you see at the top of the page.
- Create a new schematic. Choose File > New > New Schematic to create a new schematic window.
- Add a 15 Ohm resistor to the schematic. In the window, ctl-click > Draft > Component, click "res" in the pop-up window, and choose OK. Then your mouse will have a resistor attached to it. Click anywhere in the schematic window and a resistor will be placed there. After placing the resistor, the interface is ready for you to place another resistor, but we only want one, so press ESC to stop adding resistors. To set the resistance of the resistor, two-finger click on the resistor itself, or the text "R" next to the resistor, and in the pop-up window, change the value to 15. (If you wanted 1 milliOhm, you would type 1m or 0.001. If you wanted 1.1 kiloOhm, you would type 1.1K or 1100. If you wanted 3.3 megaOhm, you would type 3.3Meg or 3300000. The unit prefixes are case insensitive, so M is treated the same as m.) The resistor is labeled R1. Since we will only have one resistor in the schematic, let's change the label to R. Two-finger click on the text R1 and change the label to R in the pop-up window.
- Add a 1.6 uF (microFarad) capacitor to the schematic. ctl-click > Draft > Component, click "cap" in the pop-up window, and choose OK. Put the capacitor anywhere; don't worry about connecting it or orienting it yet. Change the value to "1.6u" (easier to type and more readable than 0.0000016; you could use n for nanoFarad or p for picoFarad) and the label to "C" similar to what you did for the resistor.
- Add a 1 mH (milliHenry) inductor to the schematic. ctl-click > Draft > Component, click "ind" in the pop-up window, and choose OK. Change the value to "1m" and the label to "L."
- Add a variable voltage source to the schematic. ctl-click > Draft > Component, click "voltage" in the pop-up window, and choose OK. Change the label to "Vin." Two-finger click the voltage source to set its behavior. Choose "Advanced" from the pop-up window so we can get a behavior other than just a constant DC value. From the new pop-up window, choose the time domain function style "PULSE." This will make a periodic square-wave voltage that switches between "Vinitial" and "Von." The time the voltage is "on" is "Ton" and the period of the cycle is "Tperiod." You also need to set a nonzero rise time Trise and fall time Tfall for the square wave transitions. For our circuit, choose Vinitial = 0, Von = 5, Trise = 1n (1 ns), Tfall = 1n, Ton = 1m (1 ms), and Tperiod = 2m. You can leave others unchanged. Finally, uncheck the "Make this information visible on the schematic" checkbox so the schematic is not so cluttered with text. (If the text is still there, edit something about the voltage settings and uncheck the box again, and the text should go away.) Other voltage types you could experiment with are SINE for a sine wave voltage and PWL (for "piecewise linear"), where you specify a series of (time, voltage) pairs and the voltage function is just a straight line between these points.
- Add a ground symbol. You need this for every circuit to properly simulate. Press 'g' on your keyboard and your mouse will drag around a ground symbol that you can place by clicking. Press ESC to stop adding ground symbols.
- Move your components and connect everything together.
- To delete a component, ctl-click > Edit > Delete. This will create a scissors cursor. Click on the component and it will be deleted. ESC to exit the delete mode.
- To move or rotate a component, ctl-click > Edit > Move. Then use the mouse to select a component by click-dragging a box around it. The component will now be attached to your cursor and you can click anywhere to put it down. To rotate the selected component, press ctl-R. ESC to exit the move mode.
- Once your components are where you want them to be, connect them up with wires. ctl-click > Draft > Wires, click on a component endpoint, move the mouse to start drawing the wire, click when you want to make a turn, and click on another component endpoint to finish the connection. You can keep drawing wires by clicking on another component endpoint. ESC to edit the wire mode. You can delete a wire using the delete mode above. Make sure to connect your ground.
- Optional: Once your circuit looks the way you want it to, you can do ctl-click > View > Zoom to Fit to make your circuit fill your schematic window. You could also try ctl-click > View > Grid Dots if you want to see the drawing grid.
Your circuit should now look similar to the image below.
Note: In this circuit, we drew a closed loop for the current to flow, and added a ground symbol to indicate which point in the circuit is treated as zero volts. We could have just put a ground symbol at the bottom of the voltage source and another ground symbol at the bottom of the capacitor and never explicitly drawn a wire connecting the voltage source and the capacitor; the loop is closed through the common ground.
Creating a timed switch: One way to create a timed switch is to use a resistor, then instead of giving it a numerical resistance value, enter the text "R=if(time<0.1,1m,1Meg)" to create a resistor with 1 milliOhm resistance before time t = 0.1s and a resistance of 1 megaOhm afterward.
Running a Simulation and Plotting the Results
- Choose the parameters of the simulation. In the schematic, ctl-click > Draft > SPICE directive. In the pop-up window that appears, ctl-click in the large text box and choose "Help me edit > Analysis Cmd" from the pop-up window. Choose "Transient" as the simulation type. For Stop Time, type "2m" for 2 ms and click OK twice. Now use your mouse to place the SPICE directive (".tran 2m") in the schematic. To edit this directive later, two-finger click it.
- Run the simulation. Click the running man in the schematic window. A plot window with the results will pop up, but there will be nothing in it, because we haven't chosen what to plot yet. Let's plot the current through the inductor (which is the same current through all the elements for this series circuit) and the voltage across the capacitor. Put the cursor on top of one of the inductor. An arrow should appear, indicating this will be a current-through-the-element measurement. After you click, the current plot will show up in the plot window. It will be labeled "I(L)" indicating it is the current "I" through the element "L." Now put your cursor at a point between the resistor and the capacitor, and the cursor should turn into a symbol that looks like a voltage probe. Click, and a plot of the voltage at this point will show up in the plot window. This plot will be labeled something like "V(n003)," the voltage at node 003. (You can provide a name for a node using ctl-click > Draft > Net Name, providing a name, and clicking on the schematic at the node.) To learn the default labels of the nodes, hover the cursor over points in the schematic, and text will appear that tells you the label for the node.
- Delete traces from the plot. Choose something to plot (e.g., a current or a voltage) in the schematic, then choose it again. All other traces will go away.
- Change colors of the plots. Two-finger click on the text V(n003) or I(L) and choose the plot line color you want.
- See information on the average and RMS of the plot. ctl-click on the text V(n003) or I(L).
- Change the horizontal or vertical scales of the plot. Two-finger click on the ticks of the axis.
- Plot functions of the data. You can plot functions of the nodes at voltages and currents through elements. Two-finger click in the plot window and choose "Add Traces." To plot the voltage across the inductor, in the "Expression(s) to Add to Plot" text box, type "V(n001)-V(n002)" (if those are the node labels around your capacitor).
- Add a grid to your plot. Two-finger click in the plot background, choose View > Grid.
- Changing colors, plot line thickness, etc. In the schematic or plot window, click the hammer icon. Under Waveforms, you can change the thickness of the plot lines. Click "Configure Colors" and choose "Background" in the "Selected Item" drop down to change the background colors of the schematic and plot (for example, to get a white background instead of black or gray). Change the colors of the traces and axis labels.
- Annotate the plot with text and other. Two-finger click in plot background and choose Draw > Text, Arrow, etc.
See if you can make the result of your plotting look similar to the image at the top of this page.
Saving Your Work
Now that you have a circuit simulation running successfully, you will want to save your work. You should save the circuit schematic (with its SPICE directive indicating the type of simulation, e.g., transient analysis and the duration of simulation) as a .asc file and the plot settings as a separate .plt file. Click in the circuit schematic window and do File > Save As and save the schematic somewhere appropriate on your computer. If your filename is "LRC-circuit.asc," then you should save your plot file as "LRC-circuit.plt" (click in the plot window and Save As). Then the next time you start LTspice, you can File > Open the .asc file, and when you run the simulation, it will automatically use settings saved in your .plt file.
Try Sample Circuits
Download the zip file with the circuit on this page and others. In LTspice, do File > Open, choose the .asc file, and run the simulation.
Analysis of the Example Circuit
This circuit has two state variables: the current through the inductor (which is the only current in the system) and the voltage across the capacitor.
After a long time at V
The circuit is in steady state (no state variable is changing), so . Since , (the capacitor acts as an open circuit). By KVL, , so . In summary,
0 | 0 | 0 | 0 |
Immediately after the switch to V at t=0
The capacitor does not allow its voltage to change discontinuously and the inductor does not allow its current to change discontinuously. Otherwise there would be an infinite current or an infinite voltage, respectively. So . By KVL, or A/s. Also . (Note the second derivative of is not zero.)
0 | 5000 A/s | 0 | 0 |
After a long time at V
(In the circuit above, 1 ms classifies as a "long time," as it is several times the time constant of decay.)
The circuit is in steady state (no state variable is changing), so . Since , . By KVL, , so . In summary,
0 | 0 | 5 V | 0 |
Immediately after the switch to V
The capacitor does not allow its voltage to change discontinuously and the inductor does not allow its current to change discontinuously. So and V. By KVL, or A/s. Also .
0 | -5000 A/s | 5 V | 0 |
Two first-order coupled state equations
The two first-order coupled state equations for and are derived as follows. By the constitutive law for a capacitor,
By KVL, , or
A single second-order differential equation for
We could write a second-order equation for either or . For , start by taking the derivative of the state equation
and plug in the state equation for to get
Follow a similar procedure taking the derivative of the state equation for and plugging in the other state equation to get
General solution for
The previous differential equations have the same form and the same characteristic equation, . Their solutions differ only by their different initial conditions.
Plugging in the numerical values for R, L, and C, the characteristic equation is , which has roots at . The roots are complex conjugates, so the solution is underdamped with the form
The decay time constant is ms, and the response will decay to 2% after ms, which is why the 1ms time for holding the pulse in our example qualifies as "a long time." The period of oscillation is ms, which is consistent with the plots above.
Particular solution after the switch from V to
The time derivative of the response is
To solve for the constants for for the case where we switch the input voltage from 5V to 0, we use the initial conditions A/s. This gives us and A, for a response
The time for one-quarter of a cycle of oscillation satisfies , or s. Plugging this time into the response, we get mA, which agrees with the plot. We could do a similar calculation to get the particular solution for using the initial conditions .