Software Guy Has a Hardware Question

Hi All,

I want to use a GPIO port on an embedded microcontroller (an LPC-2103) to switch an external voltage (7-20 VDC) on and off. Basically I want to build a solid-state switch or relay or something along those lines (not sure what you’d call it). For example, writing a ‘1’ bit to the GPIO port would close the switch and allow current to flow through the external circuit; writing a ‘0’ bit would open the switch and current would stop flowing.

I know, I know, pretty basic stuff, but I’m a software guy and I don’t know the first thing about how to design or build something like this. Would someone point me in the right direction?

Thanks for your patience with my (hardware) newbie questions. :slight_smile:

How much current?

Leon

You can use a Darlington transistor, or if you need more than one, a driver chip with several in them (and usually resistors to limit switch current and choke down diodes to quench the voltage spike that occurs when powering on a coil like a relay spool or solenoid).

If the latter and you don’t need more than 300-400 mA then look into something like a ULN2803.

Most important question is the current requirement as Leon said.

Mikael

The current is very low, probably < 100mA. I’ll double-check and make sure.

Also, what about a FET? Would that work too? Is it overkill?

Thanks!

P.S. If it makes a difference, I’d like something that is “normally open” - i.e. when power is off to the micro the “switch” is open, and only closes when the micro actively drives it.

A small MOSFET like a 2N7000 could be used.

Leon

So would wiring the 2N7000 MOSFET up like this give me what I want?

http://i33.tinypic.com/2iqk83o.jpg

In this circuit, when I write a ‘1’ to P0.10, +12V would then be present on the +12V OUT lead which could then power an external device. Do I have that right?

(As an aside, I’m a software turned hardware-software-everything person myself. It doesn’t hurt to pick up some books on the topic. One of the classics in the field is “The Art of Electronics”, and despite being a college textbook in design is rather approachable. It serves as a good reference if nothing else.)

The 2n7000 is an N-channel FET. That is, it conducts for a Vgs potential (that is, the voltage at the Gate is higher by Vgs than the voltage at the Source). Generally, the source is at or near ground potential (Source as electron source, not conventional current source). If you redraw your diagram, mirror the D and S pins, attach ground at S, and place the load in between the 12V and the D pin, you’ll be much more productive :). If you must switch the high side (that is, the voltage before it reaches the load), you need to use a P-channel FET.

You can check the datasheet for the specifics for Vgs. Be aware that a FET doesn’t saturate immediately at the Vgs threshold - it is not “fully on”.

The simplest solution is to use a prepackaged Darlington array. Commonly available in DIP format. Very useful if you have multiple things to switch.

Thanks for the book recommendation - I am ordering a copy from Amazon today. :slight_smile:

In the meantime, the local electronics store was out of stock on the ULN2803s, so I wired up a 2N7000 FET as described above. For the most part it’s working - writing a 1 bit to the GPIO port turns on power to the load and writing a 0 turns it off. There is a slight glitch, however, at power-up: when power is supplied to the microcontroller board, the GPIO line is “blipped” momentarily, supplying power to the load for about half a second. If I disconnect the wire from the GPIO port to the gate of the FET, the blip does not occur. Also, the glitch does not occur when I press the micro’s reset button - it only occurs at power-up.

My guess is that since the GPIO port defaults to an input at power-up, this somehow allows enough current to flow to the base of the FET to turn it on until the initialization code can change the direction of the port to an output. Does that seem logical? If that’s what’s happening, how can I solve this problem? Would pulling the GPIO line down to ground with a resistor help? Would I have the same problem with the Darlington array?

Thanks again for all your help - I’m learning a lot! :smiley:

Update: It seems my hunch was correct: I added a 10K resistor between the GPIO line and ground. Voila! No more blip!! The question now is, is 10K the optimal value for the pull-down resistor? If not, how do I calculate what the correct value should be?

Your processor pins should end up tri-stated until you switch them to outputs. FETs are interesting devices as the gate resistance is incredibly high, and any charge there (such as touching it with you finger) can switch them (or blow out the gate, with enough ESD voltage :)).

You can bias the gate toward ground. Attach a resistor (start with 10k, a common value for pulldowns) between the gate and the ground. Unless your microcontroller is driving the line high (check your startup code), this should keep the gate low.

theatrus:
Your processor pins should end up tri-stated until you switch them to outputs. FETs are interesting devices as the gate resistance is incredibly high, and any charge there (such as touching it with you finger) can switch them (or blow out the gate, with enough ESD voltage :)).

You can bias the gate toward ground. Attach a resistor (start with 10k, a common value for pulldowns) between the gate and the ground. Unless your microcontroller is driving the line high (check your startup code), this should keep the gate low.

Heh heh - I just now saw your reply. I grabbed a 10K because that was the first resistor I found in my parts box. How lucky is that? :lol:

Usually, for fail safe conditions and when translating voltages, I would use an opto-isolator. They are in all configurations. 4n25 type of thing, search digi-key. Basically you design it so that it has to have the opto-led lit to connect the output (add another output transistor for your load to +12).

You want to avoid situations where a glitch on power supply or logic supply could glitch your load on, the opto isolator will solve both this and the logic to +12v level translation.