Relay energising on boot

Hi.

I’ve made a simulated bomb with a timer for an airsoft site I work at. I have connected a 9v circuit via a relay using the NO contact.

When the arduino is not powered, the circuit is broken and remains so. As soon as it goes through the startup, it activates and sets off the ematch prematurely. I don’t want the circuit to be completed until the point where the user fails to enter the correct code.

Here is my startup:

void setup() {
  pinMode(ledPin, OUTPUT); // sets the digital pin as output
  pinMode(ledPin2, OUTPUT); // sets the digital pin as output
  pinMode(ledPin3, OUTPUT); // sets the digital pin as output
  pinMode(ematch, OUTPUT);
  digitalWrite(ematch, HIGH);
  lcd.begin(16, 2);
  Serial.begin(9600);
  readVcc();
  if (readVcc() >= 3800)
  {
    lcd.setCursor(0, 0);
    lcd.print("voltage is:");
    lcd.print(readVcc() / 1000.00);
    lcd.print("v");
    lcd.setCursor(0, 1);
    lcd.print("voltage good!");
    digitalWrite(ledPin3, HIGH); // sets the LED on
    delay(3000);
    digitalWrite(ledPin3, LOW); // sets the LED off
    delay(3000);
  }
  if (readVcc() <= 4700)
  {
    lcd.setCursor(0, 0);
    lcd.print("voltage is:");
    lcd.print(readVcc() / 1000.00);
    lcd.print("v");
    lcd.setCursor(0, 1);
    lcd.print("replace battery!");
    digitalWrite(ledPin, HIGH); // sets the LED on
    delay(3000);
    digitalWrite(ledPin, LOW); // sets the LED off
    delay(1000000);
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   Enter Code");
  while (currentLength < 4)
  {
    lcd.setCursor(currentLength + 6, 1);
    lcd.cursor();
    char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*') && (key != '#'))
      {
        lcd.print(key);
        password[currentLength] = key;
        currentLength++;
      }
    }
  }

Thanks!

pinMode(ematch, OUTPUT); digitalWrite(ematch, HIGH); Try: digitalWrite(ematch, HIGH); pinMode(ematch, OUTPUT);

Thanks, will try.

It's not clear, to me, if, when you first power it up, if the Relay closes, then opens. Or if it closes, then stays closed. BUT, since I don't see, anywhere in your code, a place where, keying in the correct code, changes the state of ematch, I'm guessing that the relay closes, and stays closed.

If that's the case, then merely changing

digitalWrite(ematch, HIGH);

to

digitalWrite(ematch, LOW);

should fix that.

Then, you'll need to add the code that changes that state to

digitalWrite(ematch, HIGH);

when the correct code is keyed in.

Also, it would be immensely useful if you provided a schematic.

And, indubitably sweet if you took the time to read:

How to use this forum - please read

Oops!

You do not understand that most of these relay boards are active LOW so that the original code is well-intentioned (I cannot see any part where the "ematch" is actually fired; I suppose the OP needs to get it safe to start with!) but has the problem that

 pinMode(ematch, OUTPUT);
  digitalWrite(ematch, HIGH);

allows a quite brief interval where the output is actually set LOW (as this is the default state of the output register) and this is long enough to saturate the optocoupler and briefly close the relay.

If this is to control pyrotechnics, that is a bad problem.

As larryd points out, performing

 digitalWrite(ematch, HIGH);
  pinMode(ematch, OUTPUT);

means that the output pin is set to HIGH (and in fact, that is actually INPUT_PULLUP) before it is defined as an output. This is necessary when using these (opto-isolated) active LOW relay modules and works just fine.