Embedded HandbookApps
All sections › VIII. Electronics around the microcontroller
STM32F103, STM32G431 · level intermediate · 15 min read
Open in the interactive handbook

49б. Magnetic field and induction: current creates a field, the coil, mutual inductance, the transformer, self-induced EMF and switching spikes

In section 49а charge created an electric field, and capacitance and capacitive crosstalk grew out of it. Here is the second half of the picture: moving charge, that is current, creates a magnetic field. Out of it grow the inductance of a coil and of a loop on the board, the voltage spike when a switch interrupts relay current, the transformer and noise coupled from a neighbouring loop through the magnetic field. Every formula comes with a calculation on board values: a relay coil, a trace over ground, an STM32 pin edge.

Sources: the magnetic constant is CODATA 2022 from scipy.constants (scipy 1.18.1, numpy 2.3.3, image stm-ml:1); copper resistivity is 1.7241·10⁻⁸ Ω·m per the IEC 60028 annealed copper standard (the document is not on disk). The relay coil is 12 V, 30 mA, about 100 mH, as in section 51.2. The turn-off spike is computed twice: by solving the circuit equations in Python (script calc49b.py) and by a model in ngspice 39 (image stm-lab:1, file kick49b.cir). Loop inductances come from formulas for wire segments, the coil from the sum of mutual inductances of its turns. There are no hardware measurements.

What it is called in the documentation

Original Meaning
magnetic flux density (B), tesla magnetic induction, tesla
magnetic flux (Φ), weber magnetic flux, weber
inductance, self-inductance inductance, self-induction
back EMF, inductive kickback self-induced EMF, turn-off spike
flyback (freewheeling) diode flyback diode
TVS diode, clamp voltage clamp
snubber snubber (damping RC network)
mutual inductance, coupling coefficient (k) mutual inductance, coupling coefficient
leakage inductance leakage inductance
saturation current (Isat) saturation current
eddy currents, skin effect eddy currents, skin effect
current return path, loop area return path, loop area

49б.1 Current creates a field

The magnetic field is described by the flux density B, unit tesla (T). A tesla is a lot; on and near a board we deal with micro- and millitesla. The source of the field is current. The magnetic constant is μ₀ = 1.25663706127·10⁻⁶ H/m (CODATA 2022).

What Formula Example Field
long straight wire B = μ0·I / (2π·r) 1 A, 1 cm from the wire 20.0 µT
circular turn, at the centre B = μ0·I / (2R) 1 A, radius 5 mm 126 µT
long coil, inside B = μ0·N·I / l 100 turns over 20 mm, 0.1 A 0.628 mT

The field lines of a wire are circles around it; the field falls as 1/r. A wire bent into a turn gathers the field into the middle, and a coil of many turns adds up the fields of the turns. That is why a relay, an inductor and a transformer are coils: a strong field from a small current.

Where you see it on the board. The Earth's field is tens of microtesla (the model in section 99г uses 50 µT). A wire carrying amperes a few centimetres away gives as much — which is why a magnetometer is thrown off by power wires and why the supply and return wires are run together; the heading error is computed in section 99г.6, calibration in 99б and 99е. How a sensor turns the field into a voltage (Hall effect, magnetoresistance) is section 49е.


49б.2 Flux and inductance

Magnetic flux Φ is the field times the area it passes through; unit weber (Wb = T·m²). If a loop of N turns links flux Φ from its own current I, its inductance is

L = N·Φ / I

The unit is the henry (H). Inductance is a property of shape: how much flux a loop "catches" from its own current. It grows with the square of the number of turns (the field is N times stronger and N turns link it) and with area.

Air-core coil. 100 turns of 0.2 mm wire on a 10 mm former, winding length 20 mm. The sum of the self-inductances of the turns and the mutual inductances of all pairs gives 39.8 µH. Wheeler's formula for a single-layer coil gives 40.2 µH, a difference of 0.9 %. The long-coil formula μ0·N²·S/l gives 49.3 µH — 24 % more: the coil is only two diameters long and the field "leaks out" at the ends. Each turn links 39.8 nWb at 0.1 A.

