The general question how to add a code model (from chapter 12 of the ngspice manual) to a project may be answered with the following potentiometer example.
For some time now I have planned to add a small series on videos of potentiometer simulation to my video collection at https://www.youtube.com/@holger8105.
So I may take the following as a film script for Part II of the new videos.
What do we need when we want to simulate a potentiometer?
a symbol (may be Device:R_Potentiometer or Device:R_Potentiometer_Trim)
a ngspice model (which should display the total resistance and the wiper position
on the Eeschema canvas, to be easily adapted by the user)
ngspice already offers a code model for the potentiometer (see ngspice manual chapter 12.2.34, changed to 8.2.34 for upcoming ngspice-43). All code models (more than 70 are available) start with letter A on the instance line and require a .model line. As the code model syntax may be complex, and we want to have user adaptable parameters, in my view the best approach is to put the code model instance and its .model into a subcircuit, which then may be attached to the symbol in the usual way, as any other (vendor provided) subcircuit model.
Looking at the symbols, we have three nodes: 1 is the upper resistor connection, 2 the wiper, and 3 the lower resistor connection. Our subcircuit frame takes these nodes and their sequence (1, 2, 3) into account:
* code model for potentiometer
.subckt potia ru wp rl
.ends
Just adding the potentiometer code model (copy from the manual)
* code model for potentiometer
.subckt potia ru wp rl
Apot r0 w r1 potmod
.model potmod potentiometer(position=0.45 r=1k log=FALSE log_multiplier=1)
.ends
and adapting the node names (see the description in the manual) to make them connecting to the .subckt line:
* code model for potentiometer
.subckt potia ru wp rl
Apot rl wp ru potmod
.model potmod potentiometer(position=0.45 r=1k log=FALSE log_multiplier=1)
.ends
We might now attach this model to the symbol, but its not yet what we wanted. Changing the wiper position would require editing the model. Having more than one potentiometer in the circuit with different resistance values and wiper positions would require a different model for each potentiometer.
So lets define at least the total resistance and wiper position as user-adaptable parameters. Therefore we need a change to the .subckt line (adding the parameters) and the .model line (replacing the values by its parameters):
* code model for potentiometer
.subckt potia ru wp rl params: pos=0.45 Rtot=1k
Apot rl wp ru potmod
.model potmod potentiometer(position={pos} r={Rtot} log=FALSE log_multiplier=1)
.ends
Put these lines into a file, e.g. pot.mod, and attach the model to the symbol. The pin sequence is o.k. because we have it defined correctly in the .subckt line.
To make the parameters visible and editable on the Eeschema canvas, we need to do the following: After attaching, open again the “Simulation Model Editor” window. You now see the Parameters pos and Rtot and their Defaults. Copy the Defaults into the Value field, pos with 0.45 and Rtot with 1k. Close the window by o.k. . Back in the “Symbol Properties” window, scroll down to see the new field Sim.Params. Check its “Show” box. Uncheck the “Show” box of the Value field (no relevant information shown). Close the window by o.k. On the canvas, you may move the new text line “pos=0.45 Rtot=1k” to an appropriate position. Double clicking this line allows editing, entering new values for this specific symbol only.