Relay Board Noise/Whining

MEGA
HiLetGo 8 Channel Relay Board (12VDC)
Various pushbuttons and limit switches

I am working to establish a latching program that will keep the state of a relay constant upon pushbutton depression and after the pushbutton is released. The relay is to return to shelf-state upon input from a limit switch or another pushbutton.

My relay board emits a high-pitched, whining noise when I release the pushbutton. It does stay latched but the onboard LED's do dim. The relays to unlatch as desired but I suspect I am damaging my board. My suspicion is that my programming is calling for repetitive cycling of the output pins to the relay board inputs as it checks the state of the pushbuttons and latch variables. I just do not see how to change this. Perhaps the answer is in optimizing the code as it is messy at the moment but maybe not.

Any help is greatly appreciated!

ETA: I did already try converting the pushbutton state check to one that employs millis() but the result was the same.

ETA’d again: If I hold the push button in, defeating the purpose of the latch, there are no offending sounds and the onboard LEDs are normal brightness.

const int BUTTONfwd = 14; //righ fwd direction button
const int BUTTONrev = 15; //left rev direction button
const int BUTTONeSTOP = 3; //e-stop

int fwdState = 0;
int revState = 0;
int fwdLatch = 0;
int revLatch = 0;

const int limitLl = 16; //lower left limit switch
const int limitLu = 17; //upper left limit switch
const int limitRl = 18; //lower right limit switch
const int limitRu = 19; //upper right limit switch

void setup()
{
  pinMode(BUTTONfwd, INPUT);
  pinMode(BUTTONrev, INPUT);
  pinMode(BUTTONeSTOP, INPUT);
  pinMode(limitLl, INPUT);
  pinMode(limitLu, INPUT);
  pinMode(limitRl, INPUT);
  pinMode(limitRu, INPUT);

  pinMode(4, OUTPUT); //right lower
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT); //right upper
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT); //left lower
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT); //left upper
  pinMode(11, OUTPUT);

  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}



//////////////***DRIVING LOOP***//////////////////////////////////////////////////////////////////////////////////////////////
void loop () {
  shutterOp();
}

/////////////////////***ACTUATOR OPERATION/DIRECTION***/////////////////////////////////////////////////////////////////////////////////
void shutterOp() {

  //if both buttons not pressed, all relays in shelf state
  if (digitalRead(BUTTONeSTOP) == LOW || digitalRead(BUTTONfwd) == LOW && digitalRead(BUTTONrev) == LOW) {
    digitalWrite(4, LOW); //rl
    digitalWrite(5, LOW);
    digitalWrite(6, LOW); //ru
    digitalWrite(7, LOW);
    digitalWrite(8, LOW); //ll
    digitalWrite(9, LOW);
    digitalWrite(10, LOW); //lu
    digitalWrite(11, LOW);
  }

  if (fwdState == 0 && digitalRead(BUTTONfwd) == HIGH) {
    fwdLatch = 1;
    revLatch = 0;
  }

  if (fwdState == 1 && digitalRead(BUTTONfwd) == LOW) {
    fwdLatch = 1;
    revLatch = 0;
  }

  if (revState == 0 && digitalRead(BUTTONrev) == HIGH) {
    fwdLatch = 0;
    revLatch = 1;
  }

  if (revState == 1 && digitalRead(BUTTONrev) == LOW) {
    fwdLatch = 0;
    revLatch = 1;
  }
  if (digitalRead(BUTTONeSTOP) == LOW) {
    fwdLatch = 0;
    revLatch = 0;
  }
  //if fwd is pushed, all fwd relays change state, all rev relays remain in shelf state
  if (fwdLatch == 1 && digitalRead(BUTTONrev) == LOW) {
    if (digitalRead(limitRl) == HIGH) {
      digitalWrite(4, LOW);//rl
    }
    if (digitalRead(limitRl) == LOW) {
      digitalWrite(4, HIGH);//rl
    }
    digitalWrite(5, LOW);
    if (digitalRead(limitRu) == HIGH) {
      digitalWrite(6, LOW);//ru
    }
    if (digitalRead(limitRu) == LOW) {
      digitalWrite(6, HIGH);//ru
    }
    digitalWrite(7, LOW);

    if (digitalRead(limitLl) == HIGH) {
      digitalWrite(8, LOW); //ll
    }
    if (digitalRead(limitLl) == LOW) {
      digitalWrite(8, HIGH); //ll
    }
    digitalWrite(9, LOW);
    if (digitalRead(limitLu) == HIGH) {
      digitalWrite(10, LOW); //lu
    }
    if (digitalRead(limitLu) == LOW) {
      digitalWrite(10, HIGH); //lu
    }
    digitalWrite(11, LOW);
    if (digitalRead(limitLl) == HIGH && digitalRead(limitLu) == HIGH && digitalRead(limitRu) == HIGH && digitalRead(limitRl) == HIGH) {
      fwdLatch = 0;
    }
  }

  //if rev is pushed, all rev relays change state, all fwd relays remain in shelf state
  if (revLatch == 1 && digitalRead(BUTTONfwd) == LOW) {
    digitalWrite(4, LOW);//rl
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);//ru
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW); //ll
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW); //lu
    digitalWrite(11, HIGH);
  }

  //if both fwd and rev are pushed, all relays in shelf state
  if (digitalRead(BUTTONeSTOP) == LOW || digitalRead(BUTTONfwd) == HIGH && digitalRead(BUTTONrev) == HIGH) {
    digitalWrite(4, LOW); //rl
    digitalWrite(5, LOW);
    digitalWrite(6, LOW); //ru
    digitalWrite(7, LOW);
    digitalWrite(8, LOW); //ll
    digitalWrite(9, LOW);
    digitalWrite(10, LOW); //lu
    digitalWrite(11, LOW);
  }
}

MAybe not change it, but put a 1 second delay in there to find the spot causing the trouble.
Paul

1 Like

Shorter and less error prone:

      digitalWrite(4, digitalRead(limitRl) == LOW);