main
Caleb
0.4.0: wiki page runs the live calculator (README.html loads the current source from the page repo), UI static mode, sh…
fc3ee8f
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
/* hwcalc — hardware design calculations. One module shared by the browser UI, the HTTP API and the CLI.
* Every calculator names the formula and the reference it implements. Keep them honest: no fudge factors. */
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.HWCALC = factory();
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';
// ---------------------------------------------------------------- number parsing / formatting
const SI = { f: 1e-15, p: 1e-12, n: 1e-9, u: 1e-6, 'µ': 1e-6, m: 1e-3, k: 1e3, K: 1e3, M: 1e6, G: 1e9 };
const UNIT_SUFFIX = /(ohms?|Ω|Hz|hz|F|H|V|A|W|s|°C|C)$/;
/** Parse "4k7", "10u", "2.2M", "100nF", "1e-3". si=false means a plain number (mm, %, AWG...). */
function parseNum(raw, si = true) {
if (raw === null || raw === undefined) return NaN;
if (typeof raw === 'number') return raw;
let s = String(raw).trim().replace(/,/g, '');
if (s === '') return NaN;
if (!si) return Number(s);
s = s.replace(UNIT_SUFFIX, '').trim();
const mm = s.match(/^([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*([fpnuµmkKMG]?)(\d*)$/);
if (!mm) return Number(s);
let [, num, pre, tail] = mm;
if (tail && pre) num = num + '.' + tail; // 4k7 -> 4.7k
else if (tail) return NaN;
return Number(num) * (pre ? SI[pre] : 1);
}
const PREFIXES = [[1e9, 'G'], [1e6, 'M'], [1e3, 'k'], [1, ''], [1e-3, 'm'], [1e-6, 'µ'], [1e-9, 'n'], [1e-12, 'p'], [1e-15, 'f']];
function fmtSI(v, unit = '', digits = 4) {
if (v === null || v === undefined || Number.isNaN(v)) return '—';
if (!Number.isFinite(v)) return (v > 0 ? '∞' : '−∞') + (unit ? ' ' + unit : '');
if (v === 0) return '0' + (unit ? ' ' + unit : '');
const a = Math.abs(v);
let [scale, p] = PREFIXES.find(([s]) => a >= s * 0.9995) || PREFIXES[PREFIXES.length - 1];
const n = v / scale;
return `${trim(n.toPrecision(digits))} ${p}${unit}`.trim();
}
function fmtN(v, unit = '', dp = 3) {
if (v === null || v === undefined || Number.isNaN(v)) return '—';
if (!Number.isFinite(v)) return '∞';
return `${trim(v.toFixed(dp))}${unit ? ' ' + unit : ''}`;
}
const trim = s => s.includes('e') ? s : s.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '');
// ---------------------------------------------------------------- E-series (IEC 60063)
const E_BASE = {
E3: [1.0, 2.2, 4.7],
E6: [1.0, 1.5, 2.2, 3.3, 4.7, 6.8],
E12: [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2],
E24: [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1],
};
for (const n of [48, 96, 192]) { // E48+ follow round(10^(i/n), 3 s.f.), one documented exception
const a = []; for (let i = 0; i < n; i++) a.push(Math.round(Math.pow(10, i / n) * 100) / 100);
if (n === 192) a[a.indexOf(9.19)] = 9.20; // IEC 60063: 919 is listed as 920
E_BASE['E' + n] = a;
}
const TOL = { E3: '>20%', E6: '20%', E12: '10%', E24: '5%', E48: '2%', E96: '1%', E192: '0.5%' };
/** Nearest E-series values around v: {below, above, nearest}. */
function eNearest(v, series = 'E24') {
const base = E_BASE[series] || E_BASE.E24;
if (!(v > 0)) return { below: NaN, above: NaN, nearest: NaN };
const dec = Math.floor(Math.log10(v) + 1e-12), m = v / Math.pow(10, dec);
const cand = [];
for (const d of [dec - 1, dec, dec + 1]) for (const b of base) cand.push(+(b * Math.pow(10, d)).toPrecision(4));
cand.sort((a, b) => a - b);
let below = cand.filter(c => c <= v * (1 + 1e-9)).pop(), above = cand.find(c => c >= v * (1 - 1e-9));
const nearest = (Math.abs(Math.log(v / below)) <= Math.abs(Math.log(above / v))) ? below : above;
return { below, above, nearest };
}
function eValuesInRange(lo, hi, series) {
const base = E_BASE[series] || E_BASE.E24, out = [];
for (let d = Math.floor(Math.log10(lo)) - 1; d <= Math.ceil(Math.log10(hi)) + 1; d++)
for (const b of base) { const x = +(b * Math.pow(10, d)).toPrecision(4); if (x >= lo && x <= hi) out.push(x); }
return out.sort((a, b) => a - b);
}
// resistor colour code (IEC 60062)
const COLOURS = ['black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'grey', 'white'];
function colourBands(v, sig) {
if (!(v > 0)) return [];
const exp = Math.floor(Math.log10(v) + 1e-9) - (sig - 1);
const digits = String(Math.round(v / Math.pow(10, exp))).padStart(sig, '0').slice(0, sig).split('').map(Number);
const mult = exp >= 0 ? COLOURS[exp] : (exp === -1 ? 'gold' : exp === -2 ? 'silver' : '?');
return [...digits.map(d => COLOURS[d]), mult];
}
// EIA-96 SMD code for E96 values
const EIA96_MULT = { '-3': 'Z', '-2': 'Y', '-1': 'X', '0': 'A', '1': 'B', '2': 'C', '3': 'D', '4': 'E', '5': 'F' };
function eia96(v) {
const exp = Math.floor(Math.log10(v) + 1e-9) - 2, three = Math.round(v / Math.pow(10, exp));
const idx = E_BASE.E96.findIndex(b => Math.round(b * 100) === three);
return idx < 0 ? null : String(idx + 1).padStart(2, '0') + (EIA96_MULT[String(exp)] || '?');
}
function smd3(v) { // 3-digit code (E24, 5%): 472 = 4.7k; R for decimal < 10
if (v < 10) return String(v).replace('.', 'R').slice(0, 3);
const exp = Math.floor(Math.log10(v) + 1e-9) - 1; return String(Math.round(v / Math.pow(10, exp))).padStart(2, '0') + exp;
}
// ---------------------------------------------------------------- helpers
const PI = Math.PI;
const par = (a, b) => (!Number.isFinite(a) ? b : !Number.isFinite(b) ? a : (a * b) / (a + b));
const out = (label, value, unit, opt = {}) => ({ label, value, unit, ...opt });
const need = (v, keys) => keys.filter(k => !(Number.isFinite(v[k])));
const RHO_CU = 1.724e-8, ALPHA_CU = 0.00393; // Ω·m at 20 °C, /°C (annealed copper)
const OZ_UM = 34.79; // 1 oz/ft² copper ≈ 34.79 µm (1.37 mil)
// ---------------------------------------------------------------- calculators
const CALCS = [];
const def = c => CALCS.push(c);
def({ id: 'ohms', group: 'Basics', title: "Ohm's law & power",
blurb: 'Enter any two of V, I, R, P; the other two are solved.',
inputs: [
{ key: 'V', label: 'Voltage', unit: 'V', def: '5' },
{ key: 'I', label: 'Current', unit: 'A', def: '10m' },
{ key: 'R', label: 'Resistance', unit: 'Ω', def: '' },
{ key: 'P', label: 'Power', unit: 'W', def: '' }],
formula: 'V = I·R, P = V·I = I²R = V²/R', ref: 'Ohm / Joule',
compute(v) {
const have = ['V', 'I', 'R', 'P'].filter(k => Number.isFinite(v[k]));
if (have.length !== 2) return { error: 'Enter exactly two of V, I, R, P (leave the other two blank).' };
let { V, I, R, P } = v; const h = have.join('');
if (h === 'VI') { R = V / I; P = V * I; } else if (h === 'VR') { I = V / R; P = V * V / R; }
else if (h === 'VP') { I = P / V; R = V * V / P; } else if (h === 'IR') { V = I * R; P = I * I * R; }
else if (h === 'IP') { V = P / I; R = P / (I * I); } else if (h === 'RP') { V = Math.sqrt(P * R); I = Math.sqrt(P / R); }
return { outputs: [out('Voltage', V, 'V'), out('Current', I, 'A'), out('Resistance', R, 'Ω'), out('Power', P, 'W')] };
} });
def({ id: 'divider', group: 'Basics', title: 'Voltage divider',
blurb: 'Analyse a divider (optional load), or design one from standard values.',
inputs: [
{ key: 'mode', label: 'Mode', type: 'select', options: ['analyse', 'design'], def: 'analyse' },
{ key: 'Vin', label: 'Input voltage', unit: 'V', def: '12' },
{ key: 'R1', label: 'R1 (top)', unit: 'Ω', def: '10k', show: 'analyse' },
{ key: 'R2', label: 'R2 (bottom)', unit: 'Ω', def: '3k3', show: 'analyse' },
{ key: 'RL', label: 'Load on output (blank = none)', unit: 'Ω', def: '', show: 'analyse' },
{ key: 'Vout', label: 'Target output', unit: 'V', def: '3.3', show: 'design' },
{ key: 'Rtot', label: 'Approx. total R1+R2', unit: 'Ω', def: '100k', show: 'design' },
{ key: 'series', label: 'E-series', type: 'select', options: ['E6', 'E12', 'E24', 'E48', 'E96', 'E192'], def: 'E24', show: 'design', setting: 'e_series' }],
formula: 'Vout = Vin · (R2‖RL) / (R1 + R2‖RL), Rout = R1‖R2', ref: 'Kirchhoff',
compute(v) {
if (v.mode === 'design') {
const miss = need(v, ['Vin', 'Vout', 'Rtot']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
if (!(v.Vout > 0 && v.Vout < v.Vin)) return { error: 'Target must be between 0 and Vin.' };
const rows = [];
for (const R2 of eValuesInRange(v.Rtot / 50, v.Rtot * 5, v.series)) {
const R1 = eNearest(R2 * (v.Vin - v.Vout) / v.Vout, v.series).nearest;
const Vo = v.Vin * R2 / (R1 + R2), tot = R1 + R2;
if (tot < v.Rtot / 3 || tot > v.Rtot * 3) continue;
rows.push({ R1, R2, Vo, err: (Vo - v.Vout) / v.Vout, tot });
}
rows.sort((a, b) => Math.abs(a.err) - Math.abs(b.err) || Math.abs(Math.log(a.tot / v.Rtot)) - Math.abs(Math.log(b.tot / v.Rtot)));
const best = rows.slice(0, 6);
if (!best.length) return { error: 'No pair found; widen the total resistance.' };
return { outputs: [out('Best R1', best[0].R1, 'Ω'), out('Best R2', best[0].R2, 'Ω'), out('Output', best[0].Vo, 'V'), out('Error', best[0].err * 100, '%', { fmt: 'n', dp: 3 })],
table: { head: ['R1', 'R2', 'Vout', 'Error', 'Divider current'], rows: best.map(r => [fmtSI(r.R1, 'Ω'), fmtSI(r.R2, 'Ω'), fmtSI(r.Vo, 'V'), fmtN(r.err * 100, '%', 3), fmtSI(v.Vin / r.tot, 'A')]) },
notes: [`${v.series} (${TOL[v.series]} tolerance). Resistor tolerance adds up to ±${TOL[v.series]} of worst-case error on top of the nominal error shown.`] };
}
const miss = need(v, ['Vin', 'R1', 'R2']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
const R2e = par(v.R2, Number.isFinite(v.RL) ? v.RL : Infinity);
const Vo = v.Vin * R2e / (v.R1 + R2e), I1 = v.Vin / (v.R1 + R2e);
const Vnl = v.Vin * v.R2 / (v.R1 + v.R2);
const o = [out('Output voltage', Vo, 'V'), out('Ratio', Vo / v.Vin, '', { fmt: 'n', dp: 5 }), out('Current through R1', I1, 'A'),
out('Power R1', I1 * I1 * v.R1, 'W'), out('Power R2', (Vo * Vo) / v.R2, 'W'), out('Output (Thevenin) resistance', par(v.R1, v.R2), 'Ω')];
const notes = [];
if (Number.isFinite(v.RL)) notes.push(`Unloaded output would be ${fmtSI(Vnl, 'V')}; the load pulls it ${fmtN((Vo - Vnl) / Vnl * 100, '%', 2)}.`);
return { outputs: o, notes };
} });
def({ id: 'led', group: 'Basics', title: 'LED series resistor',
inputs: [
{ key: 'Vs', label: 'Supply voltage', unit: 'V', def: '5' },
{ key: 'Vf', label: 'LED forward voltage', unit: 'V', def: '2.0' },
{ key: 'n', label: 'LEDs in series', unit: '', def: '1', si: false },
{ key: 'If', label: 'LED current', unit: 'A', def: '5m' },
{ key: 'series', label: 'E-series', type: 'select', options: ['E12', 'E24', 'E96'], def: 'E24', setting: 'e_series' }],
formula: 'R = (Vs − n·Vf) / If', ref: 'Ohm; use the datasheet Vf at the chosen If',
compute(v) {
const miss = need(v, ['Vs', 'Vf', 'n', 'If']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
const head = v.Vs - v.n * v.Vf; if (head <= 0) return { error: 'Supply is not above the total forward voltage.' };
const R = head / v.If, Rs = eNearest(R, v.series).above, I = head / Rs;
return { outputs: [out('Exact resistance', R, 'Ω'), out(`Next ${v.series} value up`, Rs, 'Ω', { highlight: true }), out('Actual current', I, 'A'),
out('Resistor dissipation', I * I * Rs, 'W'), out('LED power (total)', v.n * v.Vf * I, 'W'), out('Efficiency', v.n * v.Vf / v.Vs * 100, '%', { fmt: 'n', dp: 1 })],
notes: ['Vf varies with current, temperature and bin; the rounded-up resistor keeps the current at or below target.'] };
} });
def({ id: 'filter', group: 'Signal', title: 'RC / RL / LC time constants & cut-off',
inputs: [
{ key: 'mode', label: 'Network', type: 'select', options: ['RC', 'RL', 'LC'], def: 'RC' },
{ key: 'R', label: 'Resistance', unit: 'Ω', def: '10k', show: ['RC', 'RL'] },
{ key: 'C', label: 'Capacitance', unit: 'F', def: '100n', show: ['RC', 'LC'] },
{ key: 'L', label: 'Inductance', unit: 'H', def: '10u', show: ['RL', 'LC'] }],
formula: 'RC: fc = 1/(2πRC), τ = RC · RL: fc = R/(2πL), τ = L/R · LC: f0 = 1/(2π√(LC)), Z0 = √(L/C)', ref: 'First-order / second-order networks',
compute(v) {
if (v.mode === 'LC') {
const miss = need(v, ['L', 'C']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
return { outputs: [out('Resonant frequency', 1 / (2 * PI * Math.sqrt(v.L * v.C)), 'Hz', { highlight: true }), out('Characteristic impedance √(L/C)', Math.sqrt(v.L / v.C), 'Ω'), out('Angular frequency', 1 / Math.sqrt(v.L * v.C), 'rad/s')] };
}
const tau = v.mode === 'RC' ? v.R * v.C : v.L / v.R;
if (!Number.isFinite(tau)) return { error: 'Need R and ' + (v.mode === 'RC' ? 'C' : 'L') };
return { outputs: [out('−3 dB cut-off', 1 / (2 * PI * tau), 'Hz', { highlight: true }), out('Time constant τ', tau, 's'),
out('Rise time 10–90 % (2.2τ)', 2.2 * tau, 's'), out('Settle to 1 % (4.6τ)', 4.6 * tau, 's'), out('Settle to 0.1 % (6.9τ)', 6.9 * tau, 's')] };
} });
def({ id: 'trace', group: 'PCB', title: 'Trace width for current (IPC-2152 / IPC-2221)',
blurb: 'Width for a current and temperature rise, or the current a given width carries. Plus DC resistance and drop.',
inputs: [
{ key: 'std', label: 'Standard', type: 'select', options: ['IPC-2152 (baseline chart)', 'IPC-2221'], def: 'IPC-2152 (baseline chart)' },
{ key: 'mode', label: 'Solve for', type: 'select', options: ['width', 'current'], def: 'width' },
{ key: 'I', label: 'Current', unit: 'A', def: '2', show: 'width' },
{ key: 'w', label: 'Trace width', unit: 'mm', def: '0.5', si: false, show: 'current' },
{ key: 'dT', label: 'Temperature rise', unit: '°C', def: '10', si: false, setting: 'trace_dT' },
{ key: 'oz', label: 'Copper weight', unit: 'oz', def: '1', si: false },
{ key: 'layer', label: 'Layer (IPC-2221 only)', type: 'select', options: ['external', 'internal'], def: 'external', show: 'IPC-2221' },
{ key: 'len', label: 'Trace length', unit: 'mm', def: '50', si: false },
{ key: 'Ta', label: 'Ambient', unit: '°C', def: '25', si: false }],
formula: 'IPC-2152 baseline: A = (117.555·ΔT^−0.913 + 1.15)·I^(0.84·ΔT^−0.108 + 1.159) · IPC-2221: I = k·ΔT^0.44·A^0.725 (k 0.048 ext / 0.024 int) · A in mil²; R = ρ(T)·L/(w·t)',
ref: 'IPC-2152 (2009, current revision) baseline chart via the published curve fit on smps.us; IPC-2221C (2023) §6.2 retains the IPC-2221 chart fit. ρCu = 1.724e-8 Ω·m, α = 0.00393/°C',
compute(v) {
const k = v.layer === 'internal' ? 0.024 : 0.048, t_mil = v.oz * OZ_UM / 25.4, t_m = v.oz * OZ_UM * 1e-6;
const is2152 = String(v.std || '').startsWith('IPC-2152');
const A_of = I => is2152 ? (117.555 * Math.pow(v.dT, -0.913) + 1.15) * Math.pow(I, 0.84 * Math.pow(v.dT, -0.108) + 1.159) : Math.pow(I / (k * Math.pow(v.dT, 0.44)), 1 / 0.725);
let I, w_mm;
if (v.mode === 'current') {
const miss = need(v, ['w', 'dT', 'oz']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
w_mm = v.w; const A = w_mm / 0.0254 * t_mil;
if (is2152) { let lo = 0, hi = 200; for (let i = 0; i < 100; i++) { const m = (lo + hi) / 2; if (A_of(m) < A) lo = m; else hi = m; } I = (lo + hi) / 2; }
else I = k * Math.pow(v.dT, 0.44) * Math.pow(A, 0.725);
} else {
const miss = need(v, ['I', 'dT', 'oz']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
I = v.I; w_mm = A_of(I) / t_mil * 0.0254;
}
const T = (Number.isFinite(v.Ta) ? v.Ta : 25) + v.dT, rho = RHO_CU * (1 + ALPHA_CU * (T - 20));
const R = Number.isFinite(v.len) ? rho * (v.len / 1000) / ((w_mm / 1000) * t_m) : NaN;
const o = [v.mode === 'width' ? out('Minimum width', w_mm, 'mm', { fmt: 'n', dp: 3, highlight: true }) : out('Maximum current', I, 'A', { highlight: true }),
out('Width', w_mm / 0.0254, 'mil', { fmt: 'n', dp: 1 }), out('Copper thickness', v.oz * OZ_UM, 'µm', { fmt: 'n', dp: 1 }),
out('Cross-section', (w_mm / 0.0254) * t_mil, 'mil²', { fmt: 'n', dp: 1 })];
if (Number.isFinite(R)) o.push(out(`Resistance at ${fmtN(T, '°C', 0)}`, R, 'Ω'), out('Voltage drop', R * I, 'V'), out('Power in trace', I * I * R, 'W'));
const warn = [];
if (is2152) { if (I > 30) warn.push('IPC-2152 test data stops at 30 A.'); if (v.dT < 2 || v.dT > 100) warn.push('The IPC-2152 fit covers ΔT 2–100 °C.'); }
else { if (I > 35) warn.push('IPC-2221 curves stop at 35 A.'); if (v.dT < 10 || v.dT > 100) warn.push('IPC-2221 fit covers ΔT 10–100 °C.'); }
const note = is2152
? 'IPC-2152 baseline = the standard\'s conservative universal chart (single trace, still air, no nearby plane; 0.070 in polyimide, 3 oz). The standard\'s modifiers for board thickness, copper planes and copper weight usually allow a NARROWER trace; the internal/external distinction of IPC-2221 does not apply. Same current for internal and external layers.'
: 'IPC-2221 is the legacy 1950s-data chart kept in IPC-2221C. It is often too conservative for internal layers; prefer IPC-2152 for new designs.';
return { outputs: o, warnings: warn, notes: [note] };
} });
def({ id: 'impedance', group: 'PCB', title: 'Microstrip / stripline impedance (IPC-2141)',
inputs: [
{ key: 'geom', label: 'Geometry', type: 'select', options: ['microstrip', 'stripline'], def: 'microstrip' },
{ key: 'mode', label: 'Solve for', type: 'select', options: ['impedance', 'width'], def: 'impedance' },
{ key: 'w', label: 'Trace width', unit: 'mm', def: '0.3', si: false, show: 'impedance' },
{ key: 'Z', label: 'Target impedance', unit: 'Ω', def: '50', show: 'width' },
{ key: 'h', label: 'Dielectric height (microstrip: to plane; stripline: plane-to-plane b)', unit: 'mm', def: '0.2', si: false },
{ key: 'oz', label: 'Copper weight', unit: 'oz', def: '1', si: false },
{ key: 'er', label: 'Relative permittivity εr', unit: '', def: '4.3', si: false }],
formula: 'Microstrip: Z0 = 87/√(εr+1.41)·ln(5.98h/(0.8w+t)) · Stripline: Z0 = 60/√εr·ln(1.9b/(0.8w+t))', ref: 'IPC-2141 closed forms (±5–10 %; use a field solver for final stack-ups)',
compute(v) {
const t = v.oz * OZ_UM / 1000, ms = v.geom === 'microstrip';
const Zof = w => ms ? 87 / Math.sqrt(v.er + 1.41) * Math.log(5.98 * v.h / (0.8 * w + t)) : 60 / Math.sqrt(v.er) * Math.log(1.9 * v.h / (0.8 * w + t));
let w = v.w;
if (v.mode === 'width') {
let lo = 0.005 * v.h, hi = 20 * v.h;
if (!(Zof(lo) > v.Z && Zof(hi) < v.Z)) return { error: 'Target impedance is out of reach for this stack-up.' };
for (let i = 0; i < 80; i++) { const m = (lo + hi) / 2; if (Zof(m) > v.Z) lo = m; else hi = m; } w = (lo + hi) / 2;
}
const Z = Zof(w);
const tpd = ms ? 85 * Math.sqrt(0.475 * v.er + 0.67) : 85 * Math.sqrt(v.er); // ps/inch
const o = [v.mode === 'width' ? out('Trace width', w, 'mm', { fmt: 'n', dp: 4, highlight: true }) : out('Impedance Z0', Z, 'Ω', { fmt: 'n', dp: 2, highlight: true }),
out('Width', w / 0.0254, 'mil', { fmt: 'n', dp: 2 }), out('w/h', w / v.h, '', { fmt: 'n', dp: 3 }),
out('Propagation delay', tpd / 25.4, 'ps/mm', { fmt: 'n', dp: 2 }), out('Effective εr', Math.pow(tpd / 84.73, 2), '', { fmt: 'n', dp: 2 })];
const warn = [];
if (ms && (w / v.h < 0.1 || w / v.h > 2.0)) warn.push('Microstrip formula is valid for 0.1 < w/h < 2.0.');
if (!ms && (w / v.h > 0.35 || t / v.h > 0.25)) warn.push('Stripline formula is valid for w/b < 0.35 and t/b < 0.25.');
if (v.er < 1 || v.er > 15) warn.push('Formula is valid for 1 < εr < 15.');
return { outputs: o, warnings: warn };
} });
def({ id: 'ldo', group: 'Power', title: 'Linear regulator dissipation',
inputs: [
{ key: 'Vin', label: 'Input voltage', unit: 'V', def: '5' },
{ key: 'Vout', label: 'Output voltage', unit: 'V', def: '3.3' },
{ key: 'Iout', label: 'Load current', unit: 'A', def: '300m' },
{ key: 'Iq', label: 'Quiescent / ground current', unit: 'A', def: '55u' },
{ key: 'theta', label: 'θJA (datasheet, your layout)', unit: '°C/W', def: '184', si: false },
{ key: 'Ta', label: 'Ambient', unit: '°C', def: '25', si: false },
{ key: 'Tjmax', label: 'Max junction', unit: '°C', def: '125', si: false }],
formula: 'Pd = (Vin − Vout)·Iout + Vin·Iq, Tj = Ta + Pd·θJA', ref: 'Standard LDO thermal model (θJA is layout-dependent)',
compute(v) {
const miss = need(v, ['Vin', 'Vout', 'Iout', 'theta', 'Ta']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
const Iq = Number.isFinite(v.Iq) ? v.Iq : 0, Pd = (v.Vin - v.Vout) * v.Iout + v.Vin * Iq, Tj = v.Ta + Pd * v.theta;
const Imax = ((v.Tjmax - v.Ta) / v.theta - v.Vin * Iq) / (v.Vin - v.Vout);
const warn = Tj > v.Tjmax ? [`Junction exceeds ${v.Tjmax} °C: reduce the drop, add copper, or use a switcher.`] : [];
return { outputs: [out('Dissipation', Pd, 'W', { highlight: true }), out('Junction temperature', Tj, '°C', { fmt: 'n', dp: 1 }),
out('Margin to Tjmax', v.Tjmax - Tj, '°C', { fmt: 'n', dp: 1 }), out('Efficiency', v.Vout * v.Iout / (v.Vin * (v.Iout + Iq)) * 100, '%', { fmt: 'n', dp: 1 }),
out('Max load current at Tjmax', Imax, 'A')], warnings: warn };
} });
def({ id: 'buck', group: 'Power', title: 'Buck converter: inductor & ripple',
inputs: [
{ key: 'Vin', label: 'Max input voltage', unit: 'V', def: '12' },
{ key: 'Vout', label: 'Output voltage', unit: 'V', def: '3.3' },
{ key: 'Iout', label: 'Output current', unit: 'A', def: '2' },
{ key: 'fsw', label: 'Switching frequency', unit: 'Hz', def: '500k' },
{ key: 'rip', label: 'Inductor ripple (% of Iout)', unit: '%', def: '30', si: false },
{ key: 'eff', label: 'Estimated efficiency', unit: '%', def: '90', si: false },
{ key: 'Cout', label: 'Output capacitance', unit: 'F', def: '44u' },
{ key: 'esr', label: 'Output cap ESR', unit: 'Ω', def: '5m' }],
formula: 'D = Vout/(Vin·η), L = Vout·(Vin−Vout)/(ΔIL·fsw·Vin), ΔVout = ΔIL/(8·fsw·Cout) + ΔIL·ESR', ref: 'TI SLVA477B (basic buck power stage)',
compute(v) {
const miss = need(v, ['Vin', 'Vout', 'Iout', 'fsw', 'rip']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
if (v.Vout >= v.Vin) return { error: 'Buck needs Vout < Vin.' };
const eta = (v.eff || 90) / 100, D = v.Vout / (v.Vin * eta), dI = v.rip / 100 * v.Iout;
const L = v.Vout * (v.Vin - v.Vout) / (dI * v.fsw * v.Vin);
const o = [out('Duty cycle', D * 100, '%', { fmt: 'n', dp: 1 }), out('Minimum inductance', L, 'H', { highlight: true }), out('Ripple current ΔIL', dI, 'A'),
out('Peak inductor / switch current', v.Iout + dI / 2, 'A'), out('Inductor RMS (≈)', Math.sqrt(v.Iout ** 2 + dI ** 2 / 12), 'A')];
if (Number.isFinite(v.Cout)) o.push(out('Output ripple', dI / (8 * v.fsw * v.Cout) + (Number.isFinite(v.esr) ? dI * v.esr : 0), 'V'));
return { outputs: o, notes: ['Use the MAXIMUM input voltage for L (worst ripple). Choose an inductor saturation rating above the peak current, and check the controller datasheet: many fix the ripple ratio.'] };
} });
def({ id: 'boost', group: 'Power', title: 'Boost converter: inductor & ripple',
inputs: [
{ key: 'Vin', label: 'Min input voltage', unit: 'V', def: '3.3' },
{ key: 'Vout', label: 'Output voltage', unit: 'V', def: '12' },
{ key: 'Iout', label: 'Output current', unit: 'A', def: '500m' },
{ key: 'fsw', label: 'Switching frequency', unit: 'Hz', def: '600k' },
{ key: 'rip', label: 'Inductor ripple (% of inductor current)', unit: '%', def: '30', si: false },
{ key: 'eff', label: 'Estimated efficiency', unit: '%', def: '85', si: false },
{ key: 'Cout', label: 'Output capacitance', unit: 'F', def: '22u' },
{ key: 'esr', label: 'Output cap ESR', unit: 'Ω', def: '5m' }],
formula: 'D = 1 − Vin·η/Vout, L = Vin·(Vout−Vin)/(ΔIL·fsw·Vout), ΔVout = Iout·D/(fsw·Cout) + ESR·Ipk', ref: 'TI SLVA372C (basic boost power stage)',
compute(v) {
const miss = need(v, ['Vin', 'Vout', 'Iout', 'fsw', 'rip']); if (miss.length) return { error: 'Need ' + miss.join(', ') };
if (v.Vout <= v.Vin) return { error: 'Boost needs Vout > Vin.' };
const eta = (v.eff || 85) / 100, D = 1 - v.Vin * eta / v.Vout, IL = v.Iout / (1 - D), dI = v.rip / 100 * IL;
const L = v.Vin * (v.Vout - v.Vin) / (dI * v.fsw * v.Vout), Ipk = IL + dI / 2;
const o = [out('Duty cycle', D * 100, '%', { fmt: 'n', dp: 1 }), out('Minimum inductance', L, 'H', { highlight: true }), out('Average inductor current', IL, 'A'),
out('Ripple current ΔIL', dI, 'A'), out('Peak switch current', Ipk, 'A'), out('Input power', v.Vout * v.Iout / eta, 'W')];
if (Number.isFinite(v.Cout)) o.push(out('Output ripple', v.Iout * D / (v.fsw * v.Cout) + (Number.isFinite(v.esr) ? v.esr * Ipk : 0), 'V'));
return { outputs: o, notes: ['Use the MINIMUM input voltage (highest duty and current). Check the switch current limit against the peak.'] };
} });
def({ id: 'holdup', group: 'Power', title: 'Hold-up / bulk capacitor',
inputs: [
{ key: 'mode', label: 'Solve for', type: 'select', options: ['time', 'capacitance'], def: 'capacitance' },
{ key: 'load', label: 'Load type', type: 'select', options: ['constant power', 'constant current'], def: 'constant power' },
{ key: 'P', label: 'Load power', unit: 'W', def: '2', show: 'constant power' },
{ key: 'I', label: 'Load current', unit: 'A', def: '100m', show: 'constant current' },
{ key: 'V1', label: 'Start voltage', unit: 'V', def: '12' },
{ key: 'V2', label: 'Minimum usable voltage', unit: 'V', def: '9' },
{ key: 'C', label: 'Capacitance', unit: 'F', def: '1m', show: 'time' },
{ key: 't', label: 'Required hold-up time', unit: 's', def: '10m', show: 'capacitance' }],
formula: 'Constant P: t = C·(V1² − V2²)/(2P) · Constant I: t = C·(V1 − V2)/I', ref: 'Capacitor energy E = ½CV²',
compute(v) {
const cp = v.load === 'constant power', dV2 = v.V1 * v.V1 - v.V2 * v.V2, dV = v.V1 - v.V2;
if (!(dV > 0)) return { error: 'Start voltage must exceed the minimum.' };
const o = [];
if (v.mode === 'time') {
const t = cp ? v.C * dV2 / (2 * v.P) : v.C * dV / v.I; o.push(out('Hold-up time', t, 's', { highlight: true }));
o.push(out('Usable energy', 0.5 * v.C * dV2, 'J'));
} else {
const C = cp ? 2 * v.P * v.t / dV2 : v.I * v.t / dV; o.push(out('Required capacitance', C, 'F', { highlight: true }));
o.push(out('Usable energy', 0.5 * C * dV2, 'J'), out('Stored at start', 0.5 * C * v.V1 * v.V1, 'J'));
}
return { outputs: o, notes: ['Add margin for capacitance tolerance, ageing and DC-bias derating (ceramics can lose 50 %+ at rated voltage).'] };
} });
def({ id: 'battery', group: 'Power', title: 'Battery life (duty-cycled load)',
inputs: [
{ key: 'cap', label: 'Battery capacity', unit: 'mAh', def: '2000', si: false },
{ key: 'Ia', label: 'Active current', unit: 'A', def: '25m' },
{ key: 'Is', label: 'Sleep current', unit: 'A', def: '15u' },
{ key: 'duty', label: 'Active duty cycle', unit: '%', def: '2', si: false },
{ key: 'derate', label: 'Usable capacity', unit: '%', def: '85', si: false }],
formula: 'Iavg = Ia·d + Is·(1−d), life = C·derate / Iavg', ref: 'Charge balance (ignores self-discharge and temperature)',
compute(v) {
const d = v.duty / 100, Iavg = v.Ia * d + v.Is * (1 - d), h = (v.cap / 1000) * (v.derate / 100) / Iavg;
return { outputs: [out('Average current', Iavg, 'A'), out('Battery life', h, 'h', { fmt: 'n', dp: 1, highlight: true }),
out('…in days', h / 24, 'days', { fmt: 'n', dp: 1 }), out('…in years', h / 8766, 'years', { fmt: 'n', dp: 2 })],
notes: ['Self-discharge dominates past about a year for many chemistries; include it for long-life designs.'] };
} });
def({ id: 'i2c', group: 'Interfaces', title: 'I²C pull-up resistor range',
inputs: [
{ key: 'VDD', label: 'Pull-up supply', unit: 'V', def: '3.3' },
{ key: 'Cb', label: 'Bus capacitance', unit: 'F', def: '100p' },
{ key: 'speed', label: 'Mode', type: 'select', options: ['Standard 100 kHz', 'Fast 400 kHz', 'Fast-mode Plus 1 MHz'], def: 'Fast 400 kHz' }],
formula: 'Rmin = (VDD − VOL)/IOL, Rmax = tr / (0.8473·Cb)', ref: 'NXP UM10204 §7.1 (tr max 1000/300/120 ns; IOL 3/3/20 mA; VOL 0.4 V, or 0.2·VDD below 2 V)',
compute(v) {
const spec = { 'Standard 100 kHz': [1000e-9, 3e-3], 'Fast 400 kHz': [300e-9, 3e-3], 'Fast-mode Plus 1 MHz': [120e-9, 20e-3] }[v.speed];
const VOL = v.VDD > 2 ? 0.4 : 0.2 * v.VDD, Rmin = (v.VDD - VOL) / spec[1], Rmax = spec[0] / (0.8473 * v.Cb);
const o = [out('Minimum pull-up', Rmin, 'Ω'), out('Maximum pull-up', Rmax, 'Ω')];
const warn = [];
if (Rmin > Rmax) warn.push('No resistor satisfies both limits: reduce bus capacitance, lower the speed, or use an active pull-up / buffer.');
else { const pick = eValuesInRange(Rmin, Rmax, 'E24'); const g = Math.sqrt(Rmin * Rmax);
if (pick.length) o.push(out('Suggested E24', pick.reduce((a, b) => Math.abs(Math.log(b / g)) < Math.abs(Math.log(a / g)) ? b : a), 'Ω', { highlight: true })); }
if (v.Cb > (v.speed.startsWith('Fast-mode Plus') ? 550e-12 : 400e-12)) warn.push('Bus capacitance exceeds the spec limit for this mode.');
return { outputs: o, warnings: warn, notes: ['Estimate Cb as ~10 pF per device pin plus trace/cable capacitance.'] };
} });
def({ id: 'crystal', group: 'Interfaces', title: 'Crystal oscillator load capacitors (CL)',
blurb: 'Pierce oscillator: load caps for a crystal\'s rated CL, or the CL your caps give; with optional start-up margin, pulling and drive level.',
inputs: [
{ key: 'mode', label: 'Mode', type: 'select', options: ['caps for target CL', 'CL from C1/C2'], def: 'caps for target CL' },
{ key: 'CL', label: 'Crystal rated load capacitance CL (datasheet)', unit: 'F', def: '12p' },
{ key: 'Cs', label: 'Stray capacitance Cs (pins + traces)', unit: 'F', def: '4p' },
{ key: 'series', label: 'E-series for C1 = C2', type: 'select', options: ['E6', 'E12', 'E24'], def: 'E12', show: 'caps for target CL' },
{ key: 'C1', label: 'C1 (OSC_IN side)', unit: 'F', def: '18p', show: 'CL from C1/C2' },
{ key: 'C2', label: 'C2 (OSC_OUT side)', unit: 'F', def: '18p', show: 'CL from C1/C2' },
{ key: 'F', label: 'Crystal frequency (for gm, drive level)', unit: 'Hz', def: '8M' },
{ key: 'C0', label: 'Shunt capacitance C0 (optional)', unit: 'F', def: '' },
{ key: 'ESR', label: 'ESR max (optional)', unit: 'Ω', def: '' },
{ key: 'gm', label: 'MCU oscillator gm (optional, A/V)', unit: 'A/V', def: '' },
{ key: 'Cm', label: 'Motional capacitance C1m (optional, for pulling)', unit: 'F', def: '' },
{ key: 'Vpp', label: 'Measured Vpp at OSC_IN (optional, for drive level)', unit: 'V', def: '' },
{ key: 'Cp', label: 'Scope probe capacitance', unit: 'F', def: '1p' },
{ key: 'DLmax', label: 'Crystal max drive level (optional)', unit: 'W', def: '' }],
formula: 'CL = C1·C2/(C1+C2) + Cs → C1 = C2 = 2·(CL − Cs)\ngm_crit = 4·ESR·(2πF)²·(C0 + CL)², gain margin = gm/gm_crit ≥ 5\n' +
'pulling Δf/f = Cm/(2(C0+CL)), trim sensitivity = −Cm/(2(C0+CL)²)\nDL = ESR·IQ², IQ = 2πF·(Vpp/2√2)·(C1 + Cs/2 + Cprobe)',
ref: 'ST AN2867 Rev 10 §3.3–3.5 (CL, gm_crit, gain margin ≥ 5, drive level); pulling / trim sensitivity from the crystal equivalent circuit (CTS, ECS crystal notes)',
compute(v) {
const o = [], warnings = [], notes = [];
if (!(v.CL > 0) || !(v.Cs >= 0)) return { error: 'Enter the crystal load capacitance and the stray capacitance.' };
let C1, C2, CLa;
if (v.mode === 'caps for target CL') {
const C = 2 * (v.CL - v.Cs); if (!(C > 0)) return { error: 'Stray capacitance exceeds the load capacitance: no external caps can reach this CL.' };
const Cn = eNearest(C, v.series).nearest; C1 = C2 = Cn; CLa = Cn / 2 + v.Cs;
o.push(out('Exact C1 = C2', C, 'F'), out(`Nearest ${v.series} C1 = C2`, Cn, 'F', { highlight: true }));
} else {
C1 = v.C1; C2 = v.C2; if (!(C1 > 0 && C2 > 0)) return { error: 'Enter C1 and C2.' };
CLa = par(C1, C2) + v.Cs;
}
o.push(out('Load the crystal sees', CLa, 'F', { highlight: v.mode !== 'caps for target CL' }), out('Load error vs rated CL', (CLa - v.CL) / v.CL * 100, '%', { fmt: 'n', dp: 1 }));
if (Number.isFinite(v.C0) && Number.isFinite(v.Cm)) {
const pull = CL => v.Cm / (2 * (v.C0 + CL)) * 1e6;
o.push(out('Frequency offset from load error', pull(CLa) - pull(v.CL), 'ppm', { fmt: 'n', dp: 2 }),
out('Trim sensitivity at this load', -v.Cm / (2 * Math.pow(v.C0 + CLa, 2)) * 1e-12 * 1e6, 'ppm/pF', { fmt: 'n', dp: 2 }));
}
if (Number.isFinite(v.C0) && Number.isFinite(v.ESR) && v.F > 0) {
const gmc = 4 * v.ESR * Math.pow(2 * PI * v.F, 2) * Math.pow(v.C0 + v.CL, 2);
o.push(out('gm_crit', gmc, 'A/V'));
if (Number.isFinite(v.gm)) {
const gmm = v.gm / gmc; o.push(out('Gain margin gm/gm_crit', gmm, '', { fmt: 'n', dp: 1 }));
if (gmm < 5) warnings.push(`Gain margin ${gmm.toFixed(1)} is below 5: start-up is not guaranteed over temperature and tolerance. Pick a crystal with lower ESR or lower CL (AN2867 §3.4).`);
} else notes.push('Enter the MCU oscillator gm to get the gain margin; if the datasheet gives gm_crit_max instead, it must exceed gm_crit (gm_crit_max = gm/5).');
}
if (Number.isFinite(v.Vpp) && Number.isFinite(v.ESR) && v.F > 0) {
const Ctot = C1 + v.Cs / 2 + (Number.isFinite(v.Cp) ? v.Cp : 0), IQ = 2 * PI * v.F * (v.Vpp / (2 * Math.SQRT2)) * Ctot, DL = v.ESR * IQ * IQ;
o.push(out('Crystal current IQ (RMS)', IQ, 'A'), out('Drive level', DL, 'W'));
if (Number.isFinite(v.DLmax)) {
o.push(out('Max IQ peak-to-peak for DLmax', 2 * Math.SQRT2 * Math.sqrt(v.DLmax / v.ESR), 'A'));
if (DL > v.DLmax) warnings.push('Drive level exceeds the crystal maximum: add a series resistor RExt on OSC_OUT (AN2867 §3.5.3).');
}
}
notes.push('Stray capacitance is typically 2–5 pF (MCU pin capacitance plus traces); confirm against a frequency measurement and trim C1/C2.');
return { outputs: o, warnings, notes };
} });
def({ id: 'shunt', group: 'Measurement', title: 'Current-sense shunt',
inputs: [
{ key: 'I', label: 'Full-scale current', unit: 'A', def: '10' },
{ key: 'R', label: 'Shunt resistance', unit: 'Ω', def: '5m' },
{ key: 'G', label: 'Amplifier gain', unit: 'V/V', def: '50', si: false },
{ key: 'bits', label: 'ADC bits', unit: '', def: '12', si: false },
{ key: 'Vref', label: 'ADC reference', unit: 'V', def: '3.3' }],
formula: 'Vsense = I·R, P = I²R, Vout = G·I·R, LSB current = Vref/(2^N·G·R)', ref: 'Ohm / Joule',
compute(v) {
const Vs = v.I * v.R, Vo = v.G * Vs, lsb = v.Vref / Math.pow(2, v.bits) / (v.G * v.R);
const warn = Vo > v.Vref ? [`Amplifier output (${fmtSI(Vo, 'V')}) exceeds the ADC reference: full scale is ${fmtSI(v.Vref / (v.G * v.R), 'A')}.`] : [];
return { outputs: [out('Sense voltage', Vs, 'V'), out('Shunt dissipation', v.I * v.I * v.R, 'W', { highlight: true }), out('Amplifier output', Vo, 'V'),
out('Current per ADC LSB', lsb, 'A'), out('Usable full scale', Math.min(v.I, v.Vref / (v.G * v.R)), 'A')], warnings: warn,
notes: ['Derate the shunt power rating (often to 50–70 %) and route the sense lines Kelvin-connected to the pads.'] };
} });
def({ id: 'adc', group: 'Measurement', title: 'ADC resolution & ideal SNR',
inputs: [
{ key: 'bits', label: 'Resolution', unit: 'bits', def: '12', si: false },
{ key: 'Vref', label: 'Full-scale / reference', unit: 'V', def: '3.3' },
{ key: 'Vin', label: 'Input voltage (for code)', unit: 'V', def: '1.65' }],
formula: 'LSB = Vref/2^N, code = ⌊Vin/LSB⌋, SNR = 6.02N + 1.76 dB, q-noise = LSB/√12', ref: 'Ideal quantiser',
compute(v) {
const lsb = v.Vref / Math.pow(2, v.bits);
return { outputs: [out('LSB size', lsb, 'V', { highlight: true }), out('Code for input', Math.min(Math.pow(2, v.bits) - 1, Math.floor(v.Vin / lsb)), '', { fmt: 'n', dp: 0 }),
out('Ideal SNR', 6.02 * v.bits + 1.76, 'dB', { fmt: 'n', dp: 2 }), out('Quantisation noise (RMS)', lsb / Math.sqrt(12), 'V'), out('Dynamic range', 20 * Math.log10(Math.pow(2, v.bits)), 'dB', { fmt: 'n', dp: 1 })] };
} });
def({ id: 'thermal', group: 'Thermal', title: 'Junction temperature & heatsink',
inputs: [
{ key: 'mode', label: 'Solve for', type: 'select', options: ['junction temperature', 'required heatsink'], def: 'junction temperature' },
{ key: 'P', label: 'Power dissipated', unit: 'W', def: '5' },
{ key: 'Ta', label: 'Ambient', unit: '°C', def: '40', si: false },
{ key: 'jc', label: 'θJC', unit: '°C/W', def: '1.5', si: false },
{ key: 'cs', label: 'θCS (interface)', unit: '°C/W', def: '0.5', si: false },
{ key: 'sa', label: 'θSA (heatsink)', unit: '°C/W', def: '8', si: false, show: 'junction temperature' },
{ key: 'Tj', label: 'Max junction', unit: '°C', def: '125', si: false, show: 'required heatsink' }],
formula: 'Tj = Ta + P·(θJC + θCS + θSA)', ref: 'Series thermal resistance model',
compute(v) {
if (v.mode === 'required heatsink') {
const sa = (v.Tj - v.Ta) / v.P - v.jc - v.cs;
return { outputs: [out('Heatsink θSA must be ≤', sa, '°C/W', { fmt: 'n', dp: 2, highlight: true })], warnings: sa <= 0 ? ['Not achievable: the junction-to-sink path alone exceeds the budget.'] : [] };
}
const Tj = v.Ta + v.P * (v.jc + v.cs + v.sa);
return { outputs: [out('Junction temperature', Tj, '°C', { fmt: 'n', dp: 1, highlight: true }), out('Case temperature', v.Ta + v.P * (v.cs + v.sa), '°C', { fmt: 'n', dp: 1 }),
out('Heatsink temperature', v.Ta + v.P * v.sa, '°C', { fmt: 'n', dp: 1 })] };
} });
def({ id: 'wire', group: 'Wiring', title: 'Wire gauge: resistance & drop',
inputs: [
{ key: 'awg', label: 'Gauge (AWG; 0 = 1/0, −1 = 2/0 …)', unit: 'AWG', def: '24', si: false },
{ key: 'len', label: 'Cable length (one way)', unit: 'm', def: '1', si: false },
{ key: 'I', label: 'Current', unit: 'A', def: '1' },
{ key: 'rt', label: 'Count return conductor', type: 'select', options: ['yes', 'no'], def: 'yes' },
{ key: 'T', label: 'Conductor temperature', unit: '°C', def: '20', si: false }],
formula: 'd = 0.127 mm · 92^((36 − AWG)/39), R = ρ(T)·L/A', ref: 'ASTM B258 gauge definition; annealed copper ρ = 1.724e-8 Ω·m',
compute(v) {
const d = 0.127 * Math.pow(92, (36 - v.awg) / 39), A = PI * (d / 2) ** 2 * 1e-6, rho = RHO_CU * (1 + ALPHA_CU * (v.T - 20));
const Rm = rho / A, L = v.len * (v.rt === 'yes' ? 2 : 1), R = Rm * L;
return { outputs: [out('Diameter', d, 'mm', { fmt: 'n', dp: 3 }), out('Area', A * 1e6, 'mm²', { fmt: 'n', dp: 4 }), out('Resistance per metre', Rm, 'Ω/m'),
out('Loop resistance', R, 'Ω'), out('Voltage drop', R * v.I, 'V', { highlight: true }), out('Power lost', v.I * v.I * R, 'W')],
notes: ['Solid-conductor figures; stranded wire of the same gauge is a few percent higher. Ampacity depends on insulation and bundling.'] };
} });
def({ id: 'rf', group: 'RF', title: 'dBm · watts · volts',
inputs: [
{ key: 'val', label: 'Value', unit: '', def: '0', si: true },
{ key: 'unit', label: 'Unit', type: 'select', options: ['dBm', 'W', 'Vrms', 'Vpp'], def: 'dBm' },
{ key: 'Z', label: 'System impedance', unit: 'Ω', def: '50' }],
formula: 'P = 10^((dBm − 30)/10) W, Vrms = √(P·Z), Vpp = 2√2·Vrms', ref: 'Sinusoid into a resistive load',
compute(v) {
let P;
if (v.unit === 'dBm') P = Math.pow(10, (v.val - 30) / 10); else if (v.unit === 'W') P = v.val;
else if (v.unit === 'Vrms') P = v.val * v.val / v.Z; else P = (v.val / (2 * Math.SQRT2)) ** 2 / v.Z;
const Vr = Math.sqrt(P * v.Z);
return { outputs: [out('Power', 10 * Math.log10(P) + 30, 'dBm', { fmt: 'n', dp: 2, highlight: true }), out('Power', P, 'W'), out('dBW', 10 * Math.log10(P), 'dBW', { fmt: 'n', dp: 2 }),
out('RMS voltage', Vr, 'V'), out('Peak-to-peak voltage', Vr * 2 * Math.SQRT2, 'V'), out('dBµV', 20 * Math.log10(Vr / 1e-6), 'dBµV', { fmt: 'n', dp: 1 })] };
} });
def({ id: 'wavelength', group: 'RF', title: 'Wavelength & antenna lengths',
inputs: [
{ key: 'f', label: 'Frequency', unit: 'Hz', def: '2.4G' },
{ key: 'er', label: 'Effective εr (1 = free space)', unit: '', def: '1', si: false }],
formula: 'λ = c / (f·√εr)', ref: 'c = 299 792 458 m/s',
compute(v) {
const lam = 299792458 / (v.f * Math.sqrt(v.er));
return { outputs: [out('Wavelength λ', lam * 1000, 'mm', { fmt: 'n', dp: 2, highlight: true }), out('λ/2', lam * 500, 'mm', { fmt: 'n', dp: 2 }), out('λ/4', lam * 250, 'mm', { fmt: 'n', dp: 2 }),
out('λ/10 (lumped-element limit)', lam * 100, 'mm', { fmt: 'n', dp: 2 }), out('Delay per mm', Math.sqrt(v.er) / 299792458 * 1e-3, 's')],
notes: ['Real dipoles are cut ~5 % shorter than λ/2 (end effect); tune on the bench.'] };
} });
def({ id: 'eseries', group: 'Components', title: 'Standard values & resistor codes',
inputs: [
{ key: 'val', label: 'Wanted value', unit: 'Ω', def: '4k75' },
{ key: 'series', label: 'E-series', type: 'select', options: ['E6', 'E12', 'E24', 'E48', 'E96', 'E192'], def: 'E96', setting: 'e_series' }],
formula: 'IEC 60063 preferred numbers; IEC 60062 colour code; EIA-96 SMD code', ref: 'IEC 60063 / 60062, EIA-96',
compute(v) {
const n = eNearest(v.val, v.series), sig = ['E48', 'E96', 'E192'].includes(v.series) ? 3 : 2;
const o = [out('Nearest', n.nearest, 'Ω', { highlight: true }), out('Next below', n.below, 'Ω'), out('Next above', n.above, 'Ω'),
out('Error of nearest', (n.nearest - v.val) / v.val * 100, '%', { fmt: 'n', dp: 2 }), out('Tolerance class', TOL[v.series], '', { fmt: 'raw' }),
out('Colour bands', colourBands(n.nearest, sig).join(' · '), '', { fmt: 'raw' })];
o.push(sig === 3 ? out('EIA-96 SMD code', eia96(n.nearest) || '— (not an E96 value)', '', { fmt: 'raw' }) : out('3-digit SMD code', smd3(n.nearest), '', { fmt: 'raw' }));
return { outputs: o };
} });
// ================================================================ expansion 2026-09-25
const IN = 0.0254, MU0 = 4e-7 * PI, C0 = 299792458;
const Z0ms = (w, h, t, er) => 87 / Math.sqrt(er + 1.41) * Math.log(5.98 * h / (0.8 * w + t));
const Z0sl = (w, b, t, er) => 60 / Math.sqrt(er) * Math.log(1.9 * b / (0.8 * w + t));
def({ id: 'spacing', group: 'PCB', title: 'Conductor spacing for voltage (IPC-2221 Table 6-1)',
blurb: 'Minimum electrical clearance between conductors at sea level to 3050 m.',
inputs: [
{ key: 'V', label: 'Peak voltage between conductors (DC or AC peak)', unit: 'V', def: '48' },
{ key: 'col', label: 'Conductor location', type: 'select', options: ['B1 internal layer', 'B2 external, uncoated (sea level – 3050 m)', 'B4 external, with permanent polymer coating / solder mask'], def: 'B2 external, uncoated (sea level – 3050 m)' }],
formula: 'Table lookup up to 500 V; above 500 V: 500 V value + (V − 500) × per-volt increment', ref: 'IPC-2221B Table 6-1 (columns unchanged at sea level in IPC-2221C, Dec 2023); verified against two independent reproductions',
compute(v) {
const rows = [[15, .05, .1, .05], [30, .05, .1, .05], [50, .1, .6, .13], [100, .1, .6, .13], [150, .2, .6, .4], [170, .2, 1.25, .4], [250, .2, 1.25, .4], [300, .2, 1.25, .4], [500, .25, 2.5, .8]];
const inc = [0.0025, 0.005, 0.00305], c = v.col.startsWith('B1') ? 0 : v.col.startsWith('B2') ? 1 : 2;
if (!(v.V >= 0)) return { error: 'Enter a voltage.' };
const d = v.V <= 500 ? rows.find(r => v.V <= r[0])[c + 1] : rows[8][c + 1] + (v.V - 500) * inc[c];
return { outputs: [out('Minimum spacing', d, 'mm', { fmt: 'n', dp: 3, highlight: true }), out('Minimum spacing', d / IN, 'mil', { fmt: 'n', dp: 1 })],
notes: ['IPC-2221C adds a B5 column, altitude categories and explicit solder-mask wording; columns A5–A7 (component leads, conformal coating) are not included here. Safety standards (IEC 62368-1, IEC 60664-1, UL) usually demand MORE creepage and clearance than IPC-2221 for mains and user-accessible circuits: they govern where they apply.'] };
} });
def({ id: 'diffpair', group: 'PCB', title: 'Differential pair impedance (edge-coupled)',
inputs: [
{ key: 'geom', label: 'Geometry', type: 'select', options: ['microstrip', 'stripline'], def: 'microstrip' },
{ key: 'w', label: 'Trace width', unit: 'mm', def: '0.15', si: false },
{ key: 's', label: 'Edge-to-edge gap', unit: 'mm', def: '0.15', si: false },
{ key: 'h', label: 'Dielectric height (stripline: plane-to-plane b)', unit: 'mm', def: '0.1', si: false },
{ key: 'oz', label: 'Copper weight', unit: 'oz', def: '0.5', si: false },
{ key: 'er', label: 'εr', unit: '', def: '4.3', si: false }],
formula: 'Microstrip: Zdiff = 2·Z0·(1 − 0.48·e^(−0.96·s/h)) · Stripline: Zdiff = 2·Z0·(1 − 0.347·e^(−2.9·s/b))', ref: 'IPC-2141A / National Semiconductor AN-905 closed forms (±10 %; confirm with your fab\'s field solver)',
compute(v) {
const t = v.oz * OZ_UM / 1000, ms = v.geom === 'microstrip';
const Z0 = ms ? Z0ms(v.w, v.h, t, v.er) : Z0sl(v.w, v.h, t, v.er);
const Zd = ms ? 2 * Z0 * (1 - 0.48 * Math.exp(-0.96 * v.s / v.h)) : 2 * Z0 * (1 - 0.347 * Math.exp(-2.9 * v.s / v.h));
return { outputs: [out('Differential impedance', Zd, 'Ω', { fmt: 'n', dp: 1, highlight: true }), out('Odd-mode impedance', Zd / 2, 'Ω', { fmt: 'n', dp: 1 }),
out('Single-ended Z0 (uncoupled)', Z0, 'Ω', { fmt: 'n', dp: 1 }), out('Gap / height', v.s / v.h, '', { fmt: 'n', dp: 2 })],
notes: ['Common targets: USB 90 Ω, Ethernet / LVDS / PCIe / HDMI 100 Ω (PCIe Gen3+ 85 Ω).'] };
} });
def({ id: 'via', group: 'PCB', title: 'Via: resistance, current, parasitics',
inputs: [
{ key: 'd', label: 'Finished hole diameter', unit: 'mm', def: '0.3', si: false },
{ key: 'tp', label: 'Barrel plating thickness', unit: 'µm', def: '25', si: false },
{ key: 'h', label: 'Via length (board thickness)', unit: 'mm', def: '1.6', si: false },
{ key: 'pad', label: 'Pad diameter', unit: 'mm', def: '0.6', si: false },
{ key: 'anti', label: 'Antipad (plane clearance) diameter', unit: 'mm', def: '1.0', si: false },
{ key: 'er', label: 'εr', unit: '', def: '4.3', si: false },
{ key: 'dT', label: 'Allowed temperature rise', unit: '°C', def: '10', si: false },
{ key: 'Z0', label: 'Line impedance (for rise-time degradation)', unit: 'Ω', def: '50' }],
formula: 'A = π((d/2+t)² − (d/2)²), R = ρ·h/A, Imax: IPC-2221 external on A, L = 5.08·h·[ln(4h/d)+1] nH (h,d in in), C = 1.41·εr·T·D1/(D2 − D1) pF, Δt10-90 = 2.2·C·Z0/2',
ref: 'Barrel geometry (as KiCad Via Size); L and C: H. Johnson & M. Graham, High-Speed Digital Design §7; thermal: k_Cu = 401 W/m·K',
compute(v) {
const r = v.d / 2, t = v.tp / 1000, A = PI * ((r + t) ** 2 - r ** 2), Amm2 = A;
const R = RHO_CU * (v.h / 1000) / (Amm2 * 1e-6), Imax = 0.048 * Math.pow(v.dT, 0.44) * Math.pow(Amm2 / (IN * IN), 0.725);
const hin = v.h / 25.4, din = v.d / 25.4, L = 5.08 * hin * (Math.log(4 * hin / din) + 1) * 1e-9;
const C = v.anti > v.pad ? 1.41 * v.er * hin * (v.pad / 25.4) / ((v.anti - v.pad) / 25.4) * 1e-12 : NaN;
return { outputs: [out('Barrel cross-section', Amm2, 'mm²', { fmt: 'n', dp: 4 }), out('DC resistance', R, 'Ω', { highlight: true }), out(`Current for ${v.dT} °C rise (IPC-2221 ext.)`, Imax, 'A'),
out('Thermal resistance (barrel)', (v.h / 1000) / (401 * Amm2 * 1e-6), '°C/W', { fmt: 'n', dp: 1 }), out('Inductance', L, 'H'), out('Pad capacitance', C, 'F'),
out('Via impedance √(L/C)', Math.sqrt(L / C), 'Ω', { fmt: 'n', dp: 1 }), out('10–90 % rise-time degradation', 2.2 * C * v.Z0 / 2, 's')],
notes: ['IPC-2152 also covers vias (short vias carry more than the trace rule suggests); the IPC-2221 figure here is conservative. Use several vias in parallel for power.'] };
} });
def({ id: 'fusing', group: 'PCB', title: 'Fusing current (Onderdonk)',
inputs: [
{ key: 'shape', label: 'Conductor', type: 'select', options: ['PCB trace', 'round wire'], def: 'PCB trace' },
{ key: 'w', label: 'Trace width', unit: 'mm', def: '0.25', si: false, show: 'PCB trace' },
{ key: 'oz', label: 'Copper weight', unit: 'oz', def: '1', si: false, show: 'PCB trace' },
{ key: 'dia', label: 'Wire diameter', unit: 'mm', def: '0.5', si: false, show: 'round wire' },
{ key: 't', label: 'Pulse duration', unit: 's', def: '1' },
{ key: 'Ta', label: 'Ambient', unit: '°C', def: '25', si: false }],
formula: 'I = A·√( log10(1 + (Tm − Ta)/(234 + Ta)) / (33·t) ), A in circular mils, Tm = 1083 °C (copper)', ref: 'Onderdonk equation (adiabatic; used by Saturn PCB Toolkit)',
compute(v) {
const Amm2 = v.shape === 'PCB trace' ? v.w * v.oz * OZ_UM / 1000 : PI * (v.dia / 2) ** 2;
const cmil = Amm2 / (IN * IN) * 4 / PI, I = cmil * Math.sqrt(Math.log10(1 + (1083 - v.Ta) / (234 + v.Ta)) / (33 * v.t));
return { outputs: [out('Fusing current', I, 'A', { highlight: true }), out('Cross-section', Amm2, 'mm²', { fmt: 'n', dp: 4 }), out('Circular mils', cmil, 'cmil', { fmt: 'n', dp: 0 })],
warnings: v.t > 10 ? ['Onderdonk assumes no heat loss: valid for short pulses (roughly < 10 s).'] : [],
notes: ['This is where copper MELTS. Never design to it; use it to check that a fault clears a fuse before a trace opens.'] };
} });
def({ id: 'bandwidth', group: 'Signal', title: 'Rise time, bandwidth & critical length',
inputs: [
{ key: 'tr', label: 'Signal rise time (10–90 %)', unit: 's', def: '1n' },
{ key: 'er', label: 'Effective εr of the line', unit: '', def: '3.3', si: false },
{ key: 'len', label: 'Trace length (for check)', unit: 'mm', def: '100', si: false }],
formula: 'BW = 0.35/tr, f_knee = 0.5/tr, v = c/√εeff, critical length = tr·v/6', ref: 'H. Johnson, High-Speed Digital Design (1/6 rule of thumb)',
compute(v) {
const vel = C0 / Math.sqrt(v.er), lc = v.tr * vel / 6 * 1000, delay = v.len / 1000 / vel;
return { outputs: [out('−3 dB bandwidth', 0.35 / v.tr, 'Hz'), out('Knee frequency', 0.5 / v.tr, 'Hz'), out('Propagation velocity', vel / C0 * 100, '% of c', { fmt: 'n', dp: 1 }),
out('Delay per mm', 1e-3 / vel, 's'), out('Critical length (terminate beyond)', lc, 'mm', { fmt: 'n', dp: 1, highlight: true }), out('Delay of this trace', delay, 's'),
out('This trace is', v.len > lc ? 'a transmission line: control impedance and terminate' : 'electrically short (lumped)', '', { fmt: 'raw' })] };
} });
def({ id: 'skin', group: 'Signal', title: 'Skin depth',
inputs: [{ key: 'f', label: 'Frequency', unit: 'Hz', def: '100M' }, { key: 'mat', label: 'Conductor', type: 'select', options: ['copper', 'aluminium', 'gold', 'silver'], def: 'copper' }],
formula: 'δ = √(2ρ / (ω·µ0·µr))', ref: 'Classical skin effect; ρ at 20 °C (Cu 1.724e-8, Al 2.65e-8, Au 2.44e-8, Ag 1.59e-8 Ω·m)',
compute(v) {
const rho = { copper: 1.724e-8, aluminium: 2.65e-8, gold: 2.44e-8, silver: 1.59e-8 }[v.mat], d = Math.sqrt(2 * rho / (2 * PI * v.f * MU0));
return { outputs: [out('Skin depth', d * 1e6, 'µm', { fmt: 'n', dp: 2, highlight: true }), out('…in mil', d / IN * 1000, 'mil', { fmt: 'n', dp: 3 }),
out('Copper weight equal to δ', d * 1e6 / OZ_UM, 'oz', { fmt: 'n', dp: 2 })] };
} });
def({ id: 'rlc', group: 'Signal', title: 'Reactance & RLC resonance',
inputs: [
{ key: 'f', label: 'Frequency', unit: 'Hz', def: '1M' },
{ key: 'L', label: 'Inductance', unit: 'H', def: '10u' },
{ key: 'C', label: 'Capacitance', unit: 'F', def: '1n' },
{ key: 'R', label: 'Resistance', unit: 'Ω', def: '10' },
{ key: 'cfg', label: 'Configuration', type: 'select', options: ['series', 'parallel'], def: 'series' }],
formula: 'XL = 2πfL, XC = 1/(2πfC), f0 = 1/(2π√LC), Q series = √(L/C)/R, Q parallel = R·√(C/L)', ref: 'Circuit theory',
compute(v) {
const w = 2 * PI * v.f, XL = w * v.L, XC = 1 / (w * v.C), f0 = 1 / (2 * PI * Math.sqrt(v.L * v.C));
const Q = v.cfg === 'series' ? Math.sqrt(v.L / v.C) / v.R : v.R * Math.sqrt(v.C / v.L);
let Z;
if (v.cfg === 'series') Z = Math.hypot(v.R, XL - XC); else { const g = 1 / v.R, b = 1 / XC - 1 / XL; Z = 1 / Math.hypot(g, b); }
return { outputs: [out('XL', XL, 'Ω'), out('XC', XC, 'Ω'), out('Resonant frequency', f0, 'Hz', { highlight: true }), out('Q', Q, '', { fmt: 'n', dp: 2 }),
out('−3 dB bandwidth', f0 / Q, 'Hz'), out('|Z| at f', Z, 'Ω')] };
} });
def({ id: 'combine', group: 'Components', title: 'Series / parallel combinations',
inputs: [
{ key: 'kind', label: 'Component', type: 'select', options: ['resistors', 'capacitors', 'inductors'], def: 'resistors' },
{ key: 'cfg', label: 'Connection', type: 'select', options: ['series', 'parallel'], def: 'parallel' },
{ key: 'list', label: 'Values (comma-separated, prefixes OK)', unit: '', def: '10k, 22k, 4k7', type: 'text' }],
formula: 'R, L series: Σx · parallel: 1/Σ(1/x) · C: the reverse', ref: 'Circuit theory (inductors assume no mutual coupling)',
compute(v) {
const xs = String(v.list).split(/[,\s;]+/).filter(Boolean).map(x => parseNum(x)); if (!xs.length || xs.some(x => !(x > 0))) return { error: 'Enter positive values separated by commas.' };
const sum = xs.reduce((a, b) => a + b, 0), rec = 1 / xs.reduce((a, b) => a + 1 / b, 0);
const cap = v.kind === 'capacitors', val = (v.cfg === 'series') !== cap ? sum : rec;
return { outputs: [out('Equivalent', val, v.kind === 'resistors' ? 'Ω' : cap ? 'F' : 'H', { highlight: true }), out('Parts', xs.length, '', { fmt: 'n', dp: 0 })] };
} });
def({ id: 'rfind', group: 'Components', title: 'Two-resistor combination finder',
blurb: 'Hit an odd value with two standard resistors in series or parallel.',
inputs: [
{ key: 'target', label: 'Target resistance', unit: 'Ω', def: '3k57' },
{ key: 'series', label: 'E-series', type: 'select', options: ['E6', 'E12', 'E24', 'E48', 'E96'], def: 'E24', setting: 'e_series' },
{ key: 'cfg', label: 'Allow', type: 'select', options: ['series or parallel', 'series only', 'parallel only'], def: 'series or parallel' }],
formula: 'series: R = Ra + Rb, parallel: R = Ra·Rb/(Ra + Rb)', ref: 'Exhaustive search over the chosen E-series (as KiCad E-Series tool)',
compute(v) {
const T = v.target; if (!(T > 0)) return { error: 'Enter a target.' };
const vals = eValuesInRange(T / 100, T * 100, v.series), rows = [];
const single = eNearest(T, v.series).nearest;
for (const a of vals) {
if (v.cfg !== 'parallel only' && a < T) { const b = eNearest(T - a, v.series).nearest; if (b >= a) rows.push(['+', a, b, a + b]); }
if (v.cfg !== 'series only' && a > T) { const bi = 1 / (1 / T - 1 / a); if (bi > 0) { const b = eNearest(bi, v.series).nearest; if (b >= a) rows.push(['‖', a, b, a * b / (a + b)]); } }
}
rows.sort((x, y) => Math.abs(x[3] - T) - Math.abs(y[3] - T));
const top = rows.slice(0, 8); if (!top.length) return { error: 'No combination found.' };
return { outputs: [out('Best combination', `${fmtSI(top[0][1], 'Ω')} ${top[0][0]} ${fmtSI(top[0][2], 'Ω')}`, '', { fmt: 'raw', highlight: true }), out('Gives', top[0][3], 'Ω'),
out('Error', (top[0][3] - T) / T * 100, '%', { fmt: 'n', dp: 3 }), out(`Single ${v.series} nearest`, single, 'Ω'), out('Single-part error', (single - T) / T * 100, '%', { fmt: 'n', dp: 2 })],
table: { head: ['Ra', '', 'Rb', 'Result', 'Error'], rows: top.map(r => [fmtSI(r[1], 'Ω'), r[0], fmtSI(r[2], 'Ω'), fmtSI(r[3], 'Ω', 5), fmtN((r[3] - T) / T * 100, '%', 3)]) } };
} });
def({ id: 'attenuator', group: 'RF', title: 'Resistive attenuator (Pi / Tee)',
inputs: [{ key: 'dB', label: 'Attenuation', unit: 'dB', def: '6', si: false }, { key: 'Z', label: 'Impedance (in = out)', unit: 'Ω', def: '50' },
{ key: 'series', label: 'Nearest', type: 'select', options: ['E24', 'E96', 'E192'], def: 'E96' }],
formula: 'K = 10^(dB/20) · Pi: Rshunt = Z(K+1)/(K−1), Rseries = Z(K²−1)/(2K) · Tee: Rseries = Z(K−1)/(K+1), Rshunt = 2ZK/(K²−1)', ref: 'Matched symmetric pads (as KiCad RF-Attenuators, Digi-Key attenuator calculator)',
compute(v) {
const K = Math.pow(10, v.dB / 20), Z = v.Z; if (!(K > 1)) return { error: 'Attenuation must be > 0 dB.' };
const pS = Z * (K + 1) / (K - 1), pR = Z * (K * K - 1) / (2 * K), tR = Z * (K - 1) / (K + 1), tS = 2 * Z * K / (K * K - 1), n = x => eNearest(x, v.series).nearest;
return { outputs: [out('Pi: shunt resistors (×2)', pS, 'Ω', { highlight: true }), out('Pi: series resistor', pR, 'Ω', { highlight: true }), out('Tee: series resistors (×2)', tR, 'Ω'), out('Tee: shunt resistor', tS, 'Ω')],
table: { head: ['Part', 'Exact', `Nearest ${v.series}`], rows: [['Pi shunt', fmtSI(pS, 'Ω'), fmtSI(n(pS), 'Ω')], ['Pi series', fmtSI(pR, 'Ω'), fmtSI(n(pR), 'Ω')], ['Tee series', fmtSI(tR, 'Ω'), fmtSI(n(tR), 'Ω')], ['Tee shunt', fmtSI(tS, 'Ω'), fmtSI(n(tS), 'Ω')]] } };
} });
def({ id: 'vswr', group: 'RF', title: 'VSWR · return loss · mismatch loss',
inputs: [{ key: 'val', label: 'Value', unit: '', def: '1.5', si: false }, { key: 'kind', label: 'Value is', type: 'select', options: ['VSWR', 'return loss (dB)', '|Γ|', 'load resistance (Ω)'], def: 'VSWR' },
{ key: 'Z0', label: 'System impedance', unit: 'Ω', def: '50' }],
formula: '|Γ| = (VSWR−1)/(VSWR+1) = 10^(−RL/20) = |ZL−Z0|/(ZL+Z0), RL = −20·log|Γ|, ML = −10·log(1−|Γ|²)', ref: 'Transmission-line theory (resistive load)',
compute(v) {
let G;
if (v.kind === 'VSWR') G = (v.val - 1) / (v.val + 1); else if (v.kind.startsWith('return')) G = Math.pow(10, -v.val / 20);
else if (v.kind === '|Γ|') G = v.val; else G = Math.abs(v.val - v.Z0) / (v.val + v.Z0);
if (!(G >= 0 && G < 1)) return { error: 'Out of range.' };
return { outputs: [out('VSWR', (1 + G) / (1 - G), ':1', { fmt: 'n', dp: 3, highlight: true }), out('|Γ|', G, '', { fmt: 'n', dp: 4 }), out('Return loss', -20 * Math.log10(G), 'dB', { fmt: 'n', dp: 2 }),
out('Mismatch loss', -10 * Math.log10(1 - G * G), 'dB', { fmt: 'n', dp: 3 }), out('Power reflected', G * G * 100, '%', { fmt: 'n', dp: 2 })] };
} });
def({ id: 'regulator', group: 'Power', title: 'Adjustable regulator feedback resistors',
inputs: [
{ key: 'mode', label: 'Solve for', type: 'select', options: ['R2 for target Vout', 'Vout from resistors'], def: 'R2 for target Vout' },
{ key: 'Vref', label: 'Reference / feedback voltage', unit: 'V', def: '1.25' },
{ key: 'Iadj', label: 'ADJ/FB pin current (0 for most switchers)', unit: 'A', def: '50u' },
{ key: 'R1', label: 'R1 (between OUT and ADJ/FB → ground side: see note)', unit: 'Ω', def: '240' },
{ key: 'Vout', label: 'Target output', unit: 'V', def: '5', show: 'R2 for target Vout' },
{ key: 'R2', label: 'R2 (ADJ/FB to ground)', unit: 'Ω', def: '750', show: 'Vout from resistors' },
{ key: 'series', label: 'E-series', type: 'select', options: ['E24', 'E96'], def: 'E96' }],
formula: 'Vout = Vref·(1 + R2/R1) + Iadj·R2', ref: 'TI LM317 datasheet (Vref 1.25 V, Iadj 50 µA typ); same form for switcher FB dividers with Iadj ≈ 0',
compute(v) {
const vo = R2 => v.Vref * (1 + R2 / v.R1) + v.Iadj * R2;
if (v.mode.startsWith('R2')) {
const R2 = (v.Vout - v.Vref) / (v.Vref / v.R1 + v.Iadj); if (!(R2 > 0)) return { error: 'Target must exceed Vref.' };
const R2n = eNearest(R2, v.series).nearest;
return { outputs: [out('Exact R2', R2, 'Ω'), out(`Nearest ${v.series}`, R2n, 'Ω', { highlight: true }), out('Resulting Vout', vo(R2n), 'V'), out('Error', (vo(R2n) - v.Vout) / v.Vout * 100, '%', { fmt: 'n', dp: 2 }), out('Divider current', v.Vref / v.R1, 'A')],
notes: ['R1 sits between OUT and ADJ/FB, R2 between ADJ/FB and ground (LM317 naming). LM317 needs ≥ ~5 mA minimum load: R1 = 240 Ω gives 5.2 mA.'] };
}
return { outputs: [out('Output voltage', vo(v.R2), 'V', { highlight: true }), out('Divider current', v.Vref / v.R1, 'A')] };
} });
def({ id: 'mosfet', group: 'Power', title: 'MOSFET conduction & switching loss',
inputs: [
{ key: 'V', label: 'Switched (off-state) voltage', unit: 'V', def: '24' },
{ key: 'I', label: 'Drain current (on)', unit: 'A', def: '5' },
{ key: 'Rds', label: 'Rds(on) at 25 °C', unit: 'Ω', def: '10m' },
{ key: 'kT', label: 'Rds(on) hot multiplier (datasheet, e.g. 1.5)', unit: '×', def: '1.5', si: false },
{ key: 'D', label: 'On duty cycle', unit: '%', def: '50', si: false },
{ key: 'fsw', label: 'Switching frequency (0 = static)', unit: 'Hz', def: '100k' },
{ key: 'tr', label: 'Rise time', unit: 's', def: '20n' },
{ key: 'tf', label: 'Fall time', unit: 's', def: '20n' },
{ key: 'Qg', label: 'Total gate charge', unit: 'C', def: '30n', si: true },
{ key: 'Vgs', label: 'Gate drive voltage', unit: 'V', def: '10' }],
formula: 'Pcond = I²·Rds·k·D, Psw ≈ ½·V·I·(tr+tf)·fsw, Pgate = Qg·Vgs·fsw (in the driver)', ref: 'First-order hard-switching estimate (e.g. Infineon/TI MOSFET loss app notes)',
compute(v) {
const Pc = v.I * v.I * v.Rds * v.kT * v.D / 100, Ps = 0.5 * v.V * v.I * (v.tr + v.tf) * v.fsw, Pg = v.Qg * v.Vgs * v.fsw;
return { outputs: [out('Conduction loss', Pc, 'W'), out('Switching loss', Ps, 'W'), out('Total in the MOSFET', Pc + Ps, 'W', { highlight: true }), out('Gate-drive power (driver)', Pg, 'W'), out('Average gate current', v.Qg * v.fsw, 'A')],
notes: ['Ignores reverse recovery, Coss and ringing losses; add margin at high frequency.'] };
} });
def({ id: 'pdn', group: 'Power', title: 'PDN target impedance',
inputs: [{ key: 'V', label: 'Rail voltage', unit: 'V', def: '1.0' }, { key: 'rip', label: 'Allowed ripple', unit: '%', def: '3', si: false }, { key: 'dI', label: 'Transient current step', unit: 'A', def: '5' },
{ key: 'fmax', label: 'Highest frequency of concern', unit: 'Hz', def: '100M' }],
formula: 'Z_target = V·ripple / ΔI', ref: 'L. D. Smith et al., "Power Distribution System Design Methodology" (target-impedance method)',
compute(v) {
const Z = v.V * v.rip / 100 / v.dI;
return { outputs: [out('Target impedance', Z, 'Ω', { highlight: true }), out('Max ESL per decap at fmax', Z / (2 * PI * v.fmax), 'H')], notes: ['Keep |Z| below the target from DC to fmax with VRM + bulk + ceramics + plane capacitance.'] };
} });
def({ id: 'opamp', group: 'Signal', title: 'Op-amp gain & bandwidth',
inputs: [
{ key: 'cfg', label: 'Configuration', type: 'select', options: ['non-inverting', 'inverting'], def: 'non-inverting' },
{ key: 'Rf', label: 'Feedback resistor Rf', unit: 'Ω', def: '10k' },
{ key: 'Rg', label: 'Gain / input resistor Rg', unit: 'Ω', def: '1k' },
{ key: 'gbw', label: 'Gain-bandwidth product', unit: 'Hz', def: '10M' },
{ key: 'sr', label: 'Slew rate', unit: 'V/s', def: '10M' },
{ key: 'Vpk', label: 'Output amplitude (peak)', unit: 'V', def: '2' }],
formula: 'non-inv: G = 1 + Rf/Rg · inv: G = −Rf/Rg · f−3dB = GBW/(1 + Rf/Rg) · full-power BW = SR/(2π·Vpk)', ref: 'Voltage-feedback op-amp, single-pole model',
compute(v) {
const ng = 1 + v.Rf / v.Rg, G = v.cfg === 'inverting' ? -v.Rf / v.Rg : ng;
return { outputs: [out('Gain', G, 'V/V', { fmt: 'n', dp: 3, highlight: true }), out('Gain', 20 * Math.log10(Math.abs(G)), 'dB', { fmt: 'n', dp: 2 }), out('Noise gain', ng, 'V/V', { fmt: 'n', dp: 3 }),
out('Small-signal −3 dB bandwidth', v.gbw / ng, 'Hz'), out('Full-power bandwidth', v.sr / (2 * PI * v.Vpk), 'Hz'), out('Input impedance', v.cfg === 'inverting' ? v.Rg : Infinity, 'Ω')] };
} });
def({ id: 'timer555', group: 'Signal', title: '555 timer',
inputs: [
{ key: 'mode', label: 'Mode', type: 'select', options: ['astable', 'monostable'], def: 'astable' },
{ key: 'RA', label: 'RA', unit: 'Ω', def: '1k', show: 'astable' }, { key: 'RB', label: 'RB', unit: 'Ω', def: '10k', show: 'astable' },
{ key: 'R', label: 'R', unit: 'Ω', def: '100k', show: 'monostable' }, { key: 'C', label: 'C', unit: 'F', def: '10n' }],
formula: 'astable: f = 1.44/((RA+2RB)·C), tH = 0.693(RA+RB)C, tL = 0.693·RB·C · monostable: t = 1.1·R·C', ref: 'TI NE555 datasheet (SLFS022)',
compute(v) {
if (v.mode === 'monostable') return { outputs: [out('Pulse width', 1.1 * v.R * v.C, 's', { highlight: true })] };
const tH = 0.693 * (v.RA + v.RB) * v.C, tL = 0.693 * v.RB * v.C;
return { outputs: [out('Frequency', 1.44 / ((v.RA + 2 * v.RB) * v.C), 'Hz', { highlight: true }), out('High time', tH, 's'), out('Low time', tL, 's'), out('Duty cycle', tH / (tH + tL) * 100, '%', { fmt: 'n', dp: 1 })] };
} });
def({ id: 'ntc', group: 'Measurement', title: 'NTC thermistor (β model)',
inputs: [
{ key: 'mode', label: 'Solve for', type: 'select', options: ['resistance at temperature', 'temperature from resistance'], def: 'resistance at temperature' },
{ key: 'R25', label: 'R at 25 °C', unit: 'Ω', def: '10k' }, { key: 'B', label: 'β (B25/85)', unit: 'K', def: '3435', si: false },
{ key: 'T', label: 'Temperature', unit: '°C', def: '60', si: false, show: 'resistance at temperature' },
{ key: 'R', label: 'Measured resistance', unit: 'Ω', def: '3k', show: 'temperature from resistance' },
{ key: 'Rp', label: 'Pull-up to Vcc (divider, optional)', unit: 'Ω', def: '10k' }, { key: 'Vcc', label: 'Divider supply', unit: 'V', def: '3.3' }],
formula: 'R(T) = R25·exp(β·(1/T − 1/298.15)), 1/T = 1/298.15 + ln(R/R25)/β', ref: 'β-parameter model (±1 °C typical over 0–100 °C; Steinhart-Hart is tighter)',
compute(v) {
let R, T;
if (v.mode.startsWith('resistance')) { T = v.T; R = v.R25 * Math.exp(v.B * (1 / (T + 273.15) - 1 / 298.15)); }
else { R = v.R; T = 1 / (1 / 298.15 + Math.log(R / v.R25) / v.B) - 273.15; }
const o = [out('Resistance', R, 'Ω', { highlight: v.mode.startsWith('resistance') }), out('Temperature', T, '°C', { fmt: 'n', dp: 2, highlight: !v.mode.startsWith('resistance') })];
if (Number.isFinite(v.Rp)) o.push(out('Divider output (NTC to ground)', v.Vcc * R / (R + v.Rp), 'V'));
return { outputs: o };
} });
def({ id: 'ppm', group: 'Interfaces', title: 'Frequency tolerance (ppm)',
inputs: [{ key: 'f', label: 'Nominal frequency', unit: 'Hz', def: '32.768k' }, { key: 'ppm', label: 'Tolerance', unit: 'ppm', def: '20', si: false }, { key: 'meas', label: 'Measured frequency (optional)', unit: 'Hz', def: '' }],
formula: 'Δf = f·ppm/10⁶, drift per day = 86400·ppm/10⁶ s', ref: 'Definition',
compute(v) {
const o = [out('Frequency error', v.f * v.ppm * 1e-6, 'Hz', { highlight: true }), out('Clock drift per day', 86400 * v.ppm * 1e-6, 's'), out('Clock drift per month', 30 * 86400 * v.ppm * 1e-6, 's')];
if (Number.isFinite(v.meas)) o.push(out('Measured error', (v.meas - v.f) / v.f * 1e6, 'ppm', { fmt: 'n', dp: 2 }));
return { outputs: o };
} });
def({ id: 'spiral', group: 'RF', title: 'Planar spiral inductor',
inputs: [{ key: 'shape', label: 'Shape', type: 'select', options: ['square', 'hexagonal', 'octagonal'], def: 'square' }, { key: 'n', label: 'Turns', unit: '', def: '5', si: false },
{ key: 'dout', label: 'Outer diameter', unit: 'mm', def: '5', si: false }, { key: 'din', label: 'Inner diameter', unit: 'mm', def: '2', si: false }],
formula: 'L = K1·µ0·n²·d_avg / (1 + K2·ρ), d_avg = (dout+din)/2, ρ = (dout−din)/(dout+din)', ref: 'Modified Wheeler: Mohan, Hershenson, Boyd & Lee, IEEE JSSC 34(10), 1999 (K1/K2: square 2.34/2.75, hex 2.33/3.82, oct 2.25/3.55)',
compute(v) {
const [K1, K2] = { square: [2.34, 2.75], hexagonal: [2.33, 3.82], octagonal: [2.25, 3.55] }[v.shape];
const davg = (v.dout + v.din) / 2 / 1000, rho = (v.dout - v.din) / (v.dout + v.din); if (!(v.dout > v.din)) return { error: 'Outer diameter must exceed inner.' };
return { outputs: [out('Inductance', K1 * MU0 * v.n * v.n * davg / (1 + K2 * rho), 'H', { highlight: true }), out('Fill ratio ρ', rho, '', { fmt: 'n', dp: 3 })], notes: ['About ±8 % against measurement in the paper; ignores a nearby ground plane, which lowers L.'] };
} });
def({ id: 'units', group: 'Components', title: 'Length & copper unit converter',
inputs: [{ key: 'val', label: 'Value', unit: '', def: '10', si: false }, { key: 'u', label: 'Unit', type: 'select', options: ['mm', 'mil', 'inch', 'µm', 'oz copper'], def: 'mil' }],
formula: '1 mil = 0.0254 mm, 1 oz/ft² Cu ≈ 34.8 µm (1.37 mil)', ref: 'Definitions',
compute(v) {
const mm = { mm: v.val, mil: v.val * IN, inch: v.val * 25.4, 'µm': v.val / 1000, 'oz copper': v.val * OZ_UM / 1000 }[v.u];
return { outputs: [out('mm', mm, 'mm', { fmt: 'n', dp: 4 }), out('mil', mm / IN, 'mil', { fmt: 'n', dp: 3 }), out('inch', mm / 25.4, 'in', { fmt: 'n', dp: 5 }),
out('µm', mm * 1000, 'µm', { fmt: 'n', dp: 2 }), out('as copper weight', mm * 1000 / OZ_UM, 'oz', { fmt: 'n', dp: 3 })] };
} });
// ---------------------------------------------------------------- driver
function inputsFor(calc, raw = {}, settings = {}) {
const v = {};
for (const f of calc.inputs) {
let r = raw[f.key];
if (r === undefined) r = (f.setting && settings[f.setting] !== undefined && f.type === 'select' && f.options.includes(settings[f.setting])) ? settings[f.setting]
: (f.setting && settings[f.setting] !== undefined ? String(settings[f.setting]) : f.def);
v[f.key] = (f.type === 'select' || f.type === 'text') ? String(r) : parseNum(r, f.si !== false);
}
return v;
}
function visible(field, v) {
if (!field.show) return true;
const s = Array.isArray(field.show) ? field.show : [field.show];
return Object.values(v).some(x => s.includes(x));
}
function run(id, raw = {}, settings = {}) {
const calc = CALCS.find(c => c.id === id);
if (!calc) return { ok: false, error: `Unknown calculator "${id}".`, known: CALCS.map(c => c.id) };
const v = inputsFor(calc, raw, settings);
let res;
try { res = calc.compute(v) || {}; } catch (e) { res = { error: String(e.message || e) }; }
const outputs = (res.outputs || []).map(o => ({ ...o, text: o.fmt === 'raw' ? String(o.value) : o.fmt === 'n' ? fmtN(o.value, o.unit, o.dp ?? 3) : fmtSI(o.value, o.unit) }));
return { ok: !res.error, calc: id, title: calc.title, inputs: v, error: res.error, outputs, table: res.table, notes: res.notes || [], warnings: res.warnings || [], formula: calc.formula, ref: calc.ref };
}
function catalog() {
return CALCS.map(c => ({ id: c.id, group: c.group, title: c.title, blurb: c.blurb || '', formula: c.formula, ref: c.ref,
inputs: c.inputs.map(f => ({ key: f.key, label: f.label, unit: f.unit || '', type: f.type || 'number', options: f.options, def: f.def, show: f.show, si: f.si !== false, setting: f.setting })) }));
}
// Settings shared by the server (validated, persisted) and the static wiki build (in memory). ui: shown on the wiki page.
const SETTINGS_SPEC = {
e_series: { type: 'enum', values: ['E6', 'E12', 'E24', 'E48', 'E96', 'E192'], def: 'E24', label: 'Default E-series for resistor picks', ui: true },
trace_dT: { type: 'number', min: 1, max: 100, def: 10, label: 'Default trace temperature rise (°C)', ui: true },
show_formulas: { type: 'bool', def: true, label: 'Show formula and reference under results', ui: true },
show_schematics: { type: 'bool', def: true, label: 'Show the schematic / cross-section drawing', ui: true },
ttl_hours: { type: 'number', min: 1, max: 720, def: 24, label: 'Shut the server down after this many idle hours' },
default_surface: { type: 'enum', values: ['wv', 'pup'], def: 'wv', label: 'Where "hwcalc show" opens the app' },
};
return { CALCS, SETTINGS_SPEC, run, catalog, parseNum, fmtSI, fmtN, eNearest, eValuesInRange, E_BASE, colourBands, eia96, visible, inputsFor };
});