CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Driving SPDT relay using PIC18F2580
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gaara



Joined: 10 Feb 2014
Posts: 11

View user's profile Send private message

Driving SPDT relay using PIC18F2580
PostPosted: Thu Feb 13, 2014 7:59 am     Reply with quote

Hi all,

I’m working on driving a relay i.e. the Omron G5Q-1A with a PIC18F2580 using a Darlington transistor array specifically the ULN2803A. I’m utilising the ULN2803A as I need to drive more than one relay but I’m starting with the driving of a single relay. For testing I’m connecting an LED which I intend on toggling using the PIC as my relay load. My basic objective is to use this relay to toggle an LED connected to the Normally Open pin of the relay.

I’ll describe the hardware side of things before moving into the code.

Firstly the details of my relay are as follows:
Model: Omron G5Q-1A
Coil resistance: 63ohms.
Contact form: SPDT- 1C

Here is my schematic: https://1d563b1a-a-62cb3a1a-s-sites.googlegroups.com/site/bgedsadownloads/Relay%20driving.png?attachauth=ANoY7coZglmInjceNzdOGPcfj-1dzKHq0ZqorQcDte_pGTZPnO7NOWRugChZb0tyxTswt_JUV6-LYmcoxSeQcmNvgzWSpwjo_avqv04e6gs9jPkfpSfuvPnbskvrSX3bTU1QjgqcXmXi-2VHY1rpy_YtHNzmsSixAeXiLMgIT2YOh0t2chNGYhm22aOyzWsh0gcTxt0iAeIN2tmo3t6u42ekn8e9yYLs0Q%3D%3D&attredirects=0

With regards to the relay I can confirm I’ve connected the coil pins i.e. pins 1 and 5 of the relay as per page 3 of the relay datasheet (http://www.omron.com/ecb/products/pdf/en-g5q.pdf
) correctly. I tested these two pins for resistance as they are the only pins which have resistance as of when the relay is not connected. I obtained a resistance of 60ohms which was very closed to the 63ohms for SPDT (Ic) relay as per specified in the relay datasheet.

Also using the diagram on page 3 of the relay datasheet I can confirm that the pole pin is pin 4. As seen in my schematic I then connected the LED on my NC pin which should be engaged when the relay switches using its pole.

Before posting this up I read through the following sources regarding the setup i.e.:
theory behind the darlington transistor array: http://www.electronics-tutorials.ws/transistor/tran_2.html
connection between a PIC and a darlington transistor array: http://electronics.stackexchange.com/questions/36629/bjt-resistor-and-diode-work-but-uln2803-does-not
SPDT based relays: http://www.zen22142.zen.co.uk/ronj/cpr.html

My relay’s datasheet: http://www.omron.com/ecb/products/pdf/en-g5q.pdf
ULN2803A datasheet: http://www.ti.com/lit/ds/symlink/uln2803a.pdf

Regarding the software aspects below is my code that I intend on using to achieve the basic objective I mentioned earlier i.e. is to use this relay to toggle an LED connected to the Normally Open pin of the relay. If you notice from my schematic I’ve got an opto-isolated LED connected to the PIC. This LED is being used to indicate that the respective relay pin has been pulled high or low. The code below toggles the LED connected to PIN C1 of the PIC.

Code:

#include<18F2580.h>

#fuses XT,PUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

#define REL1 PIN_C1

/* Test any led relay by passing the respective relay number */
void test_any_relay_LED(int rel_numb);

/* Test any led relay by passing the respective relay number */
void test_any_relay_LED(int rel_numb) {

   if(rel_numb == 1) {
      output_high(REL1);
      delay_ms(3000);
      
      output_low(REL1);
      delay_ms(3000);
   }
}

void main() {
   do {
      /* Step 6 - drive the relays based on the received character from the RFC-95 after a button press */
      test_any_relay_LED(1);
   } while (TRUE);
}


In mentioning the above my problem which might sound trivial is when applying the above code my relay doesn’t switch at all hence its load i.e. the LED connected to it doesn’t toggle.

Any ideas as to where either in my hardware or software logic I could be going wrong? BTW I found this resource showing with a very similar problem to what I’m encountering: http://electronics.stackexchange.com/questions/36629/bjt-resistor-and-diode-work-but-uln2803-does-not. I applied the lessons learnt but no progress yet.

Thanks
Gaara
Ttelmah



Joined: 11 Mar 2010
Posts: 19446

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 8:34 am     Reply with quote

Three things one after the other:

1) If a pin won't do what you expect, look what else is on it. In your case, RC1, is the timer1 oscillator input. If you look in the data sheet at the I/O summary for PortC, you see that this 'overrides the TRIS control when enabled'. So start by disabling Timer1:

setup_timer_1(T1_DISABLED);

May solve the I/O problem.

2) Are you sure about your part number?!..... The ULN2803, is an octal Darlington driver as you describe. The 2308, is an I2C interface driver.... Your PNG, has 2308.

3) Now key thing to understand when switching anything inductive, is that when you switch 'off', there is significant magnetic energy in the coil. This has to go somewhere. The diodes built into the chip, dump this into the supply rail. There has to be sufficient load/capacitance on this rail to prevent it from rising significantly when this happens. You only show 0.1uF on this rail.....
Gaara



Joined: 10 Feb 2014
Posts: 11

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 10:36 am     Reply with quote

Hi Ttelmah,

Thanks for the reply.

Quote:
1) If a pin won't do what you expect, look what else is on it. In your case, RC1, is the timer1 oscillator input. If you look in the data sheet at the I/O summary for PortC, you see that this 'overrides the TRIS control when enabled'. So start by disabling Timer1:

setup_timer_1(T1_DISABLED);

May solve the I/O problem.

I've had a look at the pinout description for PORT C and read little bit more regarding T1OSI and I get your point. I've disabled T1 in the code.

Quote:

2) Are you sure about your part number?!..... The ULN2803, is an octal Darlington driver as you describe. The 2308, is an I2C interface driver.... Your PNG, has 2308.

Apologies this is a big oversight on my part. I rushed a little bit whilst creating the schematic for my last post. The chip I've got for testing on my breadboard is the ULN2803A.


Quote:
3) Now key thing to understand when switching anything inductive, is that when you switch 'off', there is significant magnetic energy in the coil. This has to go somewhere. The diodes built into the chip, dump this into the supply rail. There has to be sufficient load/capacitance on this rail to prevent it from rising significantly when this happens. You only show 0.1uF on this rail.....

Would a 10uF capacitor be suitable as opposed to the 0.1uF cap?

After disabling T1 I've attempted switching the relay but no success.

Looking at the schematic particularly the connection between the ULN2803A (and not the ULN2308) and the relay plus the connection between the relay and its LED load do you feel I might have missed out on something hence causing the relay not to switch?

In addition do you think the voltage on the LED which is the load on the relay should be higher than 5v?

Also not to sound silly based on your experience and looking at my relay datasheet do you think the pins on my relay match the animation picture at the very bottom of this link http://www.zen22142.zen.co.uk/ronj/cpr.html?

Thanks,
Gaara.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 10:51 am     Reply with quote

According to your schematic you have 2 LED's on RB2 and RB3. Use them to do a 1Hz test to ensure your PIC are running at the correct speed.

Regards
Gaara



Joined: 10 Feb 2014
Posts: 11

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 11:11 am     Reply with quote

Hi alan

I believe the PIC is running at the correct speed as the LED I call REL_1 in my code (i.e. the relay connected to the optocoupler which is connected to PIN_C1 ) is toggling at the correct speed.

Thanks
Gaara
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 11:52 am     Reply with quote

Hi,

Good lord, I never imagined that such a simple topic could be made so complicated......

You really need to work on your basic troubleshooting skills!

First, what is the coil voltage of your relay? Are you sure? OK, with your relay out of the circuit, use a power supply of the correct value to activate the coil. You should hear a faint 'click' when you do this. Does that work? If not, reverse the polarity of the applied voltage. Is it working now? If so, put a continuity tester across the NO terminals and activate the coil again with your power supply. Does your continuity tester beep?

