pic controlling a relay

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

pic controlling a relay

Post by siliconchip »

hi all,
i want to connect a 5v axicom v23105-a5475-a201 relay to a port on a pic output, can this be done directly from the port to relay using a diode as a suppressor across the relay coil, or is it best practice to use a transistor to switch the relay again using the diode as a suppressor across the coil once connected to the collector of the transistor, i suspect this is the way to go but would value any input/advice

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

Hi Bob,
Best practice is using a transistor to control relay via output pins.
Even better is to use the highest practical voltage relay. e.g 12 or 24V with puts less stress & heat generated on power supplies.
Getting back to the relay you asked about, the coil resistance is 36 Ohms.
So current drawn is 5/36 = 138.9mA
Looking at a typical microcontroller data sheet e.g 16F1937:

Code: Select all

Maximum output current sunk by any I/O pin.................................................................................................... 25 mA
Maximum output current sourced by any I/O pin .............................................................................................. 25 mA
So as you can see, if used the relay directly the current drawn from the pin will be exceeded!
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi martin
thanks for the reply, yes i thought that may be the case,earlier i made a hardware version using bc109 transistors with 1K base resistors and 1n4001 diodes across the relay coils,the relays switched as intended but drained the circuit in such that the lcd virtually went off when both relays switch,(im using 2) im running from a 9v battery through a dc/dc convertor to 5v, ive not got another battery to hand and im hoping the relays although rated at 5v dont require to much from a battery powered device

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

If you are running from batteries then you can can get much higher coil resistances then 36ohms!
5V, 167 ohms is a common value.
Two suggestions if both relays have to be energised, then make sure there is a time delay between first and second relays.
Use an electrolytic capacitor across the power supply to help with current surges.
Yes definitely use a reversed bias diode across each coil to prevent back EMF from transistor damage.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

Hi martin
Thanks for the reply i shall play around with it otherwise i will need another solution rather than relays as im measuring 3 lines of resistance in turn but each line cannot be pulled to 0v once measured hence the relay with open contacts i will keep you posted

Cheers bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi all
ive added a 470uf across the supply rail and used a mains 12v supply to my dc/dc convertor and although better something is still not right as it still trys to dim the lcd even from a mains 12v supply, also my signal to the transistor base is just setting the variable as 1 then back to 0 to get the output ports on and off, but ive noticed in the flowcode wiki it shows a circuit the same as mine but it says set state as a BOOLEAN value for on and off so am i setting states wrong and if so whats the difference, also my coils resistance is 45 ohms so using ohms law i need 111 ma to operate, this seems a little high for a battery, could i add a resistor in series with the coil to raise the resistance and bring the current down ??? thanks in advance

bob

just had a look at the bc109 data sheet looks like the max ic current is 0.1A this is a problem i think
Last edited by siliconchip on Sun Oct 28, 2018 7:01 pm, edited 1 time in total.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

Hi Bob,
First of all i would not personally use BC109, its continuous current is not that great.
I would consider a BC337 as they can handle 800mA
The trouble with series limiting resistor is as its limiting the amount of current will it be enough to energise the relay?
Like I stated earlier, if operating on a battery you require a relay that has a much higher coil resistance.
Can you provide a full circuit diagram of battery and mains supply?
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi martin
ive just this min looked at a bc109 data sheet before i read this post looks like 0.1A is its max collector current this is definitely a problem

bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi martin
my dc/dc convertor for my bread board is one of those jobbies from ebay that either give you 3.3 or 5v and push into the bread board, ive made a jack plug with a pp3 attached, i also used the power supply from my matrix development board so ive no diagrams to show :oops: :oops: but im wondering if the transistors are now pulling it down i shall also look for other 5v relays with a higher coil resistance

bob

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: pic controlling a relay

Post by Steve001 »

Hi Bob

Most of the breadboard PSU things from Ebay (I am presuming the ones that pick up the power supply rails at each end of the board with a DC jack in the centre or on one end)

Most have a max current of around 250mA, there are some however rated at 500mA & 750mA if you dig around further however these are quite difficult to find. could you measure the 5V voltage and see if it sits down when you put your relay in circuit.

At 5 Volts your 36 Ohm Relay = 139mA << edited used incorrect formula

I would power the relay form a separate voltage ie 12 V or 24 V and leave the 5 V just for the micro

to add to martins post, a favourite transistor of mine - BFY51 30V @ 1 Amp bit more chunky than the BC337. TO39 package

https://www.rapidonline.com/cdil-bfy51- ... 39-81-0122

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

Hi Steve,
Steve001 wrote:At 5 Volts your 36 Ohm Relay = 695mA
?
I make it 138.9mA
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: pic controlling a relay

Post by Steve001 »

hi martin

:oops: yes your correct, habit using V2 / R - this gives you the power

Sorry for any confusion

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

siliconchip wrote:but im wondering if the transistors are now pulling it down
Have you got the protection diodes the correct way around i.e. cathode to +5V and anode to collector of transistor?
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi martin and Steve
thanks for the comments, yes double checked the diodes and they are the correct way round, Steve as for the supply ive not measured the output when circuit running but the green power led does dim a lot when in use on the psu when the relays operate, power wise 12 or 24v is not an option to power the relays as this needs to be compact and portable and as such id like to run from a 5v supply, another avenue i think power wise is to ditch the pp3 9v source and go for several 1.5v cells i think from reading about this is will provide better than the small pp3, as for transistors ive ordered as martin suggested some bc337s also another couple of relays with coil resistances of 160 ohms which by my calcs current needed is 31.25 ma :P and shall try this, big thanks to you both for help and advice so far i shall update when parts arrive

cheers bob

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: pic controlling a relay

Post by Steve001 »

Hi Bob,

just a suggestion have you seen these batteries - small and compact 12 V have used it in a project it also has a mate the A27 size similar to a AA.
The A23 gave a reasonable service life in my project and I did have a 2 x 16 LCD Display powered.

Although the A23 Ah is lower than the PP3 @ 30mA hours compared to around 1200 mA hours for the PP3 (depending on manufacturer and chemistry ) Coin cells range from around 25mA hour to 1 Amp hour

image is A23 battery next to a standard AA battery

https://uk.rs-online.com/web/p/speciali ... s/7174029/


Steve
Attachments
20181029_203859.jpg
20181029_203859.jpg (75.15 KiB) Viewed 14743 times
Success always occurs in private and failure in full view.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi steve
thanks for that i shall look into it cheers

bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi all
ive now got hold of a couple of relays which have 167 ohm coil resistance but my problem still persists, ive also shortened the delay times in the program to try to make the switching quicker and there is a slight improvement but the relays are still killing my circuit, ive tried to attach my proteus schematic for simulation but this site wont let me so i have print screened it to show this i was hoping someone would be able spot something i have not, thanks in advance

bob
Attachments
Doc2.docx
(191.14 KiB) Downloaded 151 times

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: pic controlling a relay

Post by stefan.erni »

Hi Bob

There are some relais with a high resistor coil. Some have the diode includet.
One of my favorite is a Meder relais.

https://standexelectronics.com/viewer/p ... ay_SIL.pdf

Nice drawing your design. When I draw I always write the voltage to the VCC Pin. like 3v3 or 5V, so I have a better overview.
The two relays, if they are the same, I would draw both in rest position.
What are the input to the PIC Pin2 ? An Analoginput? If yes, just put a litle capacity there 560pF to 1nF.


regards
Stefan

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: pic controlling a relay

Post by Steve001 »

Hi Bob
siliconchip wrote:ive now got hold of a couple of relays which have 167 ohm coil resistance but my problem still persists, ive also shortened the delay times in the program to try to make the switching quicker and there is a slight improvement but the relays are still killing my circuit
what I would do next,

1. Disconnect the transistors base resistors from the micro (leave the one end connected to the transistor) connect a low current led on the output and run a test, see if the LED illuminate correctly.

2. Dab the disconnected end of the resistor to simulate the output of the micro the transistor should switch the relay (monitor what happens to the supply)

What are you using for your supply ?

Steve
Success always occurs in private and failure in full view.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi steve and stefan
thanks for the comments, steve i disconnected the base resistor from the micro end and added 2 leds to the outputs these lit nicely when ordered by the program but touching the resistor still connected to the transistors did nothing i didnt monitor the supply due to this, my supply is an ebay jobby that plugs into your bread board but i will be using a dc/dc buck convertor eventually, i plugged my mains power supply set to 12v into my jack plug of the supply and this let the circuit run correctly with no dimming but there was a faint whirr/squel coming from the plug when the relays operated, BUT the circuit did operate correctly all be it with clicking relays, is there another answer to using relays im only doing so because after the resistance is pulled low for measuring it then becomes open circuit before measuring the next line of resistance

cheers bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: pic controlling a relay

Post by medelec35 »

Have you checked the maximum current rating of the 12v supply and 5v regulator? When I would do is measure the 12v supply and 5v regulator before and after relays are on.
I would also use a current meter to measure maximum current as if it's way higher than expected, then reason will require investigating.
Also, you could measure the resistance of the relay coil to make sure it's correct.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi all,
sorry for lack of any updates life got in the way,anyhow im back working on my project, im now using a buck convertor set to 5v output and noticed that when using 2 pp3 batts with a total of 18v on the input the display is now not dimming when the relays operate but although ive not measured any current i know that when the relays switch they are consuming a fair bit of current all the time their on hence i think my original problem,that brings me to my next question,
im now thinking of using latching relays reason for this is that they only need to be pulsed for around 10 micro seconds i think ?? to set them then the same again once ready to reset them,this would mean a bit of rework on my flowchart but as i see it they will use a lot less power, so after going on a video site i watched some examples of these relays being used the guy was using an arduino to operate the relays which had 125 ohm coils so at 5v were pulling 40mA, he remarked that this was the max an arduino could source/sink so he said throw a resistor in series with the coil so it was around the 20mA range no mention of using a transistor ?? so my question is - i know the pic im using can source/sink 25mA if i were to place a resistor in series with the coil using ohms law to get this down to about 10mA for the pic to switch the relay would it be acceptable, would it work without harming the pic ?? i eagerly await your reply's

cheers in advance bob

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: pic controlling a relay

Post by Steve001 »

Hi Bob,

Latching Relays usually require more current to energise / reset due to the fact they have more mechanical mass to move and this has to "Mechanically Latch" itself into position.

If you increase the coil resistance, by placing a resistor in series you may find that the relay wont latch properly. This is biased on the two coil units I have not used a single coil latch relay - The single coil units you have to reverse the coil supply to reset

Steve
Success always occurs in private and failure in full view.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: pic controlling a relay

Post by siliconchip »

hi steve
thanks for the reply, im hoping that even if the latch relays do require more power to latch it will only be momentary while they set or reset as opposed to the non latch types im using which require constant power when being operated and holding this state until power is removed from the coil

cheers bob

Post Reply