LT Spice Tutorial: Custom Blocks / Parameterized Subcircuit Models
In this tutorial, I'm going to introduce an extremely useful feature in LT Spice -- how to create a custom, parameterized part. I don't actually know what to call the resulting 'object' Maybe a block, a model, custom subcircuit, custom part -- I'm not sure.
The point is, you're going to have a custom circuit that you can create a simple symbol for, and then import and use with other circuits. Furthermore, instances of your custom model can have per-instance parameters!
For our example, we're going to create a simple model for a low pass circuit. We'll be able to import this block into other circuits, but each can have its own 3 dB roll-off frequency.
To start, we have to draw the model...
Drawing the model circuit.
We want a low-pass filter at frequency f. For the circuit above, the relationship between f, R, and C is: f = 1/(2*pi*R*C). In the design above, we pick R = 1k, and then compute a value for C based on a supplied parameter f. If you're not sure how to use the .PARAM statement in LT Spice, see here.
We assigned bi-directional ports to the input and output of our low pass circuit. These will act as the interface between our model and the circuit in which it is instantiated.
Be sure to save the curcuit with the name of your model as the filename. I saved it as Draf5.asc, you should strive for better.
Now go to Hierarchy > Open this sheet's symbol. Since there isn't one, you will be asked if you'd like to create one, as seen below. Say yes!
Prompt to create symbol.
Modifying the symbol.
Now you have a chance to modify the symbol for your model. You can use the drag and move tools to change the symbol and whatnot. It's not too critical. You can also right-click the ports to adjust them a bit.
Save the symbol. It must have the same name as the model/circuit you saved earlier!
Our model is done! Now let's start a new circuit. The purpose of this circuit will be to test our model.
Changing the symbol directory.
In the new circuit, try to add a new component in the normal way. At the top of the window, you should see the "Top Directory" field where you can select the directory which contains the custom model or custom block we just made. Add your new part to the schematic.
A test circuit for our model.
We're going to test the model with a simple AC analysis as seen in the image above.
Now for the cool part. We want to specify the cutoff frequency of our low pass block. To do this, right-click on the block, and specify the parameter 'f' as seen below.
Setting a parameter for our model instancce.
With any luck, we should see a low-pass output at the output of our test circuit. The cutoff frequency should be 1 kHz.
But wait, I'm very tired, and I screwed up creating my model! I put 'pi' in the parameter definition (which is not known to LT Spice), and I also missed parenthesis in the paramter definition.
The good news is, I can open and modify the model circuit on the fly, and the model will be 're-read' at run time for our test circuit.
Modifying the model circuit / definition.
So we can fix things right up.
Simulation result. The -3dB frequency does appear near 1 kHz!
Now that everything is fixed, we can see the low-pass output at the output. The cutoff frequency is near 1 kHz, as we expected.
Okay, that's all. If we put another block in our circuit, we could specify a different 'f' for it. Supplying the paramters per instance is what's really powerful here, besides the work-saving that comes along with creating the block.
Hopefully, this tutorial saves you some time, sometime!