Just use the G circuit element (voltage controlled current source) with a lookup table (LUT) specification:
Note: the capacitor C1 is there only to avoid an error because the simulator doesn't like node C to be floating.
This is the relevant section of the online help (emphasis mine):
G. Voltage Dependent Current Source Symbol Names: G, G2
There are three types of voltage dependent current-source circuit
elements.
Syntax: Gxxx n+ n- nc+ nc-
This circuit element asserts an output current between the nodes n+
and n- that depends on the input voltage between nodes nc+ and nc-.
This is a linearly dependent source specified solely by a constant
gain.
Syntax: Gxxx n+ n- nc+ nc- table=(, , ...)
Here a lookup table is used to specify the transfer function. The
table is a list of pairs of numbers. The second value of the pair is
the output current when the control voltage is equal to the first
value of that pair. The output is linearly interpolated when the
control voltage is between specified points. If the control voltage is
beyond the range of the look-up table, the output current is
extrapolated as a constant current of the last point of the look-up
table.
Here are the results of the simulation:
As you can see you only need to specify two points in the LUT if you just want a VCCS with an hard limiting characteristics, i.e. linear inside a given voltage range and fixed saturated limit out of that range.
Oh, cripes. I can't believe the answers.
Here.
Hit F2, find "bv", drop it on the schematic, fill out the formula, hook it up somewhere. Done. Doesn't even have to be grounded. You can stick it anywhere. Simple formula, too. Couldn't be easier.
Of course, this assumes you've labeled your two sources as indicated. If they are unlabeled and just node numbers, then use the node names instead.
Per other discussion in comments, I may as well memorialize them here:
The B source is a type of nonlinear controlled source that was added to open the door to pretty much any function you might want, covering pretty much all possible cases. Starting with Spice3, they allowed a V= (for voltage) or an I= (for current.) Most elementary math functions are allowed and you can use node voltages and terminal currents in the expression. The expression is assumed by Spice to be mathematically continuous.
These pretty much had to be added to make it more convenient to create behavioral functions needed for specialized ICs or IC subfunctions, like VCOs. LTSpice has also created (for itself) a number of very much faster internal capabilities that they use to model their own ICs, so that simulating their ICs is faster and better than simulating the ICs of other manufacturers, even if you have the behavioral models for them (because using the standard behavioral B sources in your models does make them slower than if you write specialized computer code to handle your own needs.)
An E source can also be used to duplicate your function, using the "value=" syntax. But as the help in LTSpice says, "This is an alternative syntax of the behavioral source, arbitrary behavioral voltage source, B."
Different Spice implementations are faster or slower handling these, as there are optimizations that can be applied and may be, by some developers. But the B source is generally slowish (but not necessarily noticeably so.) E sources with "value=" aren't much different (obviously.) From the LTSpice help on the E source, "It is better to use a G source shunted with a resistance to approximate an E source than to use an E source. A voltage controlled current source shunted with a resistance will compute faster and cause fewer convergence problems than a voltage controlled voltage source. Also, the resultant nonzero output impedance is more representative of a practical circuit."
In general, using Spice well as a designer tool takes training and time and experience. There is, however, more than one goal in using it. If you are a developer of models for ICs working for an IC manufacturer, you probably need to know Spice extremely well to do the job well. If you are using it more as an end user of ICs and electronic parts, then your needs may be more about verifying that you didn't forget something important (Spice will often "kick you in the head," by offering you surprising results.) You often can avoid knowing too much about using Spice, unless you encounter a very slow convergence or a cryptic error message. Then you are forced to learn something more.
I'm not worried about using a B source like the OP requires. It will almost certainly converge easily. But this does depend (of course) on the rest of the circuit and the referenced nodes' behaviors. So nothing is for sure about this. But it's probably safe for the OP and probably fast enough for their needs. But if there is a problem, there is almost always a way to address it.
Best Answer
Voltage Spike's answer will solve your problem if, as vangelo hints, there is no instantaneous feedback (quote from the help, under
LTspice > Circuit Elements > B. Arbitrary Behavioral Voltage or Current Sources
, emphasis mine):Otherwise -- and since I doubt you'll add more details -- there is a way that allows direct feedback. It looks complicated but it's not, it's very fast, but it does come at a cost:
The actual circuit is formed by:
Vi
= current sensorS1
= the variable resistanceH1, G2, C1, S2, I1
= the control circuitV1
andL1
make a random circuit. Despite the fact that it has 7 elements, it performs very fast, because it's linearized.I1
generates a 1 A DC current throughS2
, which means the voltage across it,V(R)
, is 1:1 proportional to its resistance.Vi
takes the current throughS1
and compares it withV(R)
, then feeds it through the PI filter made ofG2
andC1
. The resulting difference (the error voltageV(ctl)
), will be such thatV(R)
followsI(Vi)
, which means usingV(ctl)
onS1
will makeS1
have a resistance 1:1 proportional to the current through it. The plots showI(Vi)
andV(R)
, side by sude, and they are a match. One advantage is that if you need other resistors to vary just likeS1
, all you need is to add one VCSW for each resistor needed.But, it's not perfect:
.model
card which defines its ON and OFF resistances, whose values are the direct limits of how low or high it can vary. You may be tempted to useRon=1f
andRoff=1T
, but if the ratio between them is greater than 1012, that's a recipe for disaster.Ron=1u Roff=1meg
should be the highest range used; anything else means you're on your own. Even so, a resistance that varies between 1 μΩ and 1 MΩ should suffice in most cases.V1
generates a 1 Hz signal which, compared to the 1 nF and 1 Ω values for the loop filter, means the errors are very low. But the errors are inveresely proportional to the ratio between these values and the maximum frequency the resistor is supposed to work with. Here is how the differences betweenV(i)
(voltage representingI(Vi)
, 1:1) andV(R)
look like when the values forC1
are 10 μF (black), 100 μF (blue), and 1 mF (red):Therefore, try to keep the bandwidth at least 1...10 thousand times more than the maximum frequency of interest (i.e. if fmax = 1 kHz then keep
C1
below 100...10 nF). If you want to play with it, below is the source of the schematic. Save it as.asc
, but not.asc.txt
: