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.



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

  • \$\begingroup\$ Another piece of information that would be helpful: could you share the datasheet for the relay being used? \$\endgroup\$ – Jon L Mar 5 '14 at 0:54
  • \$\begingroup\$ @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 This is how it looks. \$\endgroup\$ – Triak Mar 5 '14 at 1:01

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Spehro Pefhany Mar 5 '14 at 13:20
  • \$\begingroup\$ @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. \$\endgroup\$ – Tut Mar 5 '14 at 14:17
  • \$\begingroup\$ 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). \$\endgroup\$ – Spehro Pefhany Mar 5 '14 at 14:21
  • \$\begingroup\$ 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. \$\endgroup\$ – Tut Mar 5 '14 at 14:30
  • \$\begingroup\$ Yes, agreed. Since the noise varies depending on where in the cycle the relay switches, a lot of testing can be required. \$\endgroup\$ – Spehro Pefhany Mar 5 '14 at 14:35

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.


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

  • 1
    \$\begingroup\$ I am having MCLR set to OFF. \$\endgroup\$ – Triak Mar 5 '14 at 8:20
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Alexey Mar 5 '14 at 8:57
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Alexey Mar 5 '14 at 10:26

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.

  • \$\begingroup\$ LMAO @ 'remove all high frequency noise' ! Hope springs eternal ! \$\endgroup\$ – Graham Stevenson Oct 9 '20 at 1:56

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


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.

  • \$\begingroup\$ Why so ? Is that based on a theoretical idea or simply from experience ? \$\endgroup\$ – Graham Stevenson Oct 9 '20 at 7:54
  • \$\begingroup\$ I say it from experience, this has solved small problems for me. \$\endgroup\$ – FranGimeno Oct 13 '20 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.