# 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