using Arduino UNO 8ch Relay starts ON

Hi, im kinda new here.... having a bit of a problem,

using an Arduino UNO and 8 channel relay.

everytime arduino starts or powered all 8 channel is ON.

what can I do to make it starting state as OFF.

heres the code,

char command;
String string;
const int Relay1 =  12;      
const int Relay2 =  11;
const int Relay3 =  10;
const int Relay4 =  9;
const int Relay5 =  8;    
const int Relay6 =  7;
const int Relay7 =  6;
const int Relay8 =  5;



void setup() {
Serial.begin(9600);
pinMode(Relay1, OUTPUT);    
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Relay5, OUTPUT);    
pinMode(Relay6, OUTPUT);
pinMode(Relay7, OUTPUT);
pinMode(Relay8, OUTPUT);

}

 void loop()  {
 if (Serial.available() > 0) 
    {string = "";}
    while(Serial.available() > 0){
    command = ((byte)Serial.read());
      
  if(command == ':'){
        break;
    }
  else {
        string += command;
    }
     delay(1);
    }
    
///////////////
    
  if(string == "a"){
    digitalWrite(12,0);
    delay(1);    
     }
    if(string == "b"){      
    digitalWrite(11,0);
    delay(1);    
     }
    if(string == "c"){
    digitalWrite(10,0);
    delay(1);    
      }
    if(string == "d"){      
     digitalWrite(9,0);
     delay(1);    
      }
   if(string == "e"){
    digitalWrite(8,0);
    delay(1);    
     }
   if(string == "f"){      
    digitalWrite(7,0);
    delay(1);    
     }
   if(string == "g"){
    digitalWrite(6,0);
    delay(1);    
      }
  if(string == "h"){      
     digitalWrite(5,0);
    delay(1);    
      }
      
///////

    if(string == "A"){
     digitalWrite(12,1);
     delay(1);   
    }
    if(string == "B"){
     digitalWrite(11,1);
     delay(1);   
    }
    if(string == "C"){
     digitalWrite(10,1);
     delay(1);   
    }
    if(string == "D"){
     digitalWrite(9,1);
     delay(1);   
    }
   if(string == "E"){
     digitalWrite(8,1);
     delay(1);   
    }
   if(string == "F"){
     digitalWrite(7,1);
     delay(1);   
    }
   if(string == "G"){
     digitalWrite(6,1);
     delay(1);   
    }
   if(string == "H"){
     digitalWrite(5,1);
     delay(1);   
    }
}

hope someone can help me..

thanks!

Please edit your post to use code tag, see "How to use the forum". If you would have done that I would have given you improved code :slight_smile:

You probably meant to write:

B4663D:
using an Arduino UNO and 8 channel relay module.

Which is very very different :wink: Devil is in the details.

But the problem probably is that a lot of those modules are active LOW. Aka, they turn on when you give them a LOW (sink). Simply digitalWrite() the pin HIGH before calling pinMode() on the pin.

ow sorry bout that...

its fixed already! thanks iv added digitalWrite (Relay1, HIGH); on the top...

thanks! more power

No thanks for you, because you didn't edit your post :frowning:

ok

Thank you! Helps keeping the forum clear. In return, simplified code :slight_smile:

Simplified code:

const byte RelayPins[] = {12, 11, 10, 9, 8, 7, 6, 5};
const byte NrRelayPins = sizeof(RelayPins);

void setup() {
  Serial.begin(9600);
  
  for(byte i = 0; i < NrRelayPins; i++){
    digitalWrite(RelayPins[i], HIGH);
    pinMode(RelayPins[i], OUTPUT);
  }
}

void loop()  {
  if (Serial.available() > 0){
    const char c = Serial.read();

    //turn on commands
    if((c >= 'a') && (c <= 'z') && ((c - 'a') < NrRelayPins)){
      digitalWrite(RelayPins[c - 'a'], LOW);
    }
    //turn off commands
    else if((c >= 'A') && (c <= 'Z') && ((c - 'A') < NrRelayPins)){
      digitalWrite(RelayPins[c - 'A'], HIGH);
    }
  }
}
1 Like

wow! super shorter and simpler coding....

thanks a lot ill try to refresh my programming again...

=)