You have probably seen it many times. Why is it done?
Many articles will say something like "to protect the transistor from damage due to the back-e.m.f. generated in the relay coil's inductance when the transistor turns off." What's a "back-e.m.f."?
One article, where the relay was being switched by the output of a 555 timer, said "because the negative-going back-e.m.f. pulse could cause damage to the transistors inside the 555." Negative-going?
Let's see what is really happening.
When the transistor in the diagram is switched on, the supply voltage is applied across an inductance and the current through it increases linearly. As it does so, a magnetic field is created around the coil.
When the transistor is switched off, the coil is disconnected from the supply. The current through it is abruptly interrupted and is no longer able to maintain the field. The field which has been established therefore collapses very rapidly. In doing so it causes a voltage to be impressed across the coil and because of the speed at which the field collapses this voltage can be very high.
Since the direction of the field is changing in the opposite direction to that which caused it, the voltage induced across the coil is opposite in polarity too. Hence "back-e.m.f.", but it's certainly not negative-going ***. The end of the coil connected to the collector of the transistor will be positive with respect to the coil's other end.
Unless something were done about it, the collector voltage could rise so far above the supply value that damage might be caused.
The incorporation of the diode clamps the voltage at the collector to Vs + Vd, or about 0.7V above the supply.
*** I had a correspondent on a forum site who insisted that the back-e.m.f. was definitely negative-going.
I told him in so many words that was a load of bollocks and that shut him up. A pity really. I wanted to send the following to him.
Get yourself a coil. More or less any readily available one. A relay, a door bell or chime. Also get yourself a simple on/off switch. A push-button might be best, but that's not important. And get yourself a voltage source. a 1.5V cell will be fine.
Connect the three items and an oscilloscope as shown below.
Close the switch. Then observe the oscilloscope display as you open the switch. Repeat as necessary and adjust the oscilloscope's controls so that you can see what is happening.
Now connect the oscilloscope as shown below.
Close the switch. Then observe the oscilloscope display as you open the switch.
I have today, 23rd August 2014, been admonished by a visitor to this site. It was pointed out that if the 'scope's probe had its shield connected to the lower end of the coil in the diagram above, and the probe's signal contact placed on the positive rail, than a negative going pulse would be observed.
While that is true, I don't think it helps. It is usual practice to make this kind of measurement with reference to the circuit's common / 0V / "ground" point. And I made the not unreasonable assumption that most people would consider the negative pole/rail of a single supply as that point. (Which seems to be ingrained in many peoples' minds.)
If you want the satisfaction of seeing a negative going pulse, call the positive rail in these diagrams the reference, and do the measurement relative to that.