I am having some strange issues with a PIC microcontroller (PIC18F45K22) driving a SainSmart 2 Channel Relay board (http://www.sainsmart.com/arduino-pro-mini.html).

After a bit of troubleshooting I have been able to isolate the issue. Whenever I have a load connected to the relay (a solenoid in an electric door strike) and I transition from a logic low output to a logic high output, the program counter seems to jump to a random location in the code.

I have found this by running my PICKit3 without any break points. Whenever this transition happens the code will randomly break (sometimes at Program Counter 0x0, other times at random locations.) Disconnecting the load from the relay (with no program changes) consistently eliminates the issue.

I have swapped microcontrollers and channels on the relay board (even driven by a different pin from the PIC) with no change.

The output pin from the microcontroller is connected directly to the IN1 pin of the board. JD-VCC jumper is in place, powered by the same power supply as the PIC.


TRISDbits.TRISD1 = 0;
ANSELDbits.ANSD1 = 1;
LATDbits.LATD1 = 1;

Macros used to control relay:

#define Relay_1ON() do { LATDbits.LATD1 = 0; } while(0)
#define Relay_1OFF() do { LATDbits.LATD1 = 1; } while(0)

I have an almost identical setup in another project with no issues, so I am at a bit of a loss. Any thoughts?

  • \$\begingroup\$ Sounds like some sort of power interference. I had trouble when I was controlling an inductive load with a relay that it would interfere with my microcontroller. Try bypass capacitors, powering the doorbell from a different circuit, moving the PIC further away from the doorbell, etc. Try using shielded or twisted pair wires between the PIC and the relay board. \$\endgroup\$ Dec 20, 2016 at 2:00

1 Answer 1


When you go to the logic high state, does this correspond to the contacts opening? If so, you might be drawing an arc because of the inductive load, which will generate broadband RF. You might be able to suppress this by putting a series RC snubber across the contacts. Is your solenoid driven by AC or DC?

  • \$\begingroup\$ Yes, this corresponds to the contacts opening. The relay is driven by 5V DC and the switched load is 12V DC. \$\endgroup\$
    – BrennanU
    Dec 22, 2016 at 0:46
  • \$\begingroup\$ I do have some diodes handy, so I added a freewheeling diode in parallel to the load, with no success. Should this do the same thing as the RC snubber? \$\endgroup\$
    – BrennanU
    Dec 22, 2016 at 0:49
  • \$\begingroup\$ Actually, after thinking this over some more, I placed the diode in parallel to the relay, not the load, which I am assuming will not work as well (or not at all). I will try moving the diode. \$\endgroup\$
    – BrennanU
    Dec 22, 2016 at 1:40
  • \$\begingroup\$ Yes, since fortunately you have a DC load, you can put a diode parallel to the load, in such a direction as to not conduct current when the load is energized. This should take up the inductive current when the contacts open, and keep and arc from forming. \$\endgroup\$
    – electrogas
    Dec 23, 2016 at 1:32
  • \$\begingroup\$ I will confirm that the diode did the trick when installed in parallel to the load. \$\endgroup\$
    – BrennanU
    Dec 23, 2016 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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