8- Channel Relay module buzzing (whining)

Hello!
I´m making a Project, where I have to use 8 Relay Modules
I have 3 buttons and i wrote codes for each one of them SEPARATELY(i know its dumb) , the codes work without any buzzing sounds (separately) but after combining them when it comes to the parts with millis() function they start to whine.
SO thats my code, this is a part 1. there are part second and third, the one and only difference between that parts are, that they are controlled by different Buttons:

void first()
{ boolean buttonState = digitalRead(button1Pin);
  if (buttonState == HIGH && lastButtonState == LOW) {
blink3State=false;
    ledon3=false;
    ledon2=false;
 currentMillis = millis(); 
    digitalWrite(LED1, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, LOW);
    ledon1 = true;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(relay3Pin, HIGH);
       digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);

    delay (10000);
 
  }
   if (ledon1 == true)
    blinkState = !blinkState;
     blink3State=false;
    currentMillis=millis();
  if (blinkState )
  { blink2State=false;
  blink3State=false;
   digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
 
    digitalWrite(relay3Pin, HIGH);
ledon1=false;
    if (currentMillis - blinkStartMillis > interval)
    {
      blinkStartMillis = currentMillis;   // aktuelle Zeit abspeichern
    
      LEDstat1 = !LEDstat1;// LED Zustand wecheln.
      digitalWrite(LED1,LEDstat1);
 }


  }
      else {
    digitalWrite(LED1, LOW);   // LED Zustand wecheln
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
     
    digitalWrite(relay3Pin, LOW);

         digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
  }
  lastButtonState = buttonState;
  }

I appreciate everything you tell me.

Hi,
Welcome to the forum.
We need to see your complete code, sowe can see how you have setup your I/O and how it is structured.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.
We need to see your complete code, sowe can see how you have setup your I/O and how it is structured.

Thanks… Tom… :slight_smile:

Hi !
Thanks for your quick reply!
here is the whole code.
i know im using too many blocking functions, which i hate to use, but i wanted to avoid millis() etc…
because the delay function is the only one for which this relay module is quiet

forum.ino (9.56 KB)

Hi,
Ops code with Auto Format.

const int LED1 = 3;
const int LED2 = 4;
const int LED3 = 6;
int LED4 = 45;
const int button1Pin =  5;     // Wasserkalibrierung
const int button2Pin =  31;     // leerpumpen, formazin
const int button3Pin =  2;     // formzurück, spüle, leerpumpen
const int button4Pin =  7;     // NOTtaster
const int relay1Pin  =  8;      // the number of the Relay1 pin (ABWASSER)
const int relay2Pin  =  9;      // the number of the Relay2 pin(fORMAZINRÜCK)
const int relay3Pin  =  10;    // the number of the Relay3 pin (VE WASSER)
const int relay4Pin  =  11; // the number of the Relay4 pin (PUMPE)
const int relay5Pin  =  12;  // the number of the Relay5 pin (ABWASSER)
const int relay6Pin  =  13;  // the number of the Relay6 pin (3. reihe links)
const int relay7pin  =  48; //nichts
const int relay8pin  =  53; //auch nichts
const int pumpericht =  30;    //richtung der Pumpe
const int pumpeein   =  38; //pumpe ein.aus

