Its really simple:
When the coil of a relay (or other inductive device) is energized, magnetic energy builds up in the magnetic core. This energy remains constant as long as the current doesn’t change.
When the current is interrupted by a switch (often a MosFet) this energy (aka “field”) must go somewhere.
The reducing field, due to the MosFet being turned off, results in a voltage generated in the coil windings as the field changes (toward zero).
This is the basics of all motors, solenoids and inductors.
The voltage created is:
- Where V is the voltage generated by the changing flux in the structure.
- L is the coil inductance in henries (a typical small coil might be 400 µH)
- di it the change in current
- dt is the change in time
If you have 500 ma going through a coil whose inductance is 400µH
And your MosFet switches off in 1/2 µS then the created voltage is:
V = 400µ * 500ma / 0.5µS
V= ~ 400 Volts. It is this voltage that will likely damage your MosFet.
By putting a diode across the coil, the coil current will flow through diode and be limited to 0.7V
If your circuit supply was 12V, the peak voltage would be 12 + 0.7 instead of 12 + 400.
In an earlier post a fellow forum user suggested the real issue is the generation of EMI (electro magnet interference). I disagree, EMI is dealt with in other ways and is very circuit and layout dependent. However it is a good idea to put a capacitor (0.1µF or so) from the ground at the MosFet to the supply of the coil. This will reduce any sudden changes in the supply voltage due to the change in current between the coil being off and the coil being energized.
Like many things there is more to the story of inductor dynamics. I chose to present the basics and reasoning for the diode across the relay coil. There are other ways to suppress the coil voltage and some effects of using a diode but to the typical Arduino user these are not of great concern.