I am very much a software person and a rookie in electronics.

I am trying to understand why do we need Diode D1 in the diagram explained in this post:

http://www.susa.net/wordpress/2012/06/raspberry-pi-relay-using-gpio/ enter image description here

I understand the need to the transistor to protect the GPIO pin; however, do we actually need D1? Is it a nice to have or if I don't use it, I damage my PI?

Thank you,


Yes, we need that diode - the so called flyback diode. It prevents the premature death of the transistor due to the voltage spike across the inductive load when its supply voltage is suddenly reduced or removed - aka when turning off the relay.

  • \$\begingroup\$ Thank you @Ghanima. One more related question before I mark it as the answer. Can I replace the Diode with an LED, so I can see if the relay is activated or not? If not, can I just add an LED beside the diode to have the visual indicator? \$\endgroup\$ – Allan Xu Oct 22 '15 at 4:33
  • \$\begingroup\$ No, you cannot exchange this diode for an LED. For one thing, its polarity is reversed compared to the forward direction of an LED to operate, This is because it is supposed to snuff the induced current during turning of the coil - which according te Lenz's Law: "If an induced current flows, its direction is always such that it will oppose the change which produced it." What could be done is to add an LED with proper series resistor in parallel to the relay. If the current of the driving transistor is not exceeded it should work out. Low current LED should not add that much compared to relay. \$\endgroup\$ – Ghanima Oct 22 '15 at 5:46
  • \$\begingroup\$ so, in essence, I should be able to parallel other things (such as 2nd relay of LED) as long as I do not exceed the current of the transistor. Right? \$\endgroup\$ – Allan Xu Oct 22 '15 at 11:55

That diode is a flyback diode, sometimes called a snubber diode. You will see these wherever there is an inductor being switched on and off. The characteristic behavior of an inductor is that is opposes changes in current. When the transistor Q1 tries to switch off the inductor inside the relay REL1, the inductor tries to oppose this change in current. It does this by increasing the voltage on it's terminals. If you switch off the inductor really quickly, you will get a huge voltage spike from the inductor (hundreds, possibly even thousands of volts depending on the inductor). This voltage spike is called a transient, flyback, or inductive kick. The diode is there so that when the inductor is switched off, there is a path for the current to flow and dissipate through without producing a voltage spike.

So, yes, you definitely need the diode. Without it you would probably blow up Q1 when you switch off the relay, and possibly damage your Pi.


Not the answer you're looking for? Browse other questions tagged or ask your own question.