# Driving a relay using a microcontroller

Discussion in 'General Electronics Chat' started by rasosina, Jun 30, 2010.

1. ### rasosina Thread Starter Member

Jun 30, 2010
25
0
Hey guys,

I have a quick question. I've done a little bit of research and seen the following circuit used for this type of application.

http://interactive.usc.edu/members/phoberman/relayCircuit.gif

my question is, why does it have to use a BJT? why not a MOSFET, or an IGBT? that way there is no power dissipation while the switch is on.

Thanks

2. ### KMoffett AAC Fanatic!

Dec 19, 2007
2,574
230
You can use a BJT, IGBT, or MOSFET. Small BJT's are just cheap and plentiful.

Ken

3. ### beenthere Retired Moderator

Apr 20, 2004
15,815
282
The note on the circuit about "high current" is the giveaway. Look at the specs on pin current capability from your microcontroller. It probably can't supply 40 ma to pull a 5 volt relay, or 60 ma @ 12 volts either. Using a BJT gives you that ability.

4. ### SgtWookie Expert

Jul 17, 2007
22,182
1,728
The schematic you linked to specified a base resistor from 100 to 1k Ohms.

Be warned, 100 Ohms is too low as a base resistor for a PIC that is operating from 5v. You will over-stress the I/O pin. For a PIC running from 5v, don't go below 220 Ohms for a base resistor.

Rbase=(Vsupply-Vbe) / (Ic/10)
where:
Vsupply = the voltage to be applied to the other end of the base transistor, usually Vcc or Vdd.
Vbe = the base to emitter saturation voltage of the transistor; at low collector currents 0.7v is used; at higher currents 0.8v or even higher. See the Vbe plots in the datasheet for the transistor being used. ONSemi's datasheets are pretty good, and usually have these charts.
Ic = the desired collector current.

Let's say you're using a PN/2N2222 transistor as a driver, and you have a 200mA load.
Rbase = (5v - 0.8v) / (200mA/10)
Rbase = 4.2/20mA
Rbase = 210 Ohms
210 Ohms is not a standard value of resistance. 220 Ohms IS a standard value, so use that.

An IRLD024 is a nifty 4-pin DIP logic-level N-ch MOSFET that can sink up to 2.5A; Mouser and Digikey both carry them, as well as other vendors. Very handy for breadboarding. Use a small resistor (50-200 Ohms) between the gate and the I/O pin to avoid gate "ringing".

Last edited: Jun 30, 2010
5. ### atferrari AAC Fanatic!

Jan 6, 2004
2,648
764
I understand that we want the transistor hard into conduction, but, isn't it the implied current gain (10), much too low? Ib= a tenth of Ic?

It seems that I have to revisit concepts.

6. ### SgtWookie Expert

Jul 17, 2007
22,182
1,728

They'll specify hFE minimums, but when you look at the saturation specifications, just about everyone uses Ib=Ic/10.

There are some very high-gain transistors like the ZTX869 which also plot saturation curves at Ib=Ic/100, but these are the exception to the rule. If you rely on the specified minimum hFE when you're using the transistor as a saturated switch, it may work OK for 80%-90% of transistors out of a batch - but that's not good for a production environment. Those few transistors with low gain will cause a lot of time and expense in rework; to be avoided. The low gain will cause a high Vce (saturation voltage) and the transistor will be much more likely to burn up.

On this website, we'd like to give people the best "scoop" we can so that their projects have the best chance of working the first time around, rather than just wondering why something burned up - or just plain didn't work.

7. ### eblc1388 Senior Member

Nov 28, 2008
1,542
102
It doesn't have to use a BJT but there are a couple of good reasons. Someone else can add to the list.

1. The BJT has been used for ages well before the MOSFET or IGBT has been invented. Compares to the dissipation against the relay, the heat dissipation is minimal.

2. A suitable BJT is cheaper and more reliable. Not many MOSFETs are suitable because of the 5V or less gate drive voltage requirement.

3. A BJT don't care if its input is being open circuited. Not so for MOSFET. Port I/O pins are often in high impedance state before proper initiation.

4. Works even with modern 3.3V MCU and those with even lower VDD

8. ### atferrari AAC Fanatic!

Jan 6, 2004
2,648
764
Sure I have!

Thanks Sarge.

9. ### rasosina Thread Starter Member

Jun 30, 2010
25
0
Hey I dont quite understand what u mean by point 3. If you open circuit the gate of a mosfet doesn't it turn off? since the gate voltage is more or less 0 and less than Vt it would turn off right? and also, what do you mean by 5V or less gate drive voltage requirement?

So I just wanted to get people's opinion, if battery life becomes an issue, would it be better if I use a mosfet/igbt since there's no power dissipation? or should i stick with the bjt?

also, does the bjt have a smaller on-state voltage drop across its collector and emitter terminals than a mosfet/igbt?

Also, i found this chip: TC4469 which is supposed to do the job of the circuit i originally posted above. and it's only a couple bucks. will this chip do the same thing as that circuit?

10. ### AlexR Well-Known Member

Jan 16, 2008
735
54
Wrong!
Since the gate is very high impedance, when the gate it is open circuit its voltage can drift to any value. It can even drift high enough to destroy the device which is why unused mosfet inputs must be tied either to Vss or Vdd.
Most mosfets require a gate/source voltage of 10volt to fully turn on. There are some mosfets specially designed to work at logic levels but they are not all that common so you have to be careful when you select your mosfet to make sure that it can work with logic circuits.

While its true that it takes no power to keep a mosfet in given state it does take power to switch a mosfet and depending on the mosfet and the frequency of the switching it can take quite a lot of power. Whether you are better of using bjt's or mosfets will depend on the application.

A saturated bjt has a Vce of about 0.2Volt
A fully turned on mosfet does not have a fixed drain/source voltage but rather an on resistance. Its value will vary with mosfet type but will typically be anywhere between 10's of ohms and milli-ohms but as a general rule the voltage drop across a mosfet will be lower than across a bjt.

True, you can also use something like a ULN2068 if you want a bjt array. The problem with both of them is that in most cases you only want to drive 1 or 2 devices so its a waste of both money and board real-estate to use the array chips.

11. ### SgtWookie Expert

Jul 17, 2007
22,182
1,728
As has already been explained, it is poor practice to allow the inputs to a CMOS/MOS device to "float", or to not have a current path to Vdd/Vcc or ground/Vss.

In order to ensure that a MOSFET will be turned off in case there is no other current path, it is good practice to connect a 10k resistor directly between the gate and source terminals. This will only introduce a very small "overhead" to the drive requirement (0.5mA for logic level, 1mA for standard level) and adds a simple, inexpensive "safety net" that can help to prevent inadvertent destruction of the MOSFET.

Although logic-level power MOSFETs are fairly new on the scene, manufacturers are introducing new models all the time. Authorized distributors should have a good deal of stock. Don't look to surplus vendors for having many of these types.

BJT's are cheap and plentiful, however you'll wind up having to source more power from your uC's I/O pin. If you are controlling a number of devices, your power budget and total package dissipation will get eaten up in a hurry. Be certain to check your circuits' power requirements against your uC's Electrical Specifications. Note the per-pin I/O current limits, and the total package current limits; you must stay within both unless you like replacing burned up uC's.

Depends on the MOSFETs and transistors being considered.

You might want to look at IRLD014 or IRLD024 N-ch logic level power MOSFETs; these four-legged DIP critters are very handy for prototyping; the '14 sinks up to 1.4A and the 024 sinks up to 2.4A.

The 2N7000 N-ch MOSFET is another popular (low-power) N-ch MOSFET; it's limited to 200mA sink current, and I suggest you don't go above 100mA sink.

You can sometimes also use MOSFET gate driver IC's instead of MOSFETs if your load is low, but you may use more current than you'd like. Check to see what the power consumption is when the input to the driver is high and low.

12. ### rasosina Thread Starter Member

Jun 30, 2010
25
0
Hey guys,

Thanks for clarifying my questions. Can you tell me if I can use the TC4469 chip to switch a 24V AC line?

I was told that i'd have to connect this chip to a relay and then have the relay switch the AC line, but can i just get rid of that relay and connect the AC line to this chip?

I know that might be a stupid question but please help me. I'm trying to design a system to replace the thermostat. so i need to switch on and off the 24ac lines to control hvac.

Last edited: Jul 2, 2010
13. ### SgtWookie Expert

Jul 17, 2007
22,182
1,728
The TC4469 is a quad CMOS buffer. It could be used as a driver for logic-level MOSFETs.

Did you look at the limits for inputs and outputs? Did you see that 18V was the maximum?

Did you know that 24VAC has a peak-peak voltage of around 33.9v?

You really need to use a relay to provide isolation. You might consider using a solid state relay, but a normal relay will be cheaper and easier to find, besides being more tolerant of abuse.

Last edited: Jul 2, 2010
14. ### rasosina Thread Starter Member

Jun 30, 2010
25
0
oh yea. you're right sarg. for some reason i thought the 18Volts was the maximum input voltage. Since the IO pin voltage from the uC is 5 volts i thought i was fine. Sorry about that.

15. ### Harrington New Member

Dec 19, 2009
86
3
There is another tip that I can give here based on my own experiences of driving relays with Pic processors and that is to use and opto coupler's on the output pin of the Micro

I now always use opto couplers for the simple reason of switching noise "Module tested by MOD staff for vehicles "

The reason being that should the transistor go short circuit base collector the port of the Micro could be damaged by the supply used on the relay supply.

Also when changing the ports state I found that the noise from the relay ,the back emf ,although using a diode across the relay to prevent back emf from damaging transistor upsets the port registers on the micro this can result in missed instructions to that port an incorrect functionality of the Micro and your circuit

These are my own personal findings they may not be for you however it is only a suggestion which may help you in the future