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 ? 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));
}
}
}