Tuesday, October 2, 2012

Revision to Relay Circuit

I have used the relay circuit from my earlier post many times and I know that it works.  However, I have seen that circuit drawn with a 1KΩ resistor and also with a 10KΩ resistor and started to wonder why.   Since this blog is intended to help new hobbyist learn, I thought it would be good to share what I found out.  (Warning:  There is math involved )

After some thought and further research I made the following changes to the GPIO relay control circuit.  The current limiting resistor is now 10K and I will explain why in a moment.  Also, a 100KΩ resistor has been added.  This is recommended to ensure that the transistor turns off if the input is left open.  In my design, the input is always either high or low and is never open, so I am leaving this out of my interface.
The current limiting resistor must be sized to allow enough current to saturate the base of the transistor to guarantee that it switches.  Current too far above this level could damage the transistor.  Every transistor has an inherent property known as the common emitter current gain, commonly referred to as HFE.  The proper size for the resistor may be calculated using the following formula.
R = Supply Voltage / ( max A / HFE * 1.3 )
The supply voltage (from the GPIO pin) is 3.3V.  The HFE for a 2N2222 transistor is assumed to be at least 100.  The current draw on the relays that I use ranges between 30 and 50mA.  Plugging in the numbers for 30mA gives a result of R = 8461.  If 50mA is used, then R = 5076.  However, the HFE is very likely higher.  If we assume that it is 150, then the resistor value at 30mA becomes 12692.  This shows that a 10KΩ resistor is probably more appropriate for this configuration.

The bottom line is that any resistor between 1K and 10K should work OK.  I just felt that this issue should be addressed before any arguments start.

15 comments:

  1. Hi Ted,

    I want to switch 24V (impuls relais), this is no problem for the 2N2222 transistor?

    Thanks

    Regards

    ReplyDelete
  2. According to the datasheet
    http://www.2n2222datasheet.com/pdf/central_semiconductor_2n2222_datasheet.pdf
    The maximum collector-emitter voltage is 30V. However, the maximum collector current is 600mA. So, unless the relay is a solid state device, I suspect that the 2n2222 won't work for a 12V relay.

    ReplyDelete
  3. Could you elaborate on how - in the context of your circuit - the diode protects the transistor?
    I understand a diode itself prevents reverse electrical flow. This is really 2 related questions below.

    I expect the diode to be "inline" on the wire which is connecting transistor's emittor to the relay coil switch input, so that electricity simply can not flow back to the transistor. Instead I see the diode connects the switching current (from the transistor) directly to 5V. Because of this, I'm assuming that when there is "backpressure" wave of electricity from the coil disconnect, that energy will push back onto both the relay AND the transistor.

    Besides the position of the diode question, a related one: How does the coil backlash spike even travel over the diode? I get that the diode is orientated to allow such traven, but at the end of the diode is 5v VCC. Does this mean that voltages OVER 5V will still travel over the diode (going to the biggest voltage difference/potential), but that some of that coil backlash (small amounts, which are under 5V) will reach the transistor, but the amount is so small it doesn't matter?

    ReplyDelete
  4. I will just quote something much better written that I would do off the top of my head: http://en.wikipedia.org/wiki/Snubber
    When the current flowing is DC, a simple rectifier diode is often employed as another form of snubber. The snubber diode is wired in parallel with an inductive load (such as a relay coil or electric motor). The diode is installed so that it does not conduct under normal conditions. When current to the inductive load is rapidly interrupted, a large voltage spike is produced in the reverse direction because the inductor attempts to keep current flowing in the circuit in the same direction. This spike is known as an "inductive kick". Placing the snubber diode in inverse parallel with the inductive load allows the current from the inductor to flow through the diode and minimize the voltage spike that might damage the switching element. The energy stored in the inductor is dissipated by the series resistance of the inductor and the (usually much smaller) resistance of the diode (over-voltage protection). One disadvantage of using a simple rectifier diode as a snubber is that the diode allows current to continue flowing, which may cause the relay to remain actuated for slightly longer; some circuit designs must account for this delay in the dropping-out of the relay. Since the rise rate of the current (di/dt) of most systems is very fast (usually fractions of microseconds) "fast", "ultrafast", or Schottky diodes are used. Regular power diodes, such as the commonly used "1N4007" series, are not suitable as snubbers as their reverse recovery time (turn on time) is several tens of microseconds; the diodes do not turn on fast enough to suppress the fast voltage spike.

    ReplyDelete
    Replies
    1. So which will be an appropriate Schottky for your example? Something like a 1N5817?

      Delete
    2. Check the data sheet to see. The reverse recovery time is the critical parameter. I used relays that had built in snubbers, so I didn't even think about it. Your (and other's) questions are actually forcing me to learn more of the fine details. Thanks.

      Delete
  5. Hi Ted,

    I'm trying to use my raspi to replace the manual switch of my old heater (honeywell)... its circuit has 2 relays (NAIS JS1-24V) one for the warm water and the other for the heating, activated via on old manual switch, acting as a selector with 3 possible states (OFF, warm water, warm water and heating). So my objective is to replace this manual switch with one small breadboard with 2 PN222 transistors (http://www.adafruit.com/products/756), and via 2 GPIO pins, control both transistor basis, to switch on or off the relays. I've tested the voltage flowing through the switch and on the relays and it's 12V.
    So my question is what I'm explaining would be possible or it could be done using any other components or another type of transistors.
    I'll really appreciate you can give me some hint with this circuit! I can send you some photos of the mainboard to see what I'm trying to do...

    Thanks in advance!

    Alex

    ReplyDelete
  6. I'm sorry I cannot follow your math:
    R = Supply Voltage / ( max A / HFE * 1.3 ) =
    3,3 / (0,030 / 100 * 1.3) =
    3,3 / (0,030 / 130) =
    3,3 / 0,00023076923 =
    R = 14304,2913 and not 8461 . Where am I wrong?

    ReplyDelete
    Replies
    1. Divide max A by HFE before multiplying by 1.3
      R = Vs / ((max A / HFE ) * 1.3)

      Sorry that wasn't clear.

      Delete
  7. I suspect the pulldown resistor isn't needed. A GPIO pin low is something like 0.4v and can sink current. That means it will hold a silicon transistor's base to a voltage too low for collector current to flow. The 10k resistor may not appreciably reduce the 0.4 volts anyway.

    ReplyDelete
    Replies
    1. Sorry, me again, I meant the 100K resistor in my last sentence

      Delete
    2. You are correct, but consider what would happen if the interface board still has power and the connection to the GPIO is removed. That is the only condition I can think of where the pulldown might be needed. Even then, the transistor is not likely to allow current flow since there is no current flowing to the base.

      Delete
  8. Are these resistors needed regardless of the load circuit (AC or DC)?

    ReplyDelete
    Replies
    1. The load is not especially relevant to the need for the resistors. The 100K resistor to ground just guarantees that the relay is turned off if the GPIO is disconnected while the circuit is still powered. The 10K resistor is there to protect the transistor from overcurrent. However, a typical GPIO cannot produce sufficient current to damage most transistors. But it is still recommended.
      (Wow. People still read my ten year old blog!)

      Delete
    2. you bet! thank you!

      Delete