PDA

View Full Version : Controlling switches with a PIC



TonyA
- 4th March 2008, 16:09
I was wondering if anyone could point me in the right direction about how to control switches with the pic18f452

I would like to replace a momentary push button switch whose contact is normally open (off), and "on" when grounded (pressed). It's a single pole, single throw switch.

So I would like to replace this switch and control it with the PIC.

Thanks for any advice.

T

falingtrea
- 4th March 2008, 16:47
Something like an SN75477 interface driver chip would work. Basically it is an open collector transistor with some glue logic. If you want some isolation use an opto-isolator like a PS2801-1. It is an LED driving a phototransitor. Just connect the phototransitor outputs up like a standard open collector driver.

JIC, an open collector drive output is just a series resistor connected to a positive voltage on one end and the other end connected to the collector pin of the transistor. The emitter is connected to ground and your output signal is connected to the collector as well, sometimes through an isolation resistor.

TonyA
- 4th March 2008, 17:03
Thank you.

Is it possible to just use a 2N2222 transistor?

I found this schematic, however my application is a little different. (It got me thinking though):

http://www.rentron.com/images/rely-drv.gif

If the relay was omitted from that circuit, and since I just need to switch an incoming wire to ground via the transistor. The "switching" would be controlled by the PIC.

Would I just connect B of the transistor to the pic pin, C to the incoming wire and E to ground?

Then toggle the PIC pin attached to B of transistor? Would this send my "incoming wire" to ground?

Thanks for the help.

T

krohtech
- 4th March 2008, 17:10
It really depends on how much current you are switching and if it is AC or DC. I would think the simplest way is to use a relay for the load and a Darlington Transistor if the relay draws too much current from the pic.

TonyA
- 4th March 2008, 17:15
The load is 5V dc. It's just a simple digital switch that I want to replace/control with the pic.

I just need to control the grounding of the "incoming" 5V dc wire.


If we removed the diode and relay, and disconnected "C" from the schematics "VCC" from this circuit: http://www.rentron.com/images/rely-drv.gif

1. Attached a 5V dc voltage wire to the "C" of the 2n2222 transistor.
2. Connected "E" of transistor to ground.

Would the 5V dc wire (connected to "C") be brought to ground when the pic pin is high?

Now, referring to the schematic as it is exactly in that diagram, is the transistor there just bringing the (-) of the relay to ground. In other words, when the PIC pin is brought high or low this action switches the relay (-) to either ground or "no connection"?

Thanks again for the help.
T

krohtech
- 4th March 2008, 19:56
Would the 5V dc wire (connected to "C") be brought to ground when the pic pin is high?




The 2n2222 is an NPN BJT transistor.

See: http://en.wikipedia.org/wiki/2N2222

Yes, when voltage is applied to the base, current will flow from C to E. When the base is low, there is no flow.

TonyA
- 4th March 2008, 21:13
Thanks for the help, I appreciate it.

Would the 2n2222 work for my application? (i.e. just pulling a 5V wire to ground?) Which transistor would you recommend?

Thanks again,

TOny

skimask
- 4th March 2008, 21:20
Thanks for the help, I appreciate it.

Would the 2n2222 work for my application? (i.e. just pulling a 5V wire to ground?) Which transistor would you recommend?

Thanks again,

TOny

Use the 2N2222. As long as you aren't pulling a zillion amps thru it, you'll be fine.
Just make sure you read and understand how to do so...

Darrel Taylor
- 4th March 2008, 22:01
(i.e. just pulling a 5V wire to ground?) Which transistor would you recommend?

None!

Just use the Tri-State output of the PIC.


PORTB.0 = 0 ; Pin goes to gnd when not Tri-Stated

TRISB.0 = 0 ; Output to Ground
;....
TRISB.0 = 1 ; Output Tri-Stated

Added:
If there's any possibility that the external 5V is higher than the PIC's 5V?
Use PORTA.4 instead. It doesn't have the VDD voltage limit.<br>

falingtrea
- 4th March 2008, 22:43
Thank you.

Is it possible to just use a 2N2222 transistor?

I found this schematic, however my application is a little different. (It got me thinking though):

http://www.rentron.com/images/rely-drv.gif

If the relay was omitted from that circuit, and since I just need to switch an incoming wire to ground via the transistor. The "switching" would be controlled by the PIC.

Would I just connect B of the transistor to the pic pin, C to the incoming wire and E to ground?

Then toggle the PIC pin attached to B of transistor? Would this send my "incoming wire" to ground?

Thanks for the help.

T


That circuit is a basic open collector one. The trick is that since the relay has a 100 or so ohms of resistance it acts as a current limit. So you would need to replace the relay with maybe a 220 ohm resistor so that you don't blow the transistor. But like Darrel mentions, You could just use the PIC pin directly. The main reason to use the transistor would be if the switch you are replacing goes to something that has a much higher voltage than the PIC could handle, or you want to protect the PIC with the cheap transistor.

TonyA
- 4th March 2008, 22:44
Darrel, and FalingTrea thanks. I was wondering about that. Thanks again.

Tony