Arduino Mega 2560 pins flicker when initialize

Hi,
I am using Arduino Mega 2560 to connect 40 Relays, used wire library to communicate it with a nodemcu which receive commands and transmit to mega to turn on/off the relay.
Later these relays are connected with another panel having 40 relays.
the issue arise when I turn on the board it gives a few ms flicker to the relays and then comes to the actual state.
How do I prevent this flicker. any idea?

I assume you have a relay module with opto couplers.

Common cure is to write a HIGH to the pin when still INPUT (enable internal pull up) before setting the pin to OUTPUT.

digitalWrite(relayPin, HIGH); // first enable internal pull up
pinMode(relayPin, OUTPUT); // then set pin to output

or, maybe better

pinMode(relayPin, INPUT_PULLUP); // first enable internal pull up
pinMode(relayPin, OUTPUT); // then set pin to output

I assume you use a pin array and set the pinMode with a for loop.
Post your code, and a link to the relay module you're using, and a power/wiring diagram.
Because the advice given might be totally wrong for your hardware.
Leo..

Put one final relay in the +V feed to the relay board. Let the Arduino start up and THEN enable the "master" relay to turn on the other 40 when the outputs driving them are already set.
The "master" relay coil can also be in series with a safety (red mushroom cap) switch to implement an E-stop function.

This is the relay board image

I have separate supply by removing JD-VCC jumper and give +5V and gnd
for opto-couplers use a separate supply.

const uint8_t GPIOPIN[40] = {7, 6, 5, 4, 3, 2, 9, 8, 10, 11, 12, 26, 25, 24, 23, 22, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

 for (int i = 0; i < sizeof(GPIOPIN); i++)
  {
    pinMode(GPIOPIN[i], OUTPUT);
    digitalWrite(GPIOPIN[i], 1);
  }

yes I forgot to PULL_UP , will check it by the EOD

As said, swap two lines.
Leo..

const byte GPIOPIN[] = {7, 6, 5, 4, 3, 2, 9, 8, 10, 11, 12, 26, 25, 24, 23, 22, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

 for (int i = 0; i < sizeof(GPIOPIN); i++)
  {
    digitalWrite(GPIOPIN[i], HIGH);
    pinMode(GPIOPIN[i], OUTPUT);
  }

and add pinMode(relayPin, INPUT_PULLUP); as well?

As explained in post#2

Use one or the other.
If the pin is still an input (default), then this
digitalWrite(pin, HIGH);
does the same as
pinMode(pin, INPUT_PULLUP);
Leo..

Thanks, will try by the EOD

This relates to "low level trigger" relay modules.

Possibly because of saturation of the transistor plus some slowness of the optocoupler, the brief interval it is driven LOW if you set the controlling pin to OUTPUT before you write it HIGH appears to be sufficient to cause the relay to pull in momentarily.

tried, but still find the flicker, I tried with both ways, also tried to add a delay(20) between but still no success. [quote="Wawa, post:6, topic:1002279, full:true"]
As said, swap two lines.
Leo..

const byte GPIOPIN[] = {7, 6, 5, 4, 3, 2, 9, 8, 10, 11, 12, 26, 25, 24, 23, 22, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

 for (int i = 0; i < sizeof(GPIOPIN); i++)
  {
    digitalWrite(GPIOPIN[i], HIGH);
    delay(20);
    pinMode(GPIOPIN[i], OUTPUT);
  }

[/quote]

could you please explain a little simple, what should I do?

If the relays are active HIGH, then the default power on will cause all the relays to pull in until the outputs initialize. Either invert the logic of the relays (may not be possible if board doesn’t support it) OR control the power TO ALL relays and don't energize them until outputs are all preset.

Never given the above advice for 40 relays.

Could you try two for loops.
The first one to set all pins HIGH, and the second one to set all pins to OUTPUT.
Leo..

const byte GPIOPIN[] = {7, 6, 5, 4, 3, 2, 9, 8, 10, 11, 12, 26, 25, 24, 23, 22, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

void setup() {
  for (int i = 0; i < sizeof(GPIOPIN); i++) pinMode(GPIOPIN[i], INPUT_PULLUP);
  for (int i = 0; i < sizeof(GPIOPIN); i++) pinMode(GPIOPIN[i], OUTPUT);
}

Why not? It gives a clean start. 40x .04mA = 1.6A peak, easily switchable.

Sorry, was not talking about your advice. Just the pull up advice.
Also, 5volt sugar cube relays draw about 75mA each.

Yes, you could switch total relay power with relay#41,
or... switch opto power (40*2mA) with a PNP transistor.
But it's still wise to explore code options first.
Leo..

My guess is that its intrinsic with active HIGH relays. Between the time the power comes up and the control output is driven low the relay receives a pull in signal. Since this happens before the software starts, there is no way to "code around" it. The "master contactor" is how we do it with industrial systems that can cause damage if uncontrolled even briefly. Nothing happens until the software says NOW. A safety loop can also be in the feed to the relay coil for E-stop functionality.

Pull up before setting to output works fine on a single 8-channel board.
There must be some factor preventing it to work on five 8-channel boards.
Not sure about the delaying effect of sizeof().
Maybe OP should also try to replace "sizeof(GPIOPIN)" with "40".

As said, switching opto power seems less crude than adding yet another relay with maybe the same problem as the others.
Leo..

Active HIGH or LOW? If HIGH activates relay, INPUT_PULLUP will trigger it! I have relay modules with jumpers to select action state. Others that are active HIGH (I have the same looking quad board and it's active high and I get the same glitches. It's just a quick flash, but that's the root cause I troubleshot to.)

https://www.walmart.com/ip/4-Channel-Relay-H-L-Level-Driver-Module-Optocoupler-Relay-Module-Red-5V/331453872

The extra relay can't introduce the same issue as long as it is active LOW. Is why TTL drives low, a floating input doesn't have any drive, but feeding an active HIGH opto the float current is enough.
OP: How say you? Are your relays active HIGH or LOW?

Yes I tried it as well, but same result. I am using Chinese relay board. that are active low. I did not check by removing sizeof function. will try with a constant value of 40 instead of sizeof function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.