# PIC18 MCU Relay Issue

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.

Setup:

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?

• 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. – Ken Shirriff Dec 20 '16 at 2:00

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?

• Yes, this corresponds to the contacts opening. The relay is driven by 5V DC and the switched load is 12V DC. – BrennanU Dec 22 '16 at 0:46
• 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? – BrennanU Dec 22 '16 at 0:49
• 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. – BrennanU Dec 22 '16 at 1:40
• 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. – electrogas Dec 23 '16 at 1:32
• I will confirm that the diode did the trick when installed in parallel to the load. – BrennanU Dec 23 '16 at 22:17