Relay coil from section 51.2: 12 V, 400 Ω resistance, 30.0 mA current, inductance about 100 mH. Energy in the field — W = L·I²/2 = 45.0 µJ. Time constant L/R = 0.250 ms. These numbers are needed in 49б.4.

A loop on the board. Pin current leaves along a trace and returns via ground; trace plus return path is one turn. The inductance of such a loop is computed from segments: each side has its own inductance, opposite sides have a mutual one with a minus sign (currents in opposite directions):

def l_rect(w, h, a):
    """Прямоугольная петля w×h из провода с эквивалентным радиусом a: частичные индуктивности сторон."""
    return 2 * (m_par(w, a) + m_par(h, a)) - 2 * (m_par(w, h) + m_par(h, w))

The 0.25 mm trace is replaced by a round wire with an equivalent radius of 55.9 µm (the geometric mean distance of a thin strip).

Loop of 0.25 mm trace Inductance Circle of the same area
50 × 20 mm 153 nH 131 nH
20 × 10 mm 56.6 nH 50.5 nH
50 × 2 mm 73.4 nH 33.3 nH
10 × 1 mm 12.2 nH 7.94 nH

Loop area matters first: 50 × 2 mm has ten times less area and half the inductance of 50 × 20 mm. A narrow loop is worse than a circle of the same area — it has a lot of wire per unit of area.

With a solid ground under the trace the return current flows right under it (section 130.2), and the loop is the trace and its image in the ground. The per-length inductance of a microstrip from Hammerstad's formulas: a 0.25 mm trace 1.5 mm above ground — 7.75 nH/cm, 0.2 mm above — 3.81 nH/cm.


49б.3 Faraday's law and self-induction

When the flux through a loop changes, an EMF appears in the loop:

EMF = −dΦ/dt

The minus sign is Lenz's rule: the induced current opposes the change of flux. It does not matter why the flux changes — a magnet moves, the current in a neighbouring wire or in the loop itself changes. This is how the transformer, the generator, the inductive sensor and all magnetic interference work.

Mains pickup example. A 50 × 20 mm loop in a 50 Hz field of 1 µT amplitude gets an EMF of 0.314 µV amplitude, in 10 µT — 3.14 µV. On a board this is small; it gets dangerous where the loop area is metres of sensor cable or supply and return wires run apart.

Self-induction. If the flux comes from the loop's own current, Φ·N = L·I, and

U = −L·dI/dt

A coil does not let the current change in a jump: to raise or lower the current takes a voltage, the larger the faster the change. This is the "inertia" of current in a converter inductor (section 58), the turn-off spike (49б.4) and ground noise on edges.

A pin edge: let 20 mA rise in 5 ns — 4.00 A/µs. Over 50 mm of path with the inductance from the formulas above:

Current path over 50 mm Inductance L·dI/dt
trace 1.5 mm above ground 38.7 nH 155 mV
trace 0.2 mm above ground 19.0 nH 76.1 mV
no ground, return as a 50 × 20 mm loop 153 nH 614 mV

This voltage drops along the current path, not on the load: "ground" at the far end of the trace jumps relative to ground at the microcontroller. Hence the decoupling rule — a capacitor at the supply pin and a short loop to ground (section 130.3) — and the demand for solid ground under fast signals.


49б.4 The spike when a coil is switched off

30 mA flows through the relay coil and the switch opens. The coil current cannot vanish instantly, and it keeps flowing — wherever it is allowed to. If there is nowhere, it charges the capacitance of the switch node (the transistor output and wiring, 100 pF in the model), and the voltage rises until all the field energy has moved into the capacitance: U ≈ Usupply + I·√(L/C) = 958 V. The calculation with losses in the coil gives 949 V, followed by ringing with a 19.9 µs period. In practice the transistor breaks down earlier: it absorbs the energy in avalanche, and repeated at every turn-off this kills it.