// veränderliche Variablen
boolean lastButtonState;
boolean blinkState;
boolean LEDstat1;
boolean lastButtonState2;
boolean blink2State;
boolean LEDstat2;
boolean ledon2;
boolean ledon1;
boolean lastButtonState3;
boolean blink3State;
boolean LEDstat3;
boolean ledon3;
boolean lastButtonState4;
boolean blink4State;
boolean LEDstat4;
unsigned long blinkStartMillis;
unsigned long blink2StartMillis;
unsigned long blink3StartMillis;
unsigned long interval = 1000;
unsigned long interval2 = 1000;
unsigned long interval3 = 100;
unsigned long currentMillis;
void setup() {
  // put your setup code here, to run once:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  //  relay pins:
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);
  pinMode(relay5Pin, OUTPUT);
  pinMode(relay6Pin, OUTPUT);
  pinMode(relay7pin, OUTPUT);
  pinMode(relay8pin, OUTPUT);
  pinMode(pumpeein, OUTPUT);
  pinMode(pumpericht, OUTPUT);
}
void loop()
{
  first();
  second();
  third();
}
void first()  // put your main code here, to run repeatedly:
{ boolean buttonState = digitalRead(button1Pin);
  if (buttonState == HIGH && lastButtonState == LOW) {
    blink3State = false;
    ledon3 = false;
    ledon2 = false;
    currentMillis = millis();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, LOW);
    ledon1 = true;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(pumpeein, LOW);
    digitalWrite(pumpericht, LOW);
    delay (10000);
  }
  if (ledon1 == true)
    blinkState = !blinkState;
  blink3State = false;
  currentMillis = millis();
  if (blinkState )
  { blink2State = false;
    blink3State = false;
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(pumpeein, LOW); //(nicht gestartet)
    digitalWrite(pumpericht, LOW);
    digitalWrite(relay3Pin, HIGH);
    ledon1 = false;
    if (currentMillis - blinkStartMillis > interval)
    {
      blinkStartMillis = currentMillis;   // aktuelle Zeit abspeichern


      LEDstat1 = !LEDstat1;// LED Zustand wecheln.
      digitalWrite(LED1, LEDstat1);
    }
  }
  else {
    digitalWrite(LED1, LOW);   // LED Zustand wecheln
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(pumpeein, LOW); //(nicht gestartet)
    digitalWrite(relay3Pin, LOW);
    digitalWrite(pumpericht, LOW);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
  }
  lastButtonState = buttonState;
}
void second ()
{ boolean button2State = digitalRead(button2Pin);
  if  (button2State == HIGH && lastButtonState2 == LOW)
  {
    currentMillis = millis();
    blinkState = false;
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    ledon2 = true;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(relay5Pin, HIGH);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(pumpeein , HIGH);
    digitalWrite(pumpericht, HIGH); // dreht gegen urzeigesinn
    delay(10000);
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, HIGH);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(pumpericht, LOW); // vorwärts
    delay(10000);
    digitalWrite(pumpeein , HIGH);
    delay(1000);
  }
  if (ledon2 == true)
    blink2State = !blink2State;
  if (blink2State)
  {
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, HIGH);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(pumpeein , HIGH);
    digitalWrite(pumpericht, LOW);
    ledon2 = false;
    blinkState = false;
    if (currentMillis -  blink2StartMillis > interval2)
    {
      blink2StartMillis = currentMillis; // aktuelle Zeit abspeichern
      LEDstat2 = !LEDstat2;// LED Zustand wecheln.
      digitalWrite(LED2, LEDstat2);
      // Wert auf den Ausgang schreiben
    }
  }
  else
  {
    digitalWrite(LED2, LOW);
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(pumpeein, LOW); //(nicht gestartet)
    digitalWrite(relay3Pin, LOW);
    digitalWrite(pumpericht, LOW);
  }
  lastButtonState2 = button2State;
}
void third()
{
  boolean button3State = digitalRead(button3Pin);
  if (button3State == HIGH && lastButtonState3 == LOW) {
    blink2State = false;
    ledon2 = false;
    blinkState = false;
    currentMillis = millis();
    digitalWrite (LED1, LOW);
    digitalWrite (LED2, LOW);
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, HIGH);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(pumpeein , HIGH);
    digitalWrite(pumpericht, HIGH); //gegen Uhrzeigesinn
    delay(10000);
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(pumpeein , LOW);
    digitalWrite(pumpericht, LOW);
    delay (10000);


    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, HIGH);
    digitalWrite(relay5Pin, HIGH);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(pumpeein , HIGH); //( gestartet)
    digitalWrite(pumpericht, HIGH);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(LED3, HIGH);
    delay (10000);
  }
  else
  {
    digitalWrite(LED3, LOW);
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(pumpeein, LOW); //(nicht gestartet)
    digitalWrite(relay3Pin, LOW);
    digitalWrite(pumpericht, LOW);
  }
  lastButtonState3 = button3State;
}

Tom... :slight_smile:

TomGeorge:
Hi,
Ops code with Auto Format

TomGeorge:
Sure! Ill do it next time

but thats not the reason for the noisy relais :smiley: ahaha

Your code is hard to follow, but it appears to me that there are paths through it that set relays LOW and microseconds later set them HIGH and vice versa. That could cause the whining.

You have delays here and there that'll stop it, but not everywhere.

Hi,
Can you describe what you want each button to do?
eg
Button 1- does this...
Button 2- does this..
Button3- does this..

This looks like a switch.. case function may be needed.

Tom... :slight_smile:

ankama911:
i know im using too many blocking functions, which i hate to use, but i wanted to avoid millis() etc..
because the delay function is the only one for which this relay module is quiet

Total nonsense! :roll_eyes:

What is exactly total nonsense?
why should I lie?
Thats why I am writing here, if you can not help just dont say ANYTHING

Hi,
Can you answer post #9 please.
What do you want each button to do?

Thanks.. Tom... :slight_smile:

TomGeorge:
Can you answer post #9 please.
What do you want each button to do?

Since presently #9 is attributed to the OP and mine is now #8, it would seem that what was #9 has mysteriously disappeared. :astonished:

TomGeorge:
Hi,
Can you answer post #9 please.
What do you want each button to do?

Thanks.. Tom... :slight_smile:

Hey! Thank you in advance!
When the first button is pushed shortly i want the external LED and 2 Relays(first and third one) to stay on, then after 10 sec. I want the 3rd Relay to start blinking (so turning on and off) and be in this condition for another 10 seconds. After that I want both relais to stay on and the external LED also on, in this case i put him as a blinking one.
The second button is supposed to turn everything off, that the first button opened and open first fourth and fifth relay, thne wait and then open 2nd,4th and 6th , the External led should blink.
the third one does almost the same and then turns off.

Anka

i solved it
I just needed to use the switch case fct

thnx Tom!

TomGeorge:
This looks like a switch.. case function may be needed.

Tom... :slight_smile:

This code doesn't update lastButtonState:

{ boolean buttonState = digitalRead(button1Pin);
  if (buttonState == HIGH && lastButtonState == LOW) {
    blink3State = false;
    ledon3 = false;
    ledon2 = false;
    currentMillis = millis();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, LOW);
    ledon1 = true;
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay4Pin, LOW);
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay6Pin, LOW);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay7pin, LOW);
    digitalWrite(relay8pin, LOW);
    digitalWrite(pumpeein, LOW);
    digitalWrite(pumpericht, LOW);
    delay (10000);
  }

Add lastButtonState = buttonState; in the body of the if.

thanks!
it was not a problem.

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