Great question, because it really focuses on some important fundamentals of circuits!
When the transistor turns on, it completes the circuit for current to flow through the relay coil. As this happens, the relay coil acts as a load, dissipating energy. The polarity of voltage dropped across the coil (positive on top, negative on bottom) is the same as it would be for a resistor (also a "load" device because it dissipates energy from the circuit). The coil's magnetic field expands to full strength, storing some energy.
When the transistor turns off, it attempts to stop all current through the coil. However, the coil's stored energy (in the form of a magnetic field) doesn't just disappear (the Conservation of Energy prohibits this!), and so now the coil acts like a *source* of energy rather than a load. As the magnetic field collapses towards zero strength, the coil temporarily acts like a battery, pushing current in the same direction as before but now with the opposite polarity (positive on bottom, negative on top).
Note how the diode is oriented in such a way that it is off (reverse-biased) when the transistor is on, but on (forward-biased) when the coil's polarity switches and the coil acts as a source. By turning on, the diode provides a safe path for the coil's sourcing current, allowing the magnetic field's energy to be dissipated gently. If the diode were not there, the coil's stored energy would manifest as a very high voltage, as the coil would attempt to push current through the turned-off transistor. This is capable of destroying the transistor!!!
This is why such "commutating diodes" are necessary for protecting the transistor outputs of switching devices such as PLCs and microcontrollers.
An interesting side-note is that the presence of this diode does slow down (prolong) the relay's dropout time, because the low-voltage forward drop of the diode results in the magnetic field of the coil being dissipated at a slower rate than if the coil were to output a higher voltage. The magnetic field's rate-of-collapse is directly proportional to the voltage dropped by the inductance of the coil -- Faraday's Law of Electromagnetic Induction. In some high-speed applications this can be a problem, as the relay won't turn off quite as fast as you might like. In such applications, one can connect a resistor in series with the diode to drop additional voltage and thereby force the coil's magnetic field to collapse faster. The resistor needs to be sized such that the voltage output by the coil during this time will not exceed the transistor's maximum rating and cause damage.
__________________
They call me "lightning" when wielding a hammer, because I never strike twice in the same place
Coils are by definition of inductance, current oriented devices. A coil doesn't care much about voltages. Its natural behavior is to oppose on any current change, be it increasing or decreasing current. By switching the transistor off you obviously "try" externally to decrease coil current. The coil responds by trying to preserve the current. The only way to do that is by reversing coil voltage polarity. And that's where the diode comes in handy (or sometimes necessary), by giving that moving charge a path to "continue flowing", or else, voltage can become as high as neded, to form a current. S.M.
__________________
Life is complex. It has a real part and an imaginary part.
First, mathematical: Faraday's Law of Electromagnetic induction states that the voltage induced in a conductor is proportional to the rate of change of the magnetic flux lines cutting through that conductor. If the magnetic flux lines are increasing in strength (+d Phi/dt), then the polarity will be in one direction; if the magnetic flux lines are decreasing in strength (-d Phi/dt), then the polarity must reverse.
Second, conceptual: think of the coil as being either a load (taking energy from the circuit) or a source (putting energy into the circuit). When the transistor turns on and the magnetic field grows in strength, the coil is acting a load and dropping voltage like a resistor would. When the transistor turns off and the magnetic field collapses, the coil is acting as a source and dropping voltage like a battery would. As SimpleMind correctly stated, the coil "wants" to keep current going the same direction, and so it is voltage that reverses polarity.
__________________
They call me "lightning" when wielding a hammer, because I never strike twice in the same place
thank you very much ( Simple mind ) and ( TONY ) for your great explanations and help
in the truck loading area . we use a lot of F C V. each is controlled by two solenoids one is normally opened and the second is normally closed ( 220 v ) . and when i opened the junction box for doing P M i sow a lot of diodes in the TB .
Good Answers: