Electronics Unit Library

← All samples

electronics-units.dplylib
1---
2version: 1.0.0
3---
4# electronics-units.dplylib
5# Common units for electrical engineering / electronics.
6#
7# Import this library and use the units inline (12 V, 470 Ohm, 100 nF) or with
8# convert(): convert(1, "MOhm", "kOhm"), convert(100, "nF", "uF"), ...
9# Only units of the same kind convert into each other. Prefixes use the ASCII
10# alias "u" for micro (uF, uH, uA); the "µ" spelling works inside convert() text.
11
12# ── Base units ───────────────────────────────────────────────────────────────
13defunit("A") # ampere — electric current
14defunit("V") # volt — voltage
15
16# ── Current prefixes ─────────────────────────────────────────────────────────
17defunit("nA", 0.000000001, "A")
18defunit("uA", 0.000001, "A")
19defunit("µA", 0.000001, "A")
20defunit("mA", 0.001, "A")
21defunit("kA", 1000, "A")
22
23# ── Voltage prefixes ─────────────────────────────────────────────────────────
24defunit("uV", 0.000001, "V")
25defunit("µV", 0.000001, "V")
26defunit("mV", 0.001, "V")
27defunit("kV", 1000, "V")
28defunit("MV", 1000000, "V")
29
30# ── Resistance — ohm (Ω = V/A) ───────────────────────────────────────────────
31defunit("Ohm", "V/A")
32defunit("mOhm", 0.001, "Ohm")
33defunit("kOhm", 1000, "Ohm")
34defunit("MOhm", 1000000, "Ohm")
35defunit("GOhm", 1000000000, "Ohm")
36
37# ── Conductance — siemens (S = A/V = 1/Ω) ────────────────────────────────────
38defunit("S", "A/V")
39defunit("mS", 0.001, "S")
40defunit("uS", 0.000001, "S")
41
42# ── Capacitance — farad (F = A*s/V) ──────────────────────────────────────────
43defunit("F", "A*s/V")
44defunit("mF", 0.001, "F")
45defunit("uF", 0.000001, "F")
46defunit("µF", 0.000001, "F")
47defunit("nF", 0.000000001, "F")
48defunit("pF", 0.000000000001, "F")
49
50# ── Inductance — henry (H = V*s/A) ───────────────────────────────────────────
51defunit("H", "V*s/A")
52defunit("mH", 0.001, "H")
53defunit("uH", 0.000001, "H")
54defunit("µH", 0.000001, "H")
55defunit("nH", 0.000000001, "H")
56
57# ── Charge — ampere-hours (battery capacity, Ah = A*h) ───────────────────────
58defunit("Ah", "A*h")
59defunit("mAh", 0.001, "Ah")
60
61# ── Quick examples (visible when you open this file directly) ─────────────────
62convert(1, "MOhm", "kOhm") # 1000 kOhm
63convert(100, "nF", "uF") # 0.1 uF
64convert(2200, "uF", "mF") # 2.2 mF
65convert(1, "F", "pF") # 1e12 pF
66convert(2000, "mAh", "Ah") # 2 Ah
67