8

I am having problems with controlling a relay with load on it. I am using PIC16F628A. The problem is following: The relay has load. At first it is off. When the microcontroller's output is on, the relay turns on too. When the relay needs to be shut down, microcontroller's output is off. But when this happens, the microcontroller resets.

EDIT: The load is 220AC. Trying to control bell.

Please note that when there is no load, the relay command fully works, without resetting the microcontroller.

Here are the schematics and the PCB from the project.

Schematics

PCB

P.S. The crystal footprint was changed to capacitor, because I need to draw footprint for crystal, but it has same hole dimensions as the capacitor.

EDIT2: PCB Design was updated as suggested. The Updated Schematics & PCB: Schematics PCB

Ricardo
  • 6,134
  • 19
  • 52
  • 85
Triak
  • 916
  • 8
  • 14
  • Another piece of information that would be helpful: could you share the datasheet for the relay being used? – Jon L Mar 05 '14 at 00:54
  • @JonL I couldn't find the datasheet for the relay. Here are some characteristics 10A/24VDC, 10A/120VAC, 50/60Hz 10A/24VDC, 10A/240VAC. [LINK](http://pewa.panasonic.com/assets/pcsd/catalog/js-catalog.pdf) This is how it looks. – Triak Mar 05 '14 at 01:01

6 Answers6

9

I had intended to only comment, but I found I had too much to say:

Follow Spehro and Alexey's advice. There are multiple issues here. Perform all of the fixes, even if you find that just a snubber or decoupling capacitor seems to do the trick.

The load is 220AC. Trying to control bell.

Please note that when there is no load, the relay command fully works, without resetting the microcontroller.

This would seem to indicate that switching the bell is the source of a spike causing the reset. Do not omit the snubber &/or MOV that Spehro suggests since the relay is switching an inductive load. I have seen too many problems with switched inductive loads causing resets in industrial equipment to consider the need for a snubber and/or MOV to be merely a "band-aid". Besides the reset issue, without suppression of the spike, additional arcing will occur on the relay contacts shortening the life of the relay. You can buy a snubber or make your own.

The added decoupling capacitor (suggested by Alexey) needs to have the shortest path possible to the PIC power/gnd pins. The cut-up ground plane does not accomplish this.

The placement of the power/gnd pins on the PIC is a little difficult to accomplish a short path to the decoupling capacitor without either running traces between pads (could require longer-skinnier pads), or placing the decoupling capacitor on the back side.

You could "kludge" the decoupling capacitor to the existing board by cutting the leads short and either soldering it directly to the PIC power/gnd pins on top of the PIC, or soldering it to the pads on the solder-side of the board.

A few more unrelated comments regarding the board layout. Since P1 and P2 are on 220VAC, isolate them to one area of the board with no other traces (even the gnd plane) even close. See Creepage distance for PCBs handling line voltage AC? ... I would also not put the switches too near the AC since fingers will be in there. Also consider adding some mounting holes to the board.