Turning off a 12 V, 100 mH, 400 Ω relay coil: on the left the circuit — 12 V supply, coil with its resistance, node X, a switch to ground and a flyback diode across the coil, which can be replaced by a diode with a 24 V zener or an RC network; on the right three plots: without protection the switch voltage rings with a 949 V peak and a 19.9 µs period; with a diode — 12.8 V, with diode and zener — 36.8 V until the current has decayed, then ringing, with the RC network — no more than 12.0 V; coil current decays to 3 mA in 0.0864 ms with diode and zener, in 0.476 ms with a diode and in 0.635 ms with the RC network

Protection gives the current a path and sets the voltage at which it decays. Five variants, the switch opens instantly:

Protection Peak at the switch Current to 10 % Energy in the coil In the clamp In the RC resistor
none 949 V 1.14 ms 44.7 µJ — —
diode across the coil 12.8 V 0.476 ms 40.4 µJ — —
diode + 24 V zener 36.8 V 0.0864 ms 10.7 µJ 33.1 µJ —
RC 400 Ω, 0.68 µF across the coil 12.0 V 0.635 ms 46.8 µJ — 46.7 µJ
33 V clamp from switch to ground 33.8 V 0.0946 ms 11.8 µJ 50.2 µJ —

The ngspice model with the same elements gave the same peaks and times within 2 %. The diode parameters in both models are IS 2.52 nA, N 1.752, RS 0.568 Ω, a widely used 1N4148 model; they were not checked against a datasheet. The zener and the clamp are a voltage source with the same diode, without their own breakdown resistance.

What follows:

A motor or a solenoid is the same, only with more energy. A motor also generates its own EMF when turning, and when braking it goes back into the supply. Choosing the switch and its protection — section 52, current decay modes in a stepper winding (slow through a diode and fast through the supply) — 57е, braking and coasting of a brushed motor — 57з, clamps on microcontroller pins — 62.9.


49б.5 Mutual inductance and the transformer

If the field of one loop passes through another, they are linked by a mutual inductance M: current in the first induces EMF = −M·dI/dt in the second. How completely is shown by the coupling coefficient k = M / √(L1·L2), from 0 to 1.

Two coaxial turns of radius 10 mm made of 0.2 mm wire; the inductance of one is 62.0 nH:

Distance between turns M k
1 mm 30.0 nH 0.48
5 mm 11.1 nH 0.18
10 mm 4.94 nH 0.080
20 mm 1.42 nH 0.023

Even close together in air the coupling is under one half. That is why transformer windings sit on a common core: it gathers almost all the flux, and k approaches one.

Transformer. The voltage per turn is dΦ/dt, the same for all turns on a common flux, so U2/U1 = N2/N1. Current goes the other way: input and output power are the same (without losses). Imperfect coupling is the leakage inductance L1·(1 − k²): the part of the primary not linked to the secondary. Let it be 10 : 1, primary 1 mH, 100 V in:

k Output without load Leakage
0.99 9.90 V 19.9 µH
0.9 9.00 V 190 µH
0.5 5.00 V 750 µH

Leakage in a flyback converter is the same "relay coil" whose current the switch interrupts every period: hence snubbers on the switch (section 58). Isolation with a transformer — section 54.

Inductive crosstalk between loops. The aggressor is a 0.25 mm trace with a current edge of 20 mA in 5 ns, the victim is a neighbouring trace, both 50 mm long, the victim returning via ground under it. EMF in the victim loop is M·dI/dt:

Layout Neighbours 1 mm apart 3 mm 10 mm
solid ground 1.5 mm below 11.5 nH, 46.1 mV 3.47 nH, 13.9 mV 0.431 nH, 1.72 mV
solid ground 0.2 mm below 0.742 nH, 2.97 mV 0.0881 nH, 0.352 mV 0.00799 nH, 0.0320 mV
no ground, returns 20 mm away on the outside 23.8 nH, 95.0 mV 14.1 nH, 56.4 mV 5.88 nH, 23.5 mV

The calculation is two-dimensional: the traces are taken as long compared to the distances, the ends are ignored. Ground in the cross-section is replaced by the image of the trace — exact for a solid plane; the check against M' = μ0/(4π)·ln(1 + (2h/s)²) agrees to six digits.

