My output Relays activate on power up or reload.

Greetings all,

I am 2 days into the world of Arduino and have learned enough to get a simple sketch running on my UNO R3. The code turns on and off 3 different relay boards in a sequence with some delays.

The issue i have in when I plug power up or reload the sketch to the UNO it energizes the relays quickly
then leaves them on...before i press the start/stop (buttonpin)..any thoughts? The relay board are active low so the energize when a logic low is on the input pin.

Shetch 1

// This sequence relays on an off
const int buttonPin = 2; // assigns digital input 2 the name buttonPin
const int ledPin1 = 11; //assigns digital output 11 the name ledPin1
const int ledPin2 = 12; //assigns digital output 12 the name ledPin2
const int ledPin3 = 13; //assigns digital output 13 the name ledPin3

int x = 1;
// variables will change:
int buttonState = 0;
int oldButtonState = LOW;

void setup() {
// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

}

void loop()
{
// Get the current state of the button
int newButtonState = digitalRead(buttonPin);

// Has the button gone high since we last read it?
if (newButtonState == HIGH && oldButtonState == LOW) {

if (x == 0) {
// Toggle on led1
digitalWrite(ledPin1, HIGH); // turn on fan
delay(5000); // delay xxxx sec

// Toggle on led2
digitalWrite(ledPin2, HIGH); // turn on pump
delay(100); // delay xxxx sec

// Toggle on led3
digitalWrite(ledPin3, LOW); // turn on ignition
delay(2000); // ignition on time xxxx sec

digitalWrite(ledPin3, HIGH); // turn off ignition

x = 1; // toggle PButton state indicator to 1

} else { // if outputs 1 2 3 on turn off
// shut down sequence
digitalWrite(ledPin2, LOW); // turn off pump
delay(5000); // cool down delay xxxx
digitalWrite(ledPin1, LOW); // turn off fan
delay(1000);

x = 0; // toggle PButton state indicator to 0
}
}

// Store the button's state so we can tell if it's changed next time round
oldButtonState = newButtonState;
}

I've not studies the startup sequence in detail, however it sounds like the only way to stop the turn on at power up is to change the drive to the relays. Even better would be if the relay board had an option to power them on when the input is high.

You could minimize the issue by initiating the relay outputs to high in your setup(), however they will still "click" on on power up.

Why? On power up the Arduino outputs are low as the Arduino goes through its startup code. Until the processor is initialized and the Setup() function is complete you have no control of the outputs.

write your pins HIGH in setup? the ones that are active low, anyway.

This:

digitalWrite(ledPin1, HIGH); // turn on fan

digitalWrite(ledPin3, LOW); // turn on ignition

tells me either only one of them is on the relay, or you've got something screwy going on.

To stop active LOW relays from chattering during bootup, do this.
In setup(), write a HIGH to the pin, before you set the pin to OUTPUT.

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

On an Uno, don't use pin 0 or 1, and avoid pin 13.
Leo..

3 Likes

Hi I tried all the suggestion with mixed results.

The chattering on boot up is gone but it seems to run the code in my else statement at the bottom of code on bootup, not sure why.
The led for the cooldownled turns on for 10 sec and turns off. Then runs as hoped.

// This will sequence the ouputs after button press 1 and and a shutdown sequence on press 2:
const int buttonPin = 2;             // assigns digital input 2 the name buttonPin
const int fan = 12;                  //assigns digital output 12 the name fan
const int pump = 11;                 //assigns digital output 11 the name pump
const int igniter = 10;              //assigns digital output 10 the name igniter
const int cooldownled = 9;           //assigns digital output 9 the name  cooldownled

int x = 1;

// variables will change:
int buttonState = 0;
int oldButtonState = LOW;


void setup() {
// initialize the load pins as an outputs:

pinMode(igniter, INPUT_PULLUP); //initalize as an input first so during power up it is not energized
pinMode(fan, OUTPUT);               // pin 12
pinMode(pump, OUTPUT);              // pin 11
pinMode(igniter, OUTPUT);           // pin 10
pinMode(cooldownled, OUTPUT);       // pin 9

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

}


void loop()
{
// Get the current state of the button
int newButtonState = digitalRead(buttonPin);

// Has the button gone high since we last read it?
if (newButtonState == HIGH && oldButtonState == LOW) {

  if (x == 0) {
    // Toggle on fan
    digitalWrite(fan, HIGH);          // turn on fan
    delay(2000);                      // delay xxxx sec

    // Toggle on pump
    digitalWrite(pump, HIGH);         // turn on pump
    delay(2000);                      // delay xxxx sec

    // Toggle on igniter
    digitalWrite(igniter, LOW);       // turn on ignition relay active low
    delay(2000);                      // ignition on time xxxx sec

    digitalWrite(igniter, HIGH);      // turn off ignition


    x = 1;                            // toggle PButton state indicator to 1
    

  } else {                            // if pump is on turn off and enter cool down mode
    
    // shut down sequence
    digitalWrite(pump, LOW);          // turn off pump
    digitalWrite(cooldownled, HIGH);  // turn on cool down led
    delay(10000);
   
                    
    digitalWrite(fan, LOW);           // turn off fan
    digitalWrite(cooldownled, LOW);        // turn off cool down led
  

    x = 0;                            // toggle PButton state indicator to 0
    }
 }
                       
// Store the button's state so we can tell if it's changed next time around
oldButtonState = newButtonState;
}

