Relay module EMI?

Hello everyone. I have an Arduino Mega and 8-Relay module controlling large garden lights remotely through an Arduino UNO (and two nRF24L01 modules). Everything is working fine most of the time. However, I seemingly have two issues that might be related:

1_ sometimes when a command (lets say, to deactivate one of the relays) is received, the relay unlatches momentarily and latches again, as if nothing has been received. In other words, the digital pin that is responsible for controlling the relay is being toggled by software but almost instantaneously reverts back to the previous state. After sending the command few times it finally does what it's supposed to do. And sometimes it works correctly from the first time.

2_ I tried having all the relays activated overnight. The next day, some of them got deactivated on their own.

What on earth is going on :confused: ? The relay module is wired in full optical isolation, and the buttons used to send the commands are properly de-bounced.

After doing a lot of research, I couldn't find anyone having a similar issue. But I suspect that it has something to do with either noise (EMI) or the internal pull-up resistors being too weak. I appreciate any insights on how to troubleshoot/resolve this problem.

Here's the code on the mega that's controlling the relays:

#include <SPI.h>
#include "RF24.h"

RF24 myRadio (9, 10);
struct package
{
  int id = 0;
  float temperature = 0.0;
  char  text[100] = "empty";
};

byte addresses[][6] = {"0"};



typedef struct package Package;
Package data;

void setup()
{
  Serial.begin(115200);
  delay(1000);
  myRadio.begin();
  myRadio.setChannel(115);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ;
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}


void loop()
{
  if ( myRadio.available())
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("\nPackage:");
    Serial.print(data.id);
    Serial.print("\n");
    Serial.println(data.temperature);
    Serial.println(data.text);
    String txt = data.text;
    if (txt == "1")
    {
      digitalWrite(2, !digitalRead(2));
    }
    if (txt == "2")
    {
      digitalWrite(3, !digitalRead(3));
    }
    if (txt == "3")
    {
      digitalWrite(4, !digitalRead(4));
    }
    if (txt == "4")
    {
      digitalWrite(5, !digitalRead(5));
    }
    if (txt == "5")
    {
      digitalWrite(6, !digitalRead(6));
    }
    if (txt == "6")
    {
      digitalWrite(7, !digitalRead(7));
    }
    if (txt == "7")
    {
      digitalWrite(8, !digitalRead(8));
    }
  }
}

What do the serial prints tell you next morning?

Yes internal pull-ups too weak, try a 1K external one, and add a 0.1 uF capacitor from input to ground.

Yes, thats a good thing to try, it will kill all but the most stubborn noise. Internal pullups are usually OK for
signals on the same PCB, but cabling coming in from external to a board will usually need more definite pull-ups.

Are you running sensor/switch cables alongside any high-current or high voltage cables? Are your cables
shielded? twisted pair?

Thanks for the replies guys..

edgemoron:
What do the serial prints tell you next morning?

I tried to get serial prints as the issue happens (I had to leave my laptop outdoors overnight), but nothing happened. It doesn't seem to occur when the Arduino is powered through USB for some reason.

Grumpy_Mike:
Yes internal pull-ups too weak, try a 1K external one, and add a 0.1 uF capacitor from input to ground.

I will give it a try. What kind of capacitors should I use?

MarkT:
Yes, thats a good thing to try, it will kill all but the most stubborn noise. Internal pullups are usually OK for
signals on the same PCB, but cabling coming in from external to a board will usually need more definite pull-ups.

Are you running sensor/switch cables alongside any high-current or high voltage cables? Are your cables
shielded? twisted pair?

The high voltage cables are somewhat close to the Arduino. They are connected to the relays, which are contained in the same box as the Arduino. The cables are not shielded, they're ordinary 220v electric cables (I think 14 AWG).
Some pairs are twisted together. However, current doesn't flow in both twisted pairs at the same time, because I'm using multiway switching (to be able to switch the lights using the relays as well as the mechanical switches previously installed on the wall). So the twisted pairs are the two cables connecting NO to NO and NC to NC of both relay and mechanical switch.

What kind of capacitors should I use?

Ceramic

Grumpy_Mike:
Yes internal pull-ups too weak, try a 1K external one, and add a 0.1 uF capacitor from input to ground.

So one resistor and one capacitor for each pin?

masri_m2000:
So one resistor and one capacitor for each pin?

Yes.