30 July 2010

Design Note: Using Outputs to Control Loads

In my plant-watering project, I need to control a relay switch to turn a water pump on and off.  I chose this nice, inexpensive relay to do the job, and confirmed that 3V does indeed close the switch in the relay.  If I connect the coil to the output of the MSP430 and set a high value on the pin, however, nothing happens.

The reason is that the MSP430 GPIO pins are limited in the amount of current they can source/sink.  According to the G2x11 datasheet, the total output for P1 should not exceed +/- 48 mA.  (This is also true for any of the Value Line devices.)  Omron's datasheet for the G5LE shows that the power consumption of its coil is 400 mW, which means at 3.6 V provided by the LaunchPad, it draws 111 mA, well above what the MSP430 can do!

Fortunately, there's an easy way around this that requires just a few inexpensive components.  A fellow ham, N1HFX, has an excellent write up on transistor switches that includes an example for a relay switch.  I've borrowed his schematic image and changed the values to what I've used for my relay.  Some external parts won't need the diode, but it's cheap insurance for any inductive circuits like a mechanical relay.  I'm using the same power source as for the chip (from the USB port via the LaunchPad for testing, but eventually a couple of AA batteries), but it's also possible to use a different supply, and even (with some careful design) use a different voltage.  Just be certain that your current draw from the MSP430 does not exceed its limits.

One thing to keep in mind with this circuit is that rather than the switch being on with logic high, it's on with logic low.  Since I want the switch off by default, I need to be sure that I've initialized the output pin to a high value before setting it as an output.

(Thanks N1HFX, de KE7BFA!)


NJC said...

Very useful circuit I think, there are other flavors of BJT (and MOSFET) circuits that do similar things.

One thing to watch out for is to not draw too much current from the LaunchPad either. Technically the computer limits the amount of current a USB device can draw.

David said...

Good point-- the USB port can source up to 500 mA of current, I believe.

SparkyGSX said...

Actually, any USB device should draw at most 100mA, unless the higher power draw is requested, in which case the device should draw at most 500mA. In practice, you can usually get away with just drawing 500mA.

The vast majority of computers cannot measure, limit, or cut-off the current through any USB port, other than tiny PCB traces acting like fuses, rendering the USB port useless in the process.

The datasheet says the G5LE should draw approximately 400mW *at it's rated voltage*; it is obviously wrong to divide this power rating by the applied voltage to get the current draw; this would imply that the current would go DOWN when the voltage goes UP, which is a rather clear violation of ohm's law. The current through a relay (of this size) is simply limited by it's resistance and the applied voltage, it's not actively controlled by a DC-DC converter, as is sometimes done on large rather contactors.

Using a PNP transistor isn't very practical; you lose about 0.3V from the available supply voltage of 3.6V because of the saturation voltage of the transistor, which means you have to use a (somewhat rare) 3V relay. It's usually more practical to use a NPN transistor to switch the low-side of the relay, because in that case, the power for the relay can be more than the 3.6V supply from the microcontroller.

As drawn, it is not possible to increase the supply voltage for the relay, because the microcontroller would be unable to turn off the transistor if it's emitter is at a higher voltage.