What it shows: with ground under the traces the coupling falls with distance as a square — the magnetic field of the pair "trace and its return" dies out quickly. Without ground the loops are large, the field of a single current dies out slowly, and at 10 mm the crosstalk is 14 times stronger. How to reduce it, in order of effect:

  1. return path right alongside — solid ground under the signal or the return wire next to it (twisted pair); the aggressor loop area is small and its field does not reach far;
  2. closer ground — 0.2 mm instead of 1.5 mm gives 16 times less at 1 mm and even more at larger distances;
  3. distance — with ground, three times farther gives 3–11 times less;
  4. a small victim loop — it catches flux by area;
  5. a slower edge — the EMF is proportional to dI/dt.

Capacitive crosstalk on the same traces — section 49а.5; when a trace is long compared to the edge it is a line, section 49в.


49б.6 The core, saturation, eddy currents

Ferromagnetic material. Iron and ferrites contain regions with their own magnetisation; an external field turns them, and the field inside is many times stronger than in air (relative permeability μr). A coil on a core gets the same inductance with fewer turns. But the regions can only turn up to a certain field: beyond it they are all aligned and μr falls towards one — saturation. Inductance collapses and the current grows without limit (numbers for a buck converter — section 58.2).

That is why inductors have a gap: a non-magnetic slit in the core. Almost all the "magnetic resistance" is concentrated in it, the inductance L ≈ μ0·N²·S/gap depends little on ferrite spread, and saturation comes at a higher current. An example with arbitrary dimensions: 20 turns, 10 mm² cross-section, 0.5 mm gap — 10.1 µH; if the ferrite is taken to saturate at 0.3 T, the saturation current B·gap/(μ0·N) = 5.97 A, and the field energy at this current is 179 µJ. The energy of an inductor is stored precisely in the gap. The 0.3 T value here is not from a datasheet: the real saturation current is taken from the inductor's datasheet.

Eddy currents. An alternating field induces EMF not only in the winding but in any conductor inside: the core, a shield, the wire itself. In solid metal ring currents flow and heat it — that is why transformer iron is stacked from thin insulated laminations, and at high frequencies ferrite, a poor conductor, is used.

In the wire itself eddy currents push the current to the surface — the skin effect. The depth at which current falls by a factor of e, δ = √(2ρ/(ω·μ0)) for copper:

Frequency Skin depth
50 Hz 9346 µm
1 MHz 66.1 µm
100 MHz 6.61 µm

A 35 µm copper trace stops working with its full thickness from about 3.57 MHz (where δ equals the thickness). For clock frequencies and edge harmonics the trace resistance is higher than the DC value of section 49.8; an inductor at hundreds of kilohertz uses fine stranded wire.


49б.7 Pitfalls

A relay without a diode works for weeks, then the transistor dies or the board resets. With no path for the current the coil lifts the switch node to hundreds of volts: 949 V in the model. A diode across the coil — 12.8 V.

With a diode the relay releases late. The current decays through the coil resistance: 0.476 ms to 10 %. A diode with a 24 V zener is 5.5 times faster, and the switch must withstand 36.8 V.

A clamp instead of a diode overheats with frequent switching. It burns more energy than was stored: 50.2 µJ versus 45.0 — at every turn-off.

An analog input catches edges of a neighbouring power circuit. The return loop is large: without ground at 10 mm — 23.5 mV, with ground 0.2 mm below — 0.0320 mV. Return right under the signal, short loops.

A slot in ground under a fast trace. The return current goes around the slot, the loop grows and with it the inductance and crosstalk (section 130.2).

An inductor "suddenly" heats up and whines under load. Peak current above the saturation current: inductance has dropped, ripple has grown (58.2).

A magnetometer is wrong near a power wire. 1 A at 1 cm is 20.0 µT, comparable to the Earth's field. Supply and return wires together (99г.6).


49б.8 Summary

Next: section 49в "Waves and transmission lines: signal speed, when a trace becomes a line, reflections, termination, radiation" — the third section of the series "Physics for the developer": why a trace has a speed and a characteristic impedance, when an STM32 pin edge turns it into a line, where ringing on a clock input comes from, why a 22–33 Ω resistor sits at the pin and how a current loop radiates the harmonics of edges.

Open in the interactive handbook