main.js entry 4.2 KB
Copy main.js
SimKit.define({
title: "Zener regulator: input ripple vs. regulated output",
dt: 2e-5, speed: 0.05, height: 320,
params: {
V: { label: "Mean input voltage", unit: "V", min: 4, max: 30, value: 20, step: 0.5 },
ripple: { label: "Input ripple (amplitude)", unit: "V", min: 0, max: 4, value: 2, step: 0.1 },
Vz: { label: "Zener voltage", unit: "V", min: 3, max: 15, value: 10, step: 0.1 },
R: { label: "Series resistance", unit: "Ω", min: 50, max: 5000, value: 400, log: true },
RL: { label: "Load resistance", unit: "Ω", min: 50, max: 10000, value: 1000, log: true },
rip: { label: "Ripple on (1) / off (0)", unit: "", min: 0, max: 1, value: 1, step: 1 },
f: { label: "Ripple frequency", unit: "Hz", min: 1, max: 200, value: 20, log: true }
},
init(P, S) {
const c = new SimKit.Circuit();
const a = c.node(), b = c.node(), m = c.node();
S.c = c; S.a = a; S.b = b; S.m = m;
S.src = c.V(a, 0, 0); // source with ripple
S.r = c.R(a, b, P.R); // series resistor
S.rl = c.R(b, 0, P.RL); // load
S.cb = c.C(b, 0, 1e-7); // small parasitic capacitance: keeps the on/off switching stable
// Zener = ideal voltage source Vz behind a switchable resistor
// (on: a few ohms, off: practically open circuit)
S.rz = c.R(b, m, 1e9);
S.vz = c.V(m, 0, P.Vz);
S.on = false;
},
step(dt, P, S, t) {
S.src.value = P.V + (P.rip >= 0.5 ? P.ripple * Math.sin(2 * Math.PI * P.f * t) : 0);
S.r.value = P.R;
S.rl.value = P.RL;
S.vz.value = P.Vz;
// Zener conducts in reverse only once the node voltage reaches Vz
S.on = S.c.v[S.b] >= P.Vz * 0.999;
S.rz.value = S.on ? 2 : 1e9;
S.vzLine = P.Vz;
S.c.step(dt);
},
draw(g, W, H, P, S) {
const sch = SimKit.sch, v = S.c.v, vm = Math.max(P.V + P.ripple, 1);
const col = n => SimKit.voltColor(v[n], vm);
const x0 = W * 0.12, x1 = W * 0.50, x2 = W * 0.82, yt = H * 0.28, yb = H * 0.78;
const ym = (yt + yb) / 2;
// wires, source, series resistor, load
sch.wire(g, x0, yb, x2, yb, "#555");
sch.vsource(g, x0, yb, x0, yt, col(S.a));
sch.resistor(g, x0, yt, x1 - 40, yt, col(S.a));
sch.wire(g, x1 - 40, yt, x2, yt, col(S.b));
sch.resistor(g, x2, yt, x2, yb, col(S.b));
sch.ground(g, (x0 + x2) / 2, yb);
// Zener symbol: uses sch.zener if SimKit has it, otherwise draws it by hand
if (sch.zener) {
sch.zener(g, x1 - 40, yt, x1 - 40, yb, col(S.b));
} else {
const x = x1 - 40, y1 = ym - 14, y2 = ym + 14, c = col(S.b);
sch.wire(g, x, yt, x, y1, c);
sch.wire(g, x, y2, x, yb, "#555");
g.save();
g.strokeStyle = c; g.fillStyle = c; g.lineWidth = 2;
g.beginPath(); g.moveTo(x - 13, y2); g.lineTo(x + 13, y2); g.lineTo(x, y1); g.closePath(); g.stroke();
g.beginPath(); // cathode bar with the typical Zener "bent" ends
g.moveTo(x - 13, y1 - 5); g.lineTo(x - 13, y1); g.lineTo(x + 13, y1); g.lineTo(x + 13, y1 + 5);
g.stroke();
g.restore();
}
// labels
const iz = Math.max(S.rz.i, 0), il = S.rl.i, it = S.r.i;
sch.label(g, "R = " + SimKit.fmt(P.R, "Ω"), (x0 + x1 - 40) / 2, yt - 18);
sch.label(g, "Vz = " + SimKit.fmt(P.Vz, "V"), x1 + 4, ym - 6);
sch.label(g, "Iz = " + SimKit.fmt(iz, "A"), x1 + 4, ym + 12, "#2a8a8a");
sch.label(g, "RL = " + SimKit.fmt(P.RL, "Ω"), x2 + 44, ym - 6);
sch.label(g, "IL = " + SimKit.fmt(il, "A"), x2 + 44, ym + 12, "#3070c0");
sch.label(g, "Vout = " + SimKit.fmt(v[S.b], "V"), (x1 + x2) / 2 + 20, yt - 18);
// current into the node
sch.arrow(g, x0 + 30, yt + 16, x1 - 70, yt + 16, "#d03030");
sch.label(g, "IT = " + SimKit.fmt(it, "A"), (x0 + x1 - 40) / 2, yt + 34, "#d03030");
// status line
const pz = P.Vz * iz;
const msg = S.on
? "Zener regulates · Pz = " + SimKit.fmt(pz, "W")
: "Zener off: not enough voltage or current to regulate";
sch.label(g, msg, W / 2, H - 12, S.on ? "#2a8a2a" : "#c05000");
},
scope: {
span: P => 4 / P.f, unit: "V",
traces: [
{ label: "Input", color: "#5aa0ff", get: S => S.c.v[S.a] },
{ label: "Output", color: "#ff6060", get: S => S.c.v[S.b] },
{ label: "Vz", color: "#888888", get: S => S.vzLine }
]
}
});