ECALCS - Electronics Calculations - version 210118 ================================================== ecalcs - 64-bit Linux binary (run in a terminal window) ecalcs.sh - shell script for running the Linux version ecalcs.exe - 32-bit Windows Console binary ecalcs.pas - Free Pascal source code ecalcs.txt - this file (view with fixed-width font) This program is a re-write of a Javascript-based calculation program I made years ago for my Sony COM2 PDA, which didn't support "normal" user programming so adapted a Javascript BASIC with an embedded BASIC program. The original is at newton.freehostia.com/com2/isbas.htm, which I still found myself frequently using some 12 years later (it works but has quirks.. have to enter a leading zero before the decimal point and has no scaling other than uF). Recently I found the Free Pascal compiler, so used it to turn these calculations into a simple console app. The program has options for calculations I often need but are tedious to do with a calculator or have obscure formulas I have to look up every time. It does not include simple calculations like ohm's law (V=RI,R=V/I,I=VR) and power (P=IV,P=V^2/R,P=I^2R etc), and usually I also have a calculator app open when doing these calculations as it's still needed for scaling, calculating currents, power dissipation etc. To keep it fairly simple the program only does 9 calculation types, 7 of which are from the original. Caution... use this program at your own risk, not responsible for errors or improper operation. Please report any bugs, comments welcome. Running the program ------------------- For Windows, double-click the ecalcs.exe program (if extensions are hidden, the one with an icon that looks like a program), should launch itself inside of a command window. For Linux, the ecalc binary must be run from inside a terminal such as Xterm or Gnome Terminal. To run from a Ubuntu-like GUI file manager, double-click the ecalcs.sh file and select "Run in Terminal". If the xterm package is installed, then selecting "Run" will run the program in an Xterm window. Using the program ----------------- Here's the initial screen after running... ECALCS - Electronics Calculations - version 210118 by WTN --------------------------------------------------------- At the Select prompt Enter to redisplay menu, Esc to exit C entries accept uF nF and pF divisors (trailing F is ignored) L entries accept mH uH and nH divisors (trailing H is ignored) Other entries accept K M G multipliers (trailing V hz ignored) and also accept ms mV mO us uV uO etc for milli and micro --------------------------------------------------------- 1) Voltage divider/gain 2) Series capacitors 3) Parallel resistors 4) Needed parallel resistor 5) Capacitive reactance 6) Inductive reactance 7) Resonance and air coil 8) Resistance noise 9) Capacitor charge Select: Press 1 through 9 keys for the associated calculation, some options lead to sub-menus to solve for different variables. The menu isn't re-displayed every time to reduce clutter, press enter at the Select: prompt to re-display the menu. Press Esc to exit the program. The entries accept common suffixes. For capacitors farads are assumed unless the entry is followed by u, uF, n, nF, p or pF (letter case doesn't matter). For inductors henries are assumed unless the entry is followed by m, mH, u, uH, n or nH. All other entries (resistors/impedance, voltage, frequency and time) accept K M or G multipliers, if V or HZ is included it is ignored. Voltage entries accept mV and uV for millivolts and microvolts, and time entries accept ms and us for milliseconds and microseconds, these require one character following m or u to keep from mixing up with the M multiplier. Option 1 - Voltage divider/gain calculations -------------------------------------------- Select:1 ----- Voltage divider/gain calculations ----- 1) Voltage divider gain (attenuation) 2) Non-inverting opamp gain 3) Inverting opamp / ratio gain 4) Load Z from series R and voltages Which one? Sub-option 1 computes the gain of a voltage divider or a resistor in series with a load, for example... .------R1(top)------*---> out | | Vsource R2(bottom) Gain = R2/(R1+R2) _|_ _|_ Which one? 1 Enter top R : 20 Enter bottom R : 16 Voltage Gain = 0.444444 (-7.04365dB) ...shows that a 20 ohm resistor in series with a 16 ohm load has a gain of about 0.44 or a loss of about -7db. For a voltage divider, the unloaded output voltage is the gain multiplied by the input voltage. To compute the loaded voltage output then the bottom R entry needs to be the parallel combination of the bottom resistor and the load, see option 3. Sub-option 2 computes the gain of a non-inverting opamp stage. For example... __ | \ .-----------|+ \ | | )--*----> output input .--|- / | _|_ | |__/ R1(out to inv-in) | | `----------* Gain = 1+(R1/R2) | R2(inv-in to gnd) _|_ Which one? 2 Enter R from out to inv-in : 100K Enter R from inv-in to gnd : 33K Voltage Gain = 4.030303 (12.106754dB) This calculation is also handy for calculating the output voltage of common voltage regulators, multiply the gain by the reference voltage, which is usually in the vicinity of 1.22 (the typical bandgap reference voltage). Sub-option 3 computes the gain of an inverting opamp stage, or in general any voltage ratio. For opamps input R is the input resistor to the inverting input and output R is the feedback resistor from the output back to the inverting input. For voltages input V is the stage's input voltage, output V is the stage's output voltage. To convert gain to dB use 1 for the input. For example... .--- R1(input)---*---R2(output)--. | | __ | input | | \ | Gain = R2/R1 _|_ `---|- \ | | )------*-----> output .---|+ / _|_ |__/ Which one? 3 Enter input R or V : 33K Enter output R or V : 100K Voltage Gain = 3.030303 (9.629721dB) For all of the above only the ratio matters, Entering 33 and 100 produce the same result as entering 33K and 100K. Suboption 4 calculates load impedance given the resistance in series with the load and the voltages before and after the resistance, for example... Vsource Vload | | *--- Rseries ---* | | Zload = (Vload*Rseries)/(Vsource-Vload) input Zload _|_ _|_ Which one? 4 Enter series resistance : 15K Enter voltage at source : 1.5 Enter voltage at load : 1.2 Load impedance = 60K Option 2 - Series capacitors ---------------------------- When capacitors are paralleled the total value is simply the sum. When capacitors are wired in series the total capacitance is reduced according to the formula: 1/(1/C1 + 1/C2 [+ 1/Cn]) This calculator will calculate for two or more capacitors in series, enter the capacitor values then press just enter to calculate. For example... ----- Series capacitors ----- Enter all C values then press enter to calculate Enter C: 0.1uF Enter C: 0.047uF (just enter pressed for next prompt) Total series C = 0.031973uF Note that electronically the voltage on the nodes between the capacitors is undefined (and Spice programs will complain), practically so long as the voltage rating of either capacitor is greater than the applied voltage then it's fine, leakage will equalize the voltage. For power supplies, resistors should be included in parallel with the capacitors to define the DC voltage, this is often done to increase the voltage rating. For example a 47uF capacitor rated at 500V in series with a 220uF capacitor rated at 100V produces a 39uF capacitor rated at close to 600V, provided the 47uF is paralleled with two 120K 1W resistors in series, and the 220uF is paralleled with a 47K 1/2W resistor. The actual safe voltage rating would be more like ~560V due to resistor and leakage tolerances but often just need a bit over 500V and want to avoid the much greater cost of a 600V part. Option 3 - Parallel resistors ----------------------------- The total value of resistors in series is simply the sum of the values. The total value of resistors (or impedances) in parallel is reduced according to the formula: 1/(1/R1+1/R2 [+1/Rn]).. the same as for series capacitors but with units scaled for resistors. For example... ----- Parallel resistors ----- Enter all R values then press enter to calculate Enter R: 100K Enter R: 470K Total parallel R = 82.45614K This is by far my most used calculation, not so much for parallel resistors but for calculating effective input impedance and loaded voltage dividers. Option 4 - Needed parallel resistor ----------------------------------- This is a variation of the parallel resistor calculation except that it solves for what value is needed, given an existing value and a target value. For example... ----- Calculate needed parallel resistor ----- Existing R : 100K Desired R : 82K Needed R = 455.555556K Option 5 - Capacitive reactance ------------------------------- These sub-options calculate various forms of the capacitive reactance formula Z=1/(2*pi*C*F). Examples... Select:5 ----- Capacitive reactance ----- 1) Calculate Z from C and F 2) Calculate C from Z and F 3) Calculate F from Z and C Which one? 1 Enter C: 1000uF Enter F: 40 Z = 3.978874 ohms Which one? 2 Enter Z: 4 Enter F: 40 C = 994.718394uF Which one? 3 Enter Z: 4 Enter C: 1000u F = 39.788736 hertz Which one? Select: Also frequently used, particularly when doing rough filter calculations. For most circuits the 6db rolloff point is when the capacitive reactance equals the source impedance (or source+load in the case of a series capacitor), or equals the feedback resistance (at least in the case of an inverting opamp). The 3db point is generally F*1.414 in the case of a series C high-pass filter, or F*0.707 in the case of a parallel C low-pass filter. Option 6 - Inductive reactance ------------------------------ These sub-options calculate various forms of the inductive reactance formula Z=2*pi*L*F. Examples... Select:6 ----- Inductive reactance ----- 1) Calculate Z from L and F 2) Calculate L from Z and F 3) Calculate F from Z and L Which one? 1 Enter L: 100mH Enter F: 5Khz Z = 3.141593K Which one? 2 Enter Z: 3.14K Enter F: 5Khz L = 99.949304mH Which one? 3 Enter Z: 3.14K Enter L: 100mH F = 4997.465213 hertz I don't use these nearly as much as the CFZ calculations but sometimes they're handy for simple crossovers or RF choke calculations. Beware of LC combinations as the resonance will distort the frequency response, for precision speaker crossovers best to use an app made for that. Option 7 - Resonance and air coil calculations ---------------------------------------------- These sub-options compute various forms of the LC resonance formula F=1/(2*pi*sqrt(L*C)). Sub-option 1 also optionally calculates the Q factor if series and/or parallel resistance is entered according to the formulas Qs=(1/Rs)*sqrt(L/C) and Qp=Rp*sqrt(C/L), if both Rs and Rp are entered then the overall Q is calculated as 1/(1/Qs+1/Qp) and both Qs and Qp are reported. For these calculations the following circuits apply... Parallel LC... Series LC... .---Z1--*----*----. .--Z1--*---*---L---*---. | | | | | | | | | | C L | | | `--Rp1--' | signal | | Rp signal Z3 Rs | Rs1 Rs2 | | | .--Rp2--. | | | | | | | | | | `---Z2--*----*----' `--Z2--*---*---C---*---' Rs=Rs1+Rs2 Rp=1/(1/Rp1+1/Rp2) Rs=Rs(L)+Rs(C) The Z impedances don't affect resonance and Q directly but do affect the response. For a parallel LC pass filter (tuned circuit), Z1 represents the source impedance, Z2 is generally 0 (ground), and Rp represents the load. For a parallel LC notch filter, Z1 represents the source impedance and Z2 represents the load. For a series LC pass filter (tuned circuit), Z1 represents the source, impedance, Z2 represents the load impedance, and Z3 represents the bypass impedance (response outside of the pass band). For a series LC notch filter, Z1 represents the load impedance, Z2 is generally 0 (ground) and Z3 represents the load. Examples... Select:7 ----- LC Resonance ----- 1) Calculate F Z Q from L C R 2) Calculate L from F and C 3) Calculate C from F and L 4) Air Coil Calculator Which one? 2 Enter F: 61.25mhz Enter C: 50pF L = 135.038645nH Which one? 3 Enter F: 61.25mhz Enter L: 135nH C = 50.014313pF Which one? 1 Enter L: 135nH Enter C: 50pF Enter Rser (optional): 0.1 Enter Rpar (optional): 1K F = 61.258766 Mhz Q = 18.557687 (Qser=519.615242 Qpar=19.245009) Zcomponent = 51.961524 ohms Zresonance(parallel LC) = 964.285714 ohms Zresonance(series LC) = 2.8 ohms Option 1 also computes the characteristic impedance (simply the capacitive or inductive reactance which are equal at resonance), and if series and/or parallel resistances are entered, then computes the circuit Q and impedance at resonance for parallel and series combinations. These calculations don't directly predict the frequency response (best to use something like LTspice for that) but can assist in estimations, for example for a notch filter the notch depth can be computed using the voltage divider calculator with the parallel resonance impedance as the top resistor and the load as the bottom and the result agrees with Spice. Calculating the notch width is trickier as the bandwidth implied by Q is in relation to maximum notch depth, not the overall -3db points of the filter which in addition to being influenced by Q, is also affected by the characteristic impedance Zcomponent.. the lower Zcomponent is compared to the circuit impedance then the flatter the filter will be up to and past the stop band. The air coil calculator is based on a popular formula for approximating the inductance of an air-core coil... L(uH) ~= (Nturns*Diameter)^2 / (18*Diameter + 40*Length) ...where Diameter is the outside diameter of the coil in inches, and Length os the outside length in inches. Note that this formula is known to be somewhat inaccurate, particularly with smaller coils, but it seems to (mostly) agree with other coil calculators, at least when generalized to just these few variables. Examples... Which one? 4 ----- Air-core Coil Calculator ----- Note - approximation only, not that accurate Formula L(uH)=(Turns*Dia)^2/(18*Dia+40*Len) units=inch 1) Solve for inductance 2) Solve for number of turns 3) Solve for diameter 4) Solve for length 5) Units and scale factor Which one? 4 Inductance (add uH etc) : 135nH Number of turns : 9 Diameter in inch : 0.25 Length = 0.825 inch .... Which one? 2 Inductance (add uH etc) : 135nH Diameter in inch : 0.25 Length in inch : 0.825 Number of turns = 9 .... Which one? 3 Inductance (add uH etc) : 135n Number of turns : 9 Length in inch : 0.825 Diameter = 0.25 inch .... Which one? 1 Diameter in inch : 0.25 Length in inch : 0.825 Number of turns : 9 Inductance = 135nH .... Which one? 1 Diameter in inch : 0.255 Length in inch : 0.7 Number of turns : 9 Inductance = 161.614759nH .... Which one? 1 Diameter in inch : 0.245 Length in inch : 0.95 Number of turns : 9 Inductance = 114.643362nH The last two examples show the effect of stretching or compressing the coil length, often used to tune coils. It also that even if these calculations were exact (which they are not), minor construction differences can have a large effect.. even just increasing the diameter from 0.25" to 0.27" in the example changes the inductance from 135nH to 156nH. Sub-option 5 permits changing the units and scaling factors... Which one? 5 Units=inch DIf=1.016 LEf=1.016 1) Toggle units 2) New DIf diameter factor 3) New LEf length factor Which one? 1 Formula L(uH)=(Turns*Dia)^2/(45.72*Dia+101.6*Len) units=cm ... Which one? 5 Units=cm DIf=1.016 LEf=1.016 1) Toggle units 2) New DIf diameter factor 3) New LEf length factor Which one? 2 Enter new DIf: 1 Formula L(uH)=(Turns*Dia)^2/(45*Dia+101.6*Len) units=cm ... Which one? 5 Units=cm DIf=1 LEf=1.016 1) Toggle units 2) New DIf diameter factor 3) New LEf length factor Which one? 3 Enter new LEf: 1 Formula L(uH)=(Turns*Dia)^2/(45*Dia+100*Len) units=cm ...which puts it back to the original cm formula I used. Likely doesn't matter that much if 1 or 1.016, the change is within this calculation's error range, but might be handy for calibrating the equation for a particular application. The changed factors are temporary and reset back to the hard-coded values when rerunning the coil calculator. Option 8 - Resistance noise --------------------------- This option calculates the thermal noise of a perfect resistor at a specified temperature over a specified frequency bandwidth. All resistance produces white noise equal to the sqrt(4*K*T*F*R) where K=Boltzmann's constant (about 1.38065E-23), T is the temperature in Kelvin, F is the frequency range in hz, and R is the resistance in ohms. The noise has equal power per frequency range so is more concentrated at high frequencies, often heard as hiss in high-gain amplifier circuits. The temperature entry defaults to Kelvin, add a C or F suffix for Celcius or Fahrenheit. For example... ----- Calculate noise voltage ----- Enter resistance: 4.7K Enter temp in K (or add C or F): 25C Enter bandwidth (20K for audio): 20K Noise = 1.244094 microvolts (uV) That's not much but ~1uV of noise ran through 3 12AX7 gain stages (a gain of roughly 100,000 at a high gain setting) produces 100mV of noise referenced to a ~100V signal.. or about -60dB, not too bad. A more common 68K input resistor produces about 3 times/10dB as much noise. Option 9 - Capacitor charge --------------------------- These calculations predict how long it will take for a capacitor to charge to a particular voltage from a particular supply voltage and series resistance, according to variations of the formula T = -log((Vs-Vc)/Vs)*R*C, where T is the time in seconds, Vs is the (effective) supply voltage, Vc is the voltage across the capacitor, R is the (effective) series resistance in ohms, and C is the capacitor value in farads. The formulas were adapted from one of Bill Bowden's calculators, now at: http://www.bowdenshobbycircuits.info/rc.htm Here's a schematic for the basic charge calculation... .----R----* <---Vc(T) | | Vs(T=0) C T = -log((Vs-Vc)/Vs)*R*C _|_ _|_ Here's a schematic showing something closer to a real-world app... .---Rsup---*----*-----------. <--Vc(T) Vs = Vsup*((Rsup+Rload)/Rload) | | _|_ | Vc = zener voltage Vsup(T=0) C /_\ zener Rload R = 1/(1/Rsup+1/Rload) _|_ _|_ _|_ _|_ T = -log((Vs-Vc)/Vs)*R*C For this application, say I have a 15V zener diode regulator fed from a 50V power supply. Estimated 15V load current is 10ma so that's an effective load of R=E/I = 1.5K. To make sure has sufficient current and won't fall out of regulation, set the input current to say 15ma so to drop 35V the series resistance should be 2.333K, say 2.2K (current=15.91ma). The effective supply voltage is determined by the 2.2K 1.5K divider, which has a gain of 0.405405 so Vs ~= 20.27V. The effective resistance is (about) the load in parallel with the series resistor (ignoring non-linear effects), or about 891.89 ohms. The circuit should be fully charged to 15V within (say) 300ms so to find a value of C that satisfies that... ----- Capacitor charge calculations ----- Vs = Absolute supply voltage Vc = Absolute capacitor voltage T = Time (default seconds unless ms or us appended) R = Series resistance (default ohms unless K or M) C = Capacitance (default F unless u n or p) 1) Calculate T from Vs Vc R C 2) Calculate R from Vs Vc T C 3) Calculate C from Vs Vc T R 4) Calculate Vc from Vs T R C 5) Calculate Vs from Vc T R C Which one? 3 Enter Vs: 20.27 Enter Vc: 15 Enter T : 300ms Enter R : 891.89 C = 249.69301uF ...so 220uF it is.. double checking... 1) Calculate T from Vs Vc R C 2) Calculate R from Vs Vc T C 3) Calculate C from Vs Vc T R 4) Calculate Vc from Vs T R C 5) Calculate Vs from Vc T R C Which one? 1 Enter Vs: 20.27 Enter Vc: 15 Enter R : 891.89 Enter C : 220uF T = 0.264325 seconds To check how long it takes to charge up unloaded... Which one? 1 Enter Vs: 50 Enter Vc: 15 Enter R : 2.2K Enter C : 220uF T = 0.172631 seconds The non-linear effect of opamps being mostly constant-current will lengthen the time-to-charge, calculating that is a lot trickier. These calculations are useful for getting in the ballpark but load effects have to also be considered. To estimate load current effects (and to verify the basic calculations) I used the LTspice program to simulate the zener regulator with four different simulated loads... No load, time to 15V = ~173ms (basic calcs are correct) 1.5K load, time to 15V = ~264ms (effective V/R calcs are correct) 10ma pure constant-current load, time to 15V = ~367ms 5ma 2N3819 current load paralleled with 3K, time to 15V = ~303ms Therefore, the turn-on time with a purely constant-current load is approximately the predicted resistive time multiplied by 1.39. The last load is more representative of real circuits, in this case half resistive and half constant-current with a threshold of a couple volts using a simulated 2N3819 N-channel JFET with a 213.3 ohm source resistor to set the current to 5ma. For this load the turn-on time is only increased by about 15%. Compile notes ------------- The ecalcs Linux binary was compiled using Free Pascal 3.0.4 running on 64-bit Ubuntu 20.04 using the command line: fpc -XX -Sx ecalcs.pas The ecalcs.exe Windows binary was cross-compiled using the i386 (Win32) version of Free Pascal 3.2.0 running on the same system using the command line: wine fpc.exe -Sx ecalcs.pas (-XX made no difference and -WC was not necessary to generate a Windows console app). Change history -------------- 201222 Initial translation from isbas.htm, options 3-9 translated mostly as-is with additional code for units (uF K etc). Options 1 and 2 are new, replacing old shop-specific calcs. 201223 Modified option 1 to also calc opamp/ratio gain/db. Simplified/improved main menu logic. 201224 Modified options 2 and 3 to use crt instead of ANSI. Changed title.. some of these aren't all that common. 201226 Modified option 8 to accept K C or F temperature. Modified EnterN so it takes any form of m? u? suffix. Added trim to [x]string functions to suppress leading spaces. 201227 Changed break for Khz prints from 1E5 to 1E4. Cleaned up some code and comments, added license. 201230 Added the air coil calculator to option 7. 210101 Added optional Q calculations to option 7 sub-option 1. Improved constant for option 7 sub-options 2 and 3. Options 5 6 7 and 9 now work like sub-menus rather than returning to the main select prompt after every calculation. Fixed entry functions so space is tolerated between value and suffix. 210105 Added LCR impedance calculations to option 7 sub-option 1. 210118 Added load impedance calculation to option 1. ----------------------------------- Terry Newton (wtn90125@yahoo.com)