Once you verify the proper operation of the relay, and verify you are connecting to the correct pins, now try to drive the relay with the ULN3803 driver chip. At this point, just control the input to the driver manually, ie. simulate the output of your PIC by connecting it to GND and +5V. Does this work?

The point of my post is that you have all this stuff right in front of you, so YOU are best able to figure out what is wrong by breaking the problem down and solving it step-by-step. You are either going to fix the problem, or find out what is the issue......

John

PS Would you ask your 'alter-ego', TokTok, if he managed to solve his communications problem with the Zigbee module?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Feb 13, 2014 12:17 pm     Reply with quote

Mike

A further thought.

You can check the relay operates (in circuit with the power on) by simply connecting from the correct LM2803 output to GND.

Incidentally the protection diode does not return energy to the supply rail.
When you turn the relay off the coil current simply circulates through the coil and the diode.
The energy is dissipated in the diode and the coil's resistance.
The time taken depends on the initial current, the coil's resistance & inductance and the diode's forward drop.
To return energy to the supply, via the diode, you would need to connect the other end of the coil to GND.

Like ezflyr says you should be able to do these simple tests on your own.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19446

View user's profile Send private message

PostPosted: Fri Feb 14, 2014 1:41 am     Reply with quote

Apologies for the phrasing. They return energy _through_ the connections between the drivers diode pin and the drivers output. This results in spikes between these dependant on how good the connection is. They also require that the supply can deliver the current change when they turn on/off, without drooping.

The poster doesn't show his supply.

