2
\$\begingroup\$

I am trying to understand how LTspice works. I copied a file from a forum, but it bothers me that there is a calculation error in it. The diode current is not the same, even if I leave out the Rs value and capacitors.

The error starts at the calculation of Ut. The .param lines are on the left side. In the error log you can see the parameters. If you calculate Ut with it there is an small error.

k = 1.38065·10-23
T = 300.15
q = 1.60218·10-19

Excel says: 0.02586489
LTspice says: 0.0258641

Is it because of this Newton iteration? I tried to have maximum accuracy with as little tolerance as possible in the settings, but it doesn't change anything.

Schematic

Version 4
SHEET 1 1428 680
WIRE 32 144 -16 144
WIRE 176 144 32 144
WIRE 688 144 512 144
WIRE -16 176 -16 144
WIRE 176 176 176 144
WIRE 512 176 512 144
WIRE 688 176 688 144
WIRE -16 288 -16 256
WIRE 176 288 176 240
WIRE 176 288 -16 288
WIRE 512 288 512 256
WIRE 688 288 688 256
WIRE 688 288 512 288
WIRE -16 320 -16 288
WIRE 512 320 512 288
FLAG -16 320 0
FLAG 512 320 0
FLAG 32 144 U
DATAFLAG 144 144 ""
DATAFLAG 160 96 "I(D1)"
DATAFLAG 656 96 "I(B1)"
DATAFLAG 624 144 ""
SYMBOL voltage -16 160 R0
SYMATTR InstName V1
SYMATTR Value 0.6
SYMBOL diode 160 176 R0
SYMATTR InstName D1
SYMATTR Value 1N914
SYMBOL bi 512 256 M180
WINDOW 0 24 80 Left 2
WINDOW 3 34 -54 Left 2
SYMATTR InstName B1
SYMATTR Value I = Is * (exp(V(U)/(N*Ut))-1)
SYMBOL res 672 160 R0
SYMATTR InstName R1
SYMATTR Value 1
TEXT -40 -152 Left 2 !.model 1N914 D(Is=2.52n N=1.752 tt=20n Iave=200m Vpk=75 mfg=OnSemi type=silicon)
TEXT -56 -96 Left 2 !.param Is=2.52n N=1.752 Rs=0.568\n.param k=boltz \n.param q=echarge\n.param T=TEMP-kelvin\n.param Ut = k*T/q
TEXT -56 -136 Left 2 !.op
TEXT 504 -96 Left 2 !.meas k_ param k\n.meas T_ param T\n.meas q_ param q\n.meas Ut_ param Ut
TEXT -48 -320 Left 5 ;I = Is * (exp(U/(N*Ut))-1)
TEXT -56 -248 Left 2 ;The small difference of Id between the diode and the formula is due to the missing series resistance Rs in the formula above.
TEXT 32 80 Left 2 ;I =
TEXT 528 80 Left 2 ;I =
TEXT 792 -96 Left 2 ;Check k, T, q and Ut in the log-file.\nView -> SPICE Error Log
\$\endgroup\$
5
  • \$\begingroup\$ Because it tells me that in the error log. If I use more accurate constants I still don't get LTSpice results. \$\endgroup\$
    – J K
    Commented Apr 28, 2023 at 6:45
  • 1
    \$\begingroup\$ Hmmm...have you tried doing a .meas directly on echarge and boltz using the curly braces? I just tried that and the values it spits out are completely different. I did find a close enough match for them within this source code file. I believe these are ancient definitions for these constants. Even this source file which references physics.nist.gov uses old constant definitions which don't match the website. \$\endgroup\$
    – Ste Kulov
    Commented Apr 28, 2023 at 7:32
  • \$\begingroup\$ So thanks a lot for your help. But don't think that neither k or q are a global constant and that they get overwritten. (If so the line .param Test=k should work) Used them again with your accurate numbers and suceeded: I also get the same result it seems. It seems the internal constants : echarge and boltz are not used in the Diode Simulation, are deprecated/old and are not used anymore. But didn't expect that at all. (Off topic: I used curly brackets to declare it a changable value which is used in a calculation. Read somewhere that that can also be a source for errors) Thanks again for your \$\endgroup\$
    – J K
    Commented Apr 28, 2023 at 14:09
  • \$\begingroup\$ Please ask for clarification of an answer as a comment. This doesn't look like an answer, but like a comment to another answer. If you have a better answer, feel free to reformat this so it'd be clear that it answers the question rather than being a commentary on another answer. \$\endgroup\$ Commented Apr 28, 2023 at 14:25
  • \$\begingroup\$ I think you misunderstood my answer. I said they are "sort of" like global parameters, where they only come to the surface when doing a .meas. Yes, you can do what you did here (and that's what I did too when I first troubleshooted), but it's very confusing because let's say you .param-ed k and q as a value with less significant digits. Then when you do a .meas, it will use the internal values and NOT the values in your .param. Do you see the problem now? If you don't believe me, then change your .param's to something bogus and see how the .meas don't change. \$\endgroup\$
    – Ste Kulov
    Commented Apr 28, 2023 at 14:35

1 Answer 1

5
\$\begingroup\$

It looks like you're accidentally trying to reuse pre-defined built-in parameter names. Although this section of the Unofficial LTspice Wiki lists out boltz and echarge, it looks like k and q are also pre-defined within the program (sort of)...and are also set to completely different values! Here's a .meas printout of those four values:

echarge_: 1.6021917e-019=1.6021917e-019
boltz_: 1.38062e-023=1.38062e-023
q_: q=1.602176462e-019
k_: k=1.3806503e-023

Since both LTspice and ngspice share the same codebase from SPICE3, I found where these are defined in the ngspice source code.

echarge and boltz seem to line up best here:

https://sourceforge.net/p/ngspice/ngspice/ci/master/tree/src/include/ngspice/numconst.h

#define  CHARGE                 1.602191770e-19     /* C */
#define  BOLTZMANN_CONSTANT 1.38062259e-23      /* J/oK */

while q and k seem to go with this:

https://sourceforge.net/p/ngspice/ngspice/ci/master/tree/src/include/ngspice/const.h

/* https://physics.nist.gov/cgi-bin/cuu/Value?e
 *                value = 1.602 176 6208 x 10-19 C
 * standard uncertainty = 0.000 000 0098 x 10-19 C */
#define CHARGE 1.6021766208e-19

/* https://physics.nist.gov/cgi-bin/cuu/Value?k
 *                value = 1.380 648 52 x 10-23 J K-1
 * standard uncertainty = 0.000 000 79 x 10-23 J K-1 */
#define CONSTboltz 1.38064852e-23

They even reference physics.nist.gov, but it looks like ngspice's definitions for q and k seem to be out of date while LTspice's actually live in this century. echarge and boltz are even worse, but I don't think either program uses them for any internal calculations.


Here's where it gets tricky. In LTspice, q and k aren't actually normal pre-defined parameters. For some strange reason they only become valid and accessible to the user when doing a .meas statement with them. So you can do .meas on them, but you can't use them within .param statements or behavioral source expressions...unless you .param them yourself first of course. But then...if you .param them yourself, they'll take on completely different values when used in a .meas because the internal pre-defined versions of q and k will override yours. Yikes! What a mess.

I think the best thing to do is avoid using the names q and k completely. If you want to match LTspice's internal calculations, you should define your own unique constant names and set them to the values spat out by the .meas printout for the internal q/k (posted above). Also, you might need to set .options measdgt and/or .options numdgt depending on what accuracy you need.

Here's an example of what I mean. Your currents are equal now between both circuits.

enter image description here


\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.