# How to pass parameters to subcircuits in LTSpice with the include statement?

Let's say I create a block with resistors R1 and R2. I want to use this block as a subcircuit in other designs. The typical way to pass in the values for R1 and R2 is to right-click on the block and type in the values as shown in the image below.

How do I pass the parameters to the block X1 as a spice command instead of right-clicking and typing the parameters manually? I would like to put all my parameters in a file and use the .include statement to import the values.

• Just look over this LTspice example from my collection of LTspice ideas I've worked on in the past. It's a PNG, but it is big and you can zoom up on it where you need to. You can see pretty much everything LTspice offers in terms of parameterizing SUBCKT blocks. If you can't find it in that mess, then it's probably not available. I used everything there that I know about. So it exhausts my abilities.
– jonk
Jun 24 '21 at 6:54
• You can set up default params within a SUBCKT and over-ride them with a call to it. A SUBCKT can have more parameters inside it, only some of which are over-ridden, or it can have fewer parameters in it and the caller can provide more than it knows how to use, too. You can set up a default arrangement and then override just the parts of that default you want to over-ride. You can refer to global parameters in the computations as well as local parameters in any combination. I'm not sure what you actually need. If you look at that picture I provided yesterday, I think it's enough for all needs.
– jonk
Jun 24 '21 at 19:15
• However, I probably don't understand why you think there is any difficulty. For example, you can create a SUBCKT block that has default parameter values for your two resistors. You can use that block in another SUBCKT and there you only may want to modify one of the resistor values, not both. You don't have to specify both. You can just specify one. You can have very complex circuit with lots of parameters and stamp it around all over the place, and only have to modify the few things you care about when using it, picking up defaults for the rest. You can have the defaults be global or local.
– jonk
Jun 24 '21 at 19:22
• So, if you post up a very specific schematic that works, but where there is also a very specific complaint that you can express clearly and ask if there "is a better way to achieve X" then I might take a hack at answering. But right now, what you show is... not enough. I can't really tell what the complaint really is about. I can just see a simplistic example that you imagine is sufficient to communicate. To me, it's not. But that may just be me. That said, I need more than I see. Questions I don't feel I fully understand merit only a few comments from me. No reflection on you. That's just me.
– jonk
Jun 24 '21 at 19:26

In the way you constructed your block with the two resitors, in that same way you can pass parameters to subcircuits using your block:

* a subcircuit/block with two resistors whose
* values need to be defined externally

.subckt r2d2 a b
r1 a 1 {r1}
r2 1 b {r2}
.ends r2d2

* another subcircuit that makes use of r2d2, and
* who needs, in turn, the value for its own,
* internal resistor, defined externally:

.subckt 3po x y
xu1 x 1 r2d2 r1={p} r2=1k
r1 1 y {q}
.ends 3po

* using 3po in some schematic/netlist with
* externally defined values :

xu1 m n 3po p=2k q={a}
.param a=3k


As you can see, not all subcircuit parameters need to be defined in .param statements, the same way not all need to be defined inside the instance of the subcircuit; they can be mixed. Using an .inc can be done the usual way from this point forward.

Just to be sure it's clear: subcircuits are like a scope and anything that happens inside a subcircuit stays within that subcircuit, unless it's made available to the outside from within the subcircuit. That means either passing parameters to the subcircuit (what you're showing in your picture) or external pins that make available internal quantities (voltage, current) to the outside world.

If your purpose is to have the .param statemens residing in an external file to be included with .inc then the only way to change them is through assignment with {}, which implies editing by hand -- one or another. The only other shortcut to defining each parameter, separately, is to have all the parameters related through a function, in which case, instead of a long .param line you can have a .func. The values will then be f(1), f(2), etc, and the function .func f(x) {2*x+1} (for example).
• Let's say I use 10 such r2d2 blocks. Then I would have to assign a parameter to each one of them, such as r1={r1} and r2={r2}. This is exactly the issue I am trying to avoid. I don't want to right-click any block and start assigning random names to them. I was hoping there would be some sort of command such as .subckt instance_name params: r1=1k r2=1k. This way I don't right-click anything and I know the block whose parameter I am changing. Jun 23 '21 at 20:52
• @ChadWinters It looks like you need the same parameters for all the blocks, i.e. r1=1k for all. In that case, what I said very much applies: place one block, assign parameters in curly braces (r1={r1}), copy-paste to your heart's desire, then add .param r1=1k. With that you've changed the values for all. If you want to avoid either the naming part (r1={r1}) or the copy-paste (or both), you're out of luck because what happens inside a subcircuit stays within the subcircuit. Those {} assignments are the only way to pass parameters. Or add an external pin and read some voltage, but... Jun 23 '21 at 21:33
• ...that won't work for values, since they will be time-related. In general, comodity comes at a price. In order to have an .inc for .params you need to define the values with {<parameter>}. The advantage of assigning values like this is that you can change them all from one .param statement. The disadvantage is that you need to type. Jun 23 '21 at 21:36