0.1uF, is a 'good' capacitor to have nice and close to the PIC (don't get rid of this), but the main supply needs to be able to have a 'step change' of 200mA, without changing by more than perhaps 0.1v. Depends on the impedance and speed of response of the supply, but several hundred uF, would be typically needed.

Just how large the spikes can be, and the effect on circuitry, can be surprising.

I prefer to have the trap pin connected independently of the processor supply. Using a logic supply to switch relays can lead to a lot of problems...
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Feb 14, 2014 4:43 am     Reply with quote

I know this is NOT a circuit analysis forum but here's a take on relay switching.
With the switch closed the coil current ~80mA flows as shown. (Using Gaara's 63Ohm relay coil. )
From supply +ve, via lead inductance, coil inductance & resistance, switch, return lead inductance, back to supply -ve
Code:

  -----------
 |           |      80mA---->        80mA---------->
 |           |     Wiring L          Coil L    Coil R
 | Power   + |-----nnnnnnnn----------nnnnnn----WWWWWW--
 | Supply    |                |    |                    |
 |           |                |    |       | /|         |
 |           |                |     -------|/ |---------|
 |           |              -----          |\ |         |
 |           |              -----          | \|         |
 |           |                |                         | Switch closed
 |           |                |                         |
 |           |                |                         |
 |         - |-----nnnnnnnn-----------------------------
 |           |     <-----80mA         <-----------80mA
  -----------

Immediately after switch opens, circuit and current flows look like this.
Code:

  -----------
 |           |      80mA---->        80mA---------->
 |           |     Wiring L          Coil L    Coil R
 | Power   + |-----nnnnnnnn----------nnnnnn----WWWWWW--
 | Supply    |                |    |                    |
 |           |                |    |       | /|         |
 |           |                |     -------|/ |---------|
 |           |              -----          |\ |
 |           |              -----          | \|
 |           |                |       <-----------80mA
 |           |                | 
 |           |                | 
 |         - |-----nnnnnnnn---
 |           |     <-----80mA
  -----------

I work on ~1uH per metre for paired cables.

Let's assume 10cm cable from power supply, that's 100nH.
Also assume 100nF for power supply decoupling.

The Z0 for the cable and decoupling is thus ~1Ohm.
(I've made the numbers easy, so I can do the maths.)

The current being drawn from the power supply can't just stop.
It will charge up the cap via the lead inductance until the voltage across the cap stops the current.
Ignoring any other components in the circuit the voltage across the cap will bounce upwards by ~80mV.
(80mA * 1Ohm = 80mV)

So turning off just the one relay will briefly raise the supply rail by ~80mV.
Turning off 8 relays together will produce ~640mV, ie over half a volt or >10%.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19446

View user's profile Send private message

PostPosted: Fri Feb 14, 2014 7:22 am     Reply with quote

Which is why I prefer to separate logic and relay supplies.

Remember this can (to a limited extent), be as simple as having one 5v rail, feeding the relays, and then having a resistor/capacitor filter feeding the logic off this.

And (of course), concentrate on how the ground rails run. You don't want the relay current being drawn 'via' the logic ground....
Gaara



Joined: 10 Feb 2014
Posts: 11

View user's profile Send private message

PostPosted: Fri Feb 14, 2014 8:34 am     Reply with quote

Hi all,

@ezflyr:

thanks for the reply.

Quote:

First, what is the coil voltage of your relay? Are you sure? OK, with your relay out of the circuit, use a power supply of the correct value to activate the coil. You should hear a faint 'click' when you do this. Does that work? If not, reverse the polarity of the applied voltage. Is it working now? If so, put a continuity tester across the NO terminals and activate the coil again with your power supply. Does your continuity tester beep?


My rated coil voltage is 5v and yeah I'm sure of this. I've placed the relay on another breadboard, powered up the breadboard with 5v and connected the two coil pins of the relay (i.e. the pins labelled pin 1 and 5 as per page 3 of the relay datasheet). There is a clicking sound. I also conducted a continuity test between the NO terminal i.e. the pin labelled pin 3 as per page 3 of the relay datasheet and the NC terminal pin i.e. the pin labelled pin 2 as per page 3 of the relay datasheet and there is continuity.

Sorry to sound silly but I though in the SPDT relay there is only 1 NO terminal?

Kindly correct me but I believe by mentioning the above I've correctly established the correct pin arrangement of my relay i.e. as per the numbering in the second diagram on page 3 of the relay datasheet:

PIN 1 - COIL (L)
PIN 5 - COIL (R)
PIN 2 - COIL
PIN 3 - NO
PIN 4 - NC

Quote:

Once you verify the proper operation of the relay, and verify you are connecting to the correct pins, now try to drive the relay with the ULN3803 driver chip. At this point, just control the input to the driver manually, ie. simulate the output of your PIC by connecting it to GND and +5V. Does this work?


I connected PIN 1 of the ULN2803 to 5v and its PIN 18 to the coil pin which I had connected to ground. This caused the relay to switch (click) hence I believe this far it works.

As a mention I'm using a 6v power supply but regulating this to 5v using this http://www.picaxe.com/docs/reg007.pdf

Gaara
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Feb 14, 2014 3:22 pm     Reply with quote

This is getting silly, and very tedious.
You claim you're using Omron G5Q-1A relay.
You insist it's SPDT
You've provided link to Omron data sheet which says SPST.

Quote:

Kindly correct me but I believe by mentioning the above I've correctly established the correct pin arrangement of my relay i.e. as per the numbering in the second diagram on page 3 of the relay datasheet:

PIN 1 - COIL (L)
PIN 5 - COIL (R)
PIN 2 - COIL
PIN 3 - NO
PIN 4 - NC

From the Omron datasheet:-

G5Q-1 and G5Q-14 are SPDT version, PIN_2 is COMMON.

I give up.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19446

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 1:49 am     Reply with quote

Also, no mention of the rating of the '6v PSU', or even if this is regulated, or not....
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Feb 15, 2014 6:31 am     Reply with quote

And the problem with the 6v supply regulated to 5v with that regulator is if you look at the spec. from the sheet posted, the dropout may be up to 1.3 volts ... that means the input to the regulator MUST be at LEAST 5.0 + 1.3 = 6.3v. And that assumes NO noise or ripple etc on the input side. At 6.0v on the input, you have typically already dropped out of regulation and are asking for nothing but trouble.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
avro698



Joined: 11 Nov 2006
Posts: 9
Location: South Wales, UK.

View user's profile Send private message

PostPosted: Sun Feb 16, 2014 10:55 am     Reply with quote

Referring to your posted schematic, what's connected to the wiper (pin 2) of the relay? All I see is a label, X17-2. If this is floating, the LED will never toggle.
If this is the case, consider connecting it to GND and the LED should toggle (assuming the relay energizes / denergizes).

FF101
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group