PDA

View Full Version : Help Rqd - Driving 5Volt Relay using two pins



Squibcakes
- 4th October 2005, 00:33
Hi

I have a 16F627A and a 5 volt relay DPST (OMRON G5V-1).

I can drive the the relay coil (167 ohms) ok using a single pin from the Pic.

However, the max current the I/O pin can source is 25ma, and the coil really needs 28ma. Could this burn out the PIC if left driving the relay too long?

I was hoping that I could parallel two I/O pins to drive the coil but when I do this, the relay does not throw at all.

Is there a problem when paralleling two Pic I/O pins together?

J

Melanie
- 4th October 2005, 03:23
Yes there is a problem.

Potentially, if they don't switch at the same time, then pin.A (which has gone High) is now feeding a high current into pin.B (which is still Low).

Ideally you need a series Diode on each PIC pin, but that would drop between 0.6 and 0.7v.

An alternative if you are SOURCING the Relay (ie pins go HIGH to trigger it), then OFF would be High-Impedance input, and ON would be Output High eg...

RelayPinA var PortB.0
RelayPinB var PortB.7

' Example Toggles Relay One Second ON/OFF
' Don't forget the Back-emf Diode across the coil
' otherwise you'll damage the PIC

Loop:
TRISB.0=1
TRISB.7=1
Pause 1000
HIGH RelayPinA
HIGH RelayPinB
Pause 1000
Goto Loop

If you're SINKING the relay rather than SOURCING it, then change the two HIGH statements to LOW.

Squibcakes
- 4th October 2005, 07:25
Thanks Mel. I Love your work. ;)

J

TK5EP
- 4th October 2005, 10:48
I think the best you can do is to switch your relay with a transistor and keep your PIC safe.
You would need only a cheap one + 1 resistor.


Patrick

Ron Marcus
- 4th October 2005, 12:08
Loop:
TRISB.0=1
TRISB.7=1
Pause 1000
HIGH RelayPinA
HIGH RelayPinB
Pause 1000
Goto Loop

The only down side to this is if you use the Port B weak pull up option. There will be a small current going through the relay constantly. Mel, would ORing the two pins at once protect from one shorting the other out for an instant, or are the diodes absolutely necessary?

Acetronics2
- 4th October 2005, 15:05
RelayPinA var PortB.0
RelayPinB var PortB.7

' Example Toggles Relay One Second ON/OFF
' Don't forget the Back-emf Diode across the coil
' otherwise you'll damage the PIC

Loop:
TRISB.0=1
TRISB.7=1
Pause 1000
HIGH RelayPinA
HIGH RelayPinB
Pause 1000
Goto Loop



Hi, Mel

May be I'm a bit asleep ...but how does that achieve toggling an output ???

woudn'it be better to write : PORTB = PORTB ^ %10000001 , to have simultaneous toggling on B.0 and B.7 ?

Alain, back to sleep

Melanie
- 4th October 2005, 15:13
1. Personally I'd use a Transistor.. even one of those tiny SMD Digital Transistors with integral Base Resistors... takes next to zero board space and only one component...

2. Second choice I'd put in series Diodes on each PIC pin leg. The Relay will still pull in.

3. Pick a Relay with a higher coil Resistance and drive from one PIC pin. If I really must drive a Relay, then (obviously your application will dictate what you use), I tend to use Reed-Relays (500R coil) directly on PICs... some even have integral back-emf Diodes built-in. Anything bigger than that - see option 1. I don't like driving Relays (or any inductive loads) off the same power-rail as the PIC's supply anyway.

4. Only if absolutely nescessary just parallel the PIC pins. I don't like this option... not at all. During Power-up, the PIC can come up with I/O's in a random state... Sods Law (like Ohms Law but different) states that your pins will come up in the most unfortunate combination to ruin your day. But who am I not to help along disaster...

Squibcakes
- 5th October 2005, 00:24
Hi Mel,

You are right in that I should have chosen a relay with a higher coil resistance... and for sure that is what I will do next time.

What is the default start up state of the TRIS registers? Wouldn't they have been designed to go High-Imedance for safety's sake and not pseudo random?

Surely the lab coats at Microchip thought this one through?!!

Melanie
- 5th October 2005, 01:54
The default Startup State of the Registers is in the back of the Datasheet. Yes, TRIS in theory comes up as Inputs, BUT trust nothing and nobody in this business and always design for worst-case. That way you won't crash and burn.