Hi All
Sorry for not posting code properly,,,I hope this is the correct way. :confused:

JohnRob:
I've not studies the startup sequence in detail, however it sounds like the only way to stop the turn on at power up is to change the drive to the relays. Even better would be if the relay board had an option to power them on when the input is high.

You could minimize the issue by initiating the relay outputs to high in your setup(), however they will still "click" on on power up.

Why? On power up the Arduino outputs are low as the Arduino goes through its startup code. Until the processor is initialized and the Setup() function is complete you have no control of the outputs.

His problem is the moronic blinking "Pin 13" LED of the bootloader.

Best thing to do is go through the bootloader code, remove all the blinky garbage, then recompile and burn a new bootloader.

Hi krupski

I am not a coder and have only been working with Arduino a few days reading tons of material and trying to absorb all the wisdom shed upon me by folks here. I am not using pin 13 due to problems I had getting things to work thus far. The blinking on reboot or download has been corrected but now when I reboot the code the else statement seems to run, please see my code, I bet it is something simple I have not learned yet, please have a look!
Thanks in advance
Mike

In setup do:

  oldButtonState = digitalRead(buttonPin);

Then you won't falsely trigger on it.

Hi Mark,

Thanks for the quick reply! Adding oldButtonState = digitalRead(buttonPin);

On power up:
All is ok until I press the buttonpin to start the sequence, it runs the instructions in the else statement.Then the second press it starts as where it should at the beginning of code.

On reset using switch on UNO:
I press the buttonpin to start the sequence, it runs the instructions in the else statement.Then the second press it starts where it should at the beginning of code.

On download:
Same thing happens.

// This will sequence the ouputs after button press 1 and and a shutdown sequence on press 2:
const int buttonPin = 2;             // assigns digital input 2 the name buttonPin
const int fan = 12;                  //assigns digital output 12 the name fan
const int pump = 11;                 //assigns digital output 11 the name pump
const int igniter = 10;              //assigns digital output 10 the name igniter
const int cooldownled = 9;           //assigns digital output 9 the name  cooldownled

int x = 1;

// variables will change:
int buttonState = 0;
int oldButtonState = LOW;

void setup()
{
  Serial.begin(9600);
  while (! Serial);                        // Wait until Serial is ready
  Serial.println("KOBE program started");  // print KOBE program started
  oldButtonState = digitalRead(buttonPin);
  // initialize the load pins as an outputs
  pinMode(igniter, INPUT_PULLUP);          //initalize as an input first so during power up it is not energized
  pinMode(fan, OUTPUT);               // pin 12
  pinMode(pump, OUTPUT);              // pin 11
  pinMode(igniter, OUTPUT);           // pin 10
  pinMode(cooldownled, OUTPUT);       // pin 9

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

}

void loop()
{
  // Get the current state of the button
  int newButtonState = digitalRead(buttonPin);


  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && oldButtonState == LOW) {

    if (x == 0) {
      Serial.println("button pressed"); // print button pressed

      // Toggle on fan
      Serial.println("fan on");
      digitalWrite(fan, HIGH);          // turn on fan
      delay(2000);                      // delay xxxx sec

      // Toggle on pump
      Serial.println("pump on");
      digitalWrite(pump, HIGH);         // turn on pump
      delay(2000);                      // delay xxxx sec

      // Toggle on igniter
      Serial.println("igniter on");
      digitalWrite(igniter, LOW);       // turn on ignition relay active low
      delay(2000);                      // ignition on time xxxx sec

      digitalWrite(igniter, HIGH);      // turn off ignition
      Serial.println("igniter off");

      x = 1;                            // toggle PButton state indicator to 1


    } else {                            // if pump is on turn off and enter cool down mode

      // shut down sequence
      digitalWrite(pump, LOW);          // turn off pump
      Serial.println("pump off");
      digitalWrite(cooldownled, HIGH);  // turn on cool down led
      Serial.println("cool down led on");
      delay(5000);


      digitalWrite(fan, LOW);           // turn off fan
      digitalWrite(cooldownled, LOW);        // turn off cool down led


      x = 0;                            // toggle PButton state indicator to 0
    }
  }

  // Store the button's state so we can tell if it's changed next time around
  oldButtonState = newButtonState;
}

I think I found the error.
I had the X variable = to 1 instead of 0 in the declaration section at the top.. :slight_smile:

int x = 1;