Tut
  • 4,195
  • 1
  • 21
  • 38
  • I would say fix the problem, then add the snubber/MOV as insurance. The contacts will get raggier as they age, and the snubber will become less effective. – Spehro Pefhany Mar 05 '14 at 13:20
  • @SpehroPefhany I'm afraid I have to disagree. The snubber &/or MOV shouldn't be considered any more optional than the flyback diode for switching DC inductive loads. Besides the reset issue, without suppression of the spike, additional arcing will occur on the relay contacts shortening the life of the relay. – Tut Mar 05 '14 at 14:17
  • Okay. I've just seen many cases where a field guy tacked a MOV or R+C across a coil and left, only to have the problem return (at great cost). – Spehro Pefhany Mar 05 '14 at 14:21
  • As I said, there are multiple problems here and all the issues need to be fixed. Just because there are still problems after adding a snubber (or MOV), doesn't mean they are not needed. – Tut Mar 05 '14 at 14:30
  • Yes, agreed. Since the noise varies depending on where in the cycle the relay switches, a lot of testing can be required. – Spehro Pefhany Mar 05 '14 at 14:35
  • I can't add a snubber in parallel to the load. Does this mean that I will have to include 2 snubbers for each contact in the schematics? – Triak Mar 05 '14 at 19:16
  • @Triak On the [Red Lion SNUB0000 datasheet](http://www.redlion-controls.com/Products/Groups/NoiseSuppression/SNUB/Docs/12027.pdf), they indicate that although not the preferred method, you can place the snubber across the contacts (one snubber from P1 to P2). – Tut Mar 05 '14 at 19:21
  • @Tut I read that, but I don't have access to the bell. EDIT: I need to use the same R and C values from the datasheet, right? – Triak Mar 05 '14 at 19:27
  • @Triak As I said, if you can't place it across the load, place the snubber from P1 to P2. The values are usually not critical and really depend on the load. For more than you probably need to know :-) see: [How to design an RC snubber for a solenoid relay driving an inductive load?](http://electronics.stackexchange.com/questions/42131/how-to-design-an-rc-snubber-for-a-solenoid-relay-driving-an-inductive-load) – Tut Mar 05 '14 at 19:47
  • @Tut I will change C9 to 0.1uF/400V < or more – Triak Mar 06 '14 at 12:10
7

Your layout is not very good (in particular the Vcc path is long, thin and inductive), and that is about the worst relay you could choose (it is about the cheapest, however). A better relay with high isolation (preferably with non-counterfeit European approvals), ground and Vcc polygon pours (you've got Altium, it's a very good and expensive program, you should use those features), and operating the relay from the unregulated input would all help.

PICs are pretty insensitive to mistreatment, but they're not magic.

You could try suppressing noise at the (presumably at least somewhat inductive) load using a snubber or MOV, but that's a band-aid and the problem will probably come back in some way.

Spehro Pefhany
  • 376,485
  • 21
  • 320
  • 842
4

Add capacitor 0.1u near pin 14. Pull up PortA5 (pin4) to Vdd.

Alexey
  • 165
  • 4
  • 1
    I am having MCLR set to OFF. – Triak Mar 05 '14 at 08:20
  • 2
    You should take a look at your onboard DC power line (C1 plus pin) with an oscilloscope at a moment of relay turning off. I guess disconnection of the load from an AC line creates an interference in it, and that interference is transfered directly to your MC power supply, causing your MC to reset. – Alexey Mar 05 '14 at 08:57
  • 3
    And couple more things about layout of your PCB: - you must be sure that all of your GND polygons are connected with each other - right now R8 GND edge is floating in the air. - rotate P1-6 180' degree - this will allow you to place your VDD line as far away as possible from input DC cirquit. – Alexey Mar 05 '14 at 10:26
4

When I was a learner I had the same problem as you are facing now. And I solved it after many failures. The solution of your problem would be as follows:

  1. First thing you have to do is to put a 0.1uF ceramic capacitor as near as possible to your microcontroller power pin. This cap will remove all high frequency noise before it reaches the microcontroller.

  2. If you are taking any input from external environment like sensor input, switch interface via long wire or any other input than unwanted high frequency noise will be introduced on wire or terminal when relay spark occurs or any other switching occurs. You MUST remove it always otherwise reset problem will continue.

This problem can be solved by putting a 0.1uF ceramic capacitor on each input pin with respect to ground before it reach at microcontroller pins.

This will remove all noise from your circuit and it will function normally.

Ricardo
  • 6,134
  • 19
  • 52
  • 85
3

BUT .. You don't actually state a question but it's easy to infer one ! On the lines of ... 'how do I fix this ?'

Since you want to switch 220V AC, why not use a zero.cross-switching SSR instead of the relay ? No contacts involved, no arcing, almost no noise !

Decades ago I was presented with a similar problem. The rubbish pcb design had been inherited from a former 'consultancy - so called' and no budget was allowed for a relayout.

In brief, the usual suspects were investigated such as adding snubbers and better local decoupling.

The fix was actually in software, somewhat bizarrely. Instead of a single I/O write, the outputs were written multiple times.

This fixed it. You may care to try the same

2

In addition to everything that you have been told here, what I always do with a pic that controls relays, it is just after the program line that orders to turn off or turn on the relay, I put a pause (NOP) of 1 or 2 ms, and then continue running the program.

winny
  • 13,064
  • 6
  • 46
  • 63
FranGimeno
  • 41
  • 5