Unwanted relay activation!

Tested circuit and sketch (attached) as shown using 2 relays and one LED.
Runs great, but each time i restart the circuit (turn power on), the 2 relays activate for about 2 seconds, then the programmed sequence runs.
The relays have the quirk of being active when driven LO.
I have run the sketch with combinations of relay outputs being set HIGH and LOW and the NC/NO contacts being changed appropriately.
I have powered up the Mega and the relay board separately and in different order.
No matter what, the relay board does the initial 2 sec activation on it's own.
The LED, driven directly from the Mega, also flashes on.
So I'm assuming it's the sketch that is the issue.
(schematic doesnt show Mega pin use accurately...sketch does)
The problem is that, in full use, 18 servos and 9 LEDs will be used. I can't have all that power draw on startup. In actual use, the Mega will be powered by a fixed 8VDC supply on the Vin pin.

I need to know how to start and stop this sequence without the initial output event.
Advice appreciated...

void setup() {
  pinMode(2,OUTPUT); //heat
  pinMode(4,OUTPUT); //LED
  pinMode(6,OUTPUT); //air
}

// basic test with loop for 1 cannon
void loop() {
  digitalWrite(2, LOW);  //heat on, stays on until write high
  delay(3600); //preheat time 
  digitalWrite(6, LOW);  // air on, stays on until write high 
  delay(100); //
  analogWrite(4, 150); // flash sequence for LED starts
  delay(50);   //these delays don't seem to effect pins 2 heat or 6 air
  analogWrite(4, 0);
  delay(15);
  analogWrite(4, 150);
  delay(75);
  analogWrite(4, 0);
  delay(15);
  analogWrite(4, 150);
  delay(50);
  analogWrite(4, 0);
  delay(15);
  analogWrite(4, 150);
  delay(50);
  analogWrite(4, 0); //LED flash sequence ends
  delay(105);
  digitalWrite(2,HIGH); // heat goes off 
  delay(100); 
  digitalWrite(6,HIGH); // air off after heat goes off - clears barrel
  delay(20000); //20 sec to start again.  
}

1 Like

What is circuit you have driving the relays ?
The problem is caused by the state of the digital outputs when the Arduino boots up, and circuit mods may well fix it .

It’s come up before , not sure of the fix, but to save time for an advisor , put up your relay drive circuit .

Setting your outputs to the desired state in setup will help as the second item in loop is a long delay . When you set pinmode as output , the outputs go low as default .

When an arduino boots up, the pins power up as INPUTS which means they float. If your relays are active low, you will want to install pull-up resistors from between your control pins and +5V so they relay powers up HIGH which means it is off.

Also, If you wiring diagram is correct, you do not have any common ground between the arduino and the relay board. You should have all of your grounds connected together (Arduino to ground of 5V regulator and ground of 12V regulator)

1 Like

In the code just before declaring pin 2, 4 and 6 as output, set them to the inactive state by an I/O write.

Railroader:
In the code just before declaring pin 2, 4 and 6 as output, set them to the inactive state by an I/O write.

Could you explain?

If an I/O low activates the relay, set it to high before declaring it as output and vise versa. The code later contains delay(x) which might prolong wrong activities.

 digitalWrite(2, LOW);  //heat on, stays on until write high
  delay(3600); //preheat time 
  digitalWrite(6, LOW);  // air on, stays on until write high 
  delay(100); //

If an I/O low activates the relay, set it to high before declaring it as output

"If an I/O low activates the relay, enable the built-in pull-up, before declaring it as output"

Unless a high is activating the relay...

Railroader:
Unless a high is activating the relay...

Yeah, that was the bit I was asking about

@DanLRC
Comment out all code except the code running those two relays and make that work as You want. Use Serial.print for test printings like "Now I am here, Now I am there".

replace setup() with this.
Leo..

void setup() {
  pinMode(2, INPUT_PULLUP); //heat
  pinMode(4, INPUT_PULLUP); //LED
  pinMode(6, INPUT_PULLUP); //air
  pinMode(2, OUTPUT); //heat
  pinMode(4, OUTPUT); //LED
  pinMode(6, OUTPUT); //air
}

@Wawa. Thanks, new and interesting knowledgev for me. It ought to take action immedeately.

The old way was

digitalWrite(2, HIGH); // enable pull up resistor
pinMode(2, OUTPUT): // then set pin to output

Both (still) work.
Leo..

That was my suggestion in #3.

Thank you! Will give it a try tonight.

I continued searching after I posted and just found this thread from long ago:
https://forum.arduino.cc/index.php?topic=216544.0

It came to the same resolution...use:

pinMode(pin, INPUT_PULLUP);
pinMode(pin, INPUT_PULLUP);

pinMode(relayOne, OUTPUT);
pinMode(relayTwo, OUTPUT);

All due, I guess, to these relay boards being pre-set active LOW.

Beginning to realizing that there is so much already posted about things Arduino that learning to search is as important as learning to write sketches. Again, thank you for the help.

1 Like

The INPUT_PULLUP approach worked perfectly. Thank you.

BUT...with these normally --"on" when LOW/ "off" when HIGH"-- relays, I totally confused myself when looking at the whole system of 18 separate relays and the power they would consume to maintain the needed contact status. When to run LOW or HIGH? Use N/O or N/C contacts? Pin, board and relay current limits?

So I made a table that made it clearer for me. It's the attached PDF with explanation. Look right/wrong? Helpful or not? Any additional advice?

With the option I chose, the sketch opens with all relays low, so to prevent them all bouncing "on" at once when the board powers up, I used the INPUT_PULLUP as suggested. Easy solution..thank you.

Arduino Relay Board.pdf (112 KB)

1 Like

Arduino pin current (sink) is ~2mA (not 5mA) for common relay boards with optocouplers.

There is an opto LED (~1.2volt), and indicator LED (~1.8volt) and a 1k resistor in series.
The remaining 2volt across the 1k resistor results in ~2mA drive current.
Apart from the ~75mA relay coil current (that should come from a seperate 5volt supply).
Leo..

Wawa...
Thanks..made corrections in the current requirements and re-posted the corrected table.