Especially our visitors from Savage///Circuits and Parallax!!!
Please take a look our new forums, we hope you register and become an active user.
The ZappBots forums are being run on phpBB 3.1.4 software which is different then the vBulletin used on the Savage///Circuits and Parallax forums.
Therefore, things look and work a little different, especially creating posts and uploading images / attachments.
The important thing is we have a place toTalk, Learn and Showcase our projects.
- Posts: 420
- Joined: Tue May 12, 2015 2:43 pm
- Location: Palmyra, NY
Relay Driver Circuits
Figure 1 shows a basic transistor circuit for driving a lamp, power LED or other device. The transistor is a current-driven amplifier. That is, a small current in the base-emitter junction controls a larger current in the collector-emitter junction. We use a transistor to drive a device if it runs at a higher voltage or draws more current than an I/O pin can handle. By using a transistor we take the load off the I/O pin by allowing the I/O pin to source a small current to drive a larger one. With many devices the above schematic contains all the basic elements of the driver circuit assuming the transistor can handle the voltage and current of the device it is switching. But there's something else to consider when controlling a relay which is that it is an inductive device.
When active, the relay coil creates a magnetic field within to pull down the contacts. When power to the relay is disconnected the field collapses generating back EMF which can damage the driver circuit. A flyback diode (also known as a snubber diode) in reverse bias in the circuit does not conduct when the relay is energized, however when the relay is de-energized there is a momentary reverse voltage generated that causes the diode to conduct, protecting the driver circuitry from a voltage spike which could damage it.
Note the schematic above (Figure 2) which is a generic transistor circuit for driving relays using a common NPN transistor. This schematic uses a common-emitter configuration and includes the flyback diode for driving inductive loads. A signal at CTRL biases the base-emitter junction through R1 drawing some current and allowing the transistor to conduct across the collector-emitter junction, turning on the relay. The big question is how do we know what value to use for R1? I often see this as a guess from people who don't know how the value is calculated. Several people admit to just using a 1K resistor since that always seems to work. Even more importantly, can the transistor handle the current of the device it is driving. Let's have a look at the following example schematic using the common 2N3904 NPN transistor.
In Figure 3 we have a 12V relay being switched by a 2N3904 NPN transistor. A 1N4001 diode is used as the snubber. In order to use this transistor to drive this relay we need to first be sure the 2N3904 can handle the current of the relay coil. Looking at the datasheet for the relay we have the coil voltage (12V), coil current (33.3 mA) and coil resistance (360Ω). To determine if the 2N3904 is suitable we need to know the collector current (IC) and the collector-emitter voltage (VCEO). Many people see the 200 mA rating and assume because it is listed as a continuous rating that this is the max value to go by. That isn't actually always the case. First of all this is the Absolute Max Rating, beyond which damage to the device can occur. The useful range as a switch is rated at 100 mA. The VCEO is 40V max (again, absolute max). Still the device would appear to be within the requirements to drive the listed relay with a coil current of 33.3 mA. In this case we're not near the limits of the 2N3904, but I did want to mention that the most important rating you don't want to exceed is power dissipation (PD). For the 2N3904 this is 625 mW max (TO-92).
You can calculate the power dissipation by multiplying the collector current times the collector-emitter voltage (IC * VCE) or so this is what many tutorials I have seen say. But the base current contributes to the overall power dissipation of the transistor, so we must also factor in the base current times the base-emitter voltage (IB * VBE). We haven't calculated that yet though, so for now in order to determine the suitability of the transistor we'll use just the first calculation. We already know our collector current is 33.3 mA and looking at the datasheet for the 2N3904 we can determine the VCE. What we actually want is VCE(sat) since we want the transistor to be in saturation and not in its active (linear) region when being used as a switch. It's important to note that VCE changes when the transistor is in saturation (and so does VBE) compared to when the transistor is operating in its active (linear) region and since for purposes of this tutorial we'll always be running the transistor in saturation we'll want the values for VCE(sat) and VBE(sat) for our calculations.
Okay, so back to calculating PD. According to the 2N3904 datasheet at IC = 50 mA VCE(sat) is 0.3V so we'll use that and calculate based on our known IC of 33.3 mA giving us PD = 9.99 mW which is well within the 625 mW maximum rating. Okay so now we know that the 2N3904 is suitable to drive our relay. But we need to calculate for R1 based on the information we have and knowing we want to keep the transistor in saturation. Looking once again at the datasheet we can see that for VBE(sat) = 0.95V @ IC = 50 mA, IB = 5 mA. To calculate the base current we take the source voltage, which in our case is the I/O pin signal from our microcontroller. Since most of us are Propeller chip users we'll use 3.3V giving us (VIN - VBE(sat)) / IB = RB or (3.3 - 0.95) / 0.005 = 470 making R1 470Ω instead of 1K. As a note 1K would probably work, but we're trying to ensure saturation and so 470Ω gives us that. Figure 4 shows the revised schematic.
So now you should be able to calculate the base resistor required to drive a suitable NPN transistor into saturation to use as a switch to control a relay. In this common-emitter configuration the transistor is essentially switching the low side of the circuit by connecting it to ground. You'll notice I didn't mention hFE. The reason is that hFE is a measure of the gain of the transistor when used as an amplifier (operating in its linear region). Since we're using the transistor as a switch in these applications we don't want the transistor to operate in this manner, however hFE can be used to tell us the minimum base current required to put the transistor into saturation.
Under the hFE parameter in the Electrical characteristics you will most often see several gain values at different IC and VCE values. You can calculate the minimum base current for saturation by taking the lowest gain value and dividing you maximum collector current (IC) by it. For example if the transistor has a minimum gain of 100 and you need to drive 100 mA, you will need a minimum of 1 mA to drive the transistor in saturation. IB(min) = IC(max) / hFE(min). As I said, this is the minimum current required. In practice most engineers calculate anywhere from 10%-30% more base current to ensure saturation.
In some situations you may want to switch the high side instead. For this we'll use a PNP transistor such as the 2N3906. The PNP transistor is almost a mirror of the PNP circuit having its emitter connected directly to the supply voltage and the base voltage dropping in relation to that voltage in order to turn the transistor on.
In Figure 5, R2 is simply a pull-up resistor designed to keep the transistor turned off by keeping the base at the same voltage as the emitter. Typically the value of R2 will be 10 times R1. We turn the transistor on by bringing the base low via R1 to create a current across the base-emitter junction, ideally at saturation. This works as shown as long as V+ is not more than our control signal. If V+ is at a higher voltage than our control signal then the base will never be able to go high enough in voltage to shut off the transistor. Because of this you will often see the PNP transistor switched by an NPN transistor when the supply is higher than the control signal as shown below.
In Figure 6, Q2 switches the supply voltage for K1. R2 is a pull-up resistor that keeps Q2 turned off when Q1 is off (open). Q1 is turned on by CTRL via R1. This allows Q1's collector to draw current across Q2's base-emitter junction via R3. As mentioned before, R2 is simply a pull-up resistor which will be 10 times the value of R3. R3 would be chosen to draw a base-emitter current in Q2 that would drive it into saturation. R1 would be chosen to do the same thing for Q1, though since we're drawing such a low current it doesn't really matter if Q1 is in saturation. Still, when using the transistor as a switch it helps to stay in saturation.
Why is R2 used? R2 ensures stability and that Q2 is completely turned off. If the base of the transistor goes slightly negative then a small amount of collector current to flow. R2 can also prevent a transient at power up which may cause Q2 to temporarily conduct. This could be bad if Q2 is switching a critical device. The NPN transistor is an open collector device. That is, when the transistor is turned on and conducting across the collector-emitter junction the collector is essentially pulled down near 0V driving whatever is connected to the collector low, however when the collector-emitter junction is not conducting the collector can float and is not driven high or low. Here's a comparison...on a microcontroller when you set an I/O pin low it is driven to GND. When you set it high it is driven to the supply voltage. However and open collector output is only driven low, but not high. On your microcontroller that would be the equivalent of the I/O pin being set to low output or to an input. As an input the pin floats. This is why you often see pull-up/pull-down resistors on inputs, so they're in a known state when not being driven. Some systems use an open-collector bus for communication, for example I2C. In an I2C system the SCL / SDA lines are only driven low, not high. They return to a high state via the pull-up resistors (usually 4.7K) on the bus.
Figure 7 shows a low-side switch similar to Figure 2, however this time Q1 is an N-Channel MOSFET instead of a BJT. Also, R1 is not in series with the control signal, but is instead a pull-down resistor to ensure that Q1 is off when CTRL is floating (not high or low). Unlike their BJT counterpart a MOSFET is voltage controlled rather than current controlled. They also have an extremely high input resistance (nearly infinite) making it possible to interface with nearly any logic driver output. A voltage on the gate of the MOSFET changes the resistance across the DS junction. When VGS is 0V there is a very high resistance across the DS junction. Likewise when VGS approaches VDD the resistance across the DS junction drops to a very low resistance noted by RDS(ON). While not zero the resistance at fully on and fully off causes the MOSFET to act like a SPST switch.
Because a MOSFET doesn't require a control current like a BJT and has a very low RDS they would seem to be an ideal choice over a BJT. The problem many hobbyists and experimenters face is the VGS required to achieve saturation (fully on) and the lowest RDS is often higher than the output voltage of their microcontroller I/O pin. And while many MOSFETs have logic level gate voltages that doesn't necessarily mean the MOSFET will be in saturation (fully on) at that voltage. The minimum gate voltage required to turn the MOSFET fully on can be determined from the I-V transfer curves in the datasheet. In the saturation region the MOSFET will be biased so that the maximum amount of gate voltage is applied which results in the lowest channel resistance (RDS) and the maximum current flows.
Driving a relay usually won't require much current so choosing a MOSFET with a logic level gate voltage will usually be sufficient. However when driving high current devices you need to be more mindful of the I-V characteristics. Also note that VGS(th) is the voltage at which the MOSFET starts to conduct. This is not where you want to be when using the MOSFET as a switch. Also note that this is an enhancement mode MOSFET, not depletion mode.
For high-side switching you will use a P-Channel MOSFET as shown in Figure 8. As in the BJT circuit shown in Figure 6 you have R2 ensuring that Q2 is off when Q1 is not biased. As before we use Q1 to control Q2 when V+ is higher than CTRL. The calculations for Q1 are as described earlier. Q2 must have a maximum VGS rating higher than V+, otherwise when Q1 conducts you will be taking Q2's gate outside of its operating range.
So what if you need to control several relays or other power devices? Figure 9 shows a UL2803 which is an octal darlington array. Darlington transistor mean lower control current required to drive the open-collector outputs low. The outputs can be used to drive inductive devices as long as you connect pin 10 (COM) to the supply voltage. This connects internal clamping diodes so that the outputs are protected against inductive devices. The outputs of the ULN2803 are rated at 500 mA output each, however you could not drive all outputs at that current level at the same time. Always remember your power dissipation (PD). Note that as with any transistor driver there will be a voltage drop (typically 0.9 V @ IC = 100 mA, IB = 250 mA) through the transistor. This is listed in the datasheet as VCE(SAT). You should keep this in mind if your V+ is chosen to be sourced at the rated voltage of the device you are driving. This voltage drop varies depending on current draw. A high on any input connects the corresponding output driver to ground. The outputs are otherwise open-collector.
So far we've looked at transistors for both low and high-side switching and the ULN2803 as an array of 8 low-side drivers in one package. Figure 10 shows the L293D which is a quad half h-bridge IC. Essentially there are four (4) push/pull drivers on one chip. These can be paired up to create an h-bridge, providing two (2) h-bridges on one chip at up to 600 mA per channel. Again, power dissipation supersedes the per output ratings. As a push-pull driver the outputs of the L293D follow the inputs. The only time the output doesn't match the input is when the enabled (EN) pin is low. The schematic shows 5 V for the logic supply which can be from 4.5-36 VDC, however the logic inputs (including enable) are compatible with 3.3 V logic. This means on a Propeller you would need to supply 5 VDC to the device, however no level translation is required on the inputs. The L293D contains internal clamping diodes allowing you to connect inductive devices directly.
Hopefully this gives you some options for driving transistors to control relays. All the transistor circuits could be used to drive non-inductive devices as well and not require the diode. And of course these aren't the only options, nor is this list detailed since each design will have specific requirements. However if you feel I have left anything out that is important to what I am covering, or if you find any errors or inaccuracies, please reply below and I will revise the post, if necessary, to address your concerns or questions.
I'm only responsible for what I say, not what you understand.
Users browsing this forum: No registered users and 1 guest