Run away relays when external power applied

Well, to start with, I cannot find a category for the problem I'm having. And, I have done as much searching as I know how too to find an answer to the problem.

So, in a nut shell, the problem is the relays run wild, faster and faster if left un-plugged.

This happens when using an 8 Relay Module, Arduino Uno (two different boards, and two or three different sketches, attaching a (measured 4.95dc from a 2elego supply) to the GND and JD-VCC of the 8 Relay Module.

The sketch and the 8 Relay Module work fine (normally, see below) when using the arduino-uno board power supply (cap on VCC, JD-VCC.

I am using a downloaded Delphi Kontrol program to switch on/off. FYI, the Delphi is not the problem because at times, when reworking the sketch to find the problem, the relays will run wild without Delphi connected.

[note: I had previously referenced the wrong site]
Correct site: Project III - 6. Control 8 Channel Relay with Delphi Apps on PC (Arduino Based) | basic arduino tutorial
One exception here: the on buttons and the off buttons are reversed on Delphi Kontrol.
I began with this sketch, but have tried others.

Currently working with the one seen below.
However, I'm missing something to make to make Arduino Uno activate the relays. Delphi is working, indicated by the TX light blinking when a button is pushed.

The relays are all on (why?). And when the external power is connected as discribed above, the relays run wild.
When the sketch is correct, I can turn the relays on and off, but when all eight are on, they run wild (non stop on and off).

So, first help me fix this particular sketch, then we can address the root problem of run-away relays.

The lcd works fine. And by the way, I am confident the "basic" wiring between the Arduino board and the relays is correct. See most anyones 8 channel wiring diagram.

[Sketch originally used] This makes the relays come on when loaded, the go into run-away mode.

Note: I have not changed this sketch.

#include <LiquidCrystal.h>
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
char b;
void setup(){
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
digitalWrite(9, 0);
digitalWrite(10, 0);
digitalWrite(11, 0);
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Kontrol 8 Relay");
lcd.setCursor(0,1);
lcd.print(" via Delphi");
delay(2000);
lcd.setCursor(0,1);
lcd.print("Tunggu Koneksi..");
do{
b = Serial.read();
}
while(b != 'R');
lcd.setCursor(0,1);
lcd.print("Koneksi OK ");
}
void loop(){
b = Serial.read();
switch(b){
//---------------------------Perintah Relay ON
case 'A': lcd.setCursor(0,1);
lcd.print("Relay 1 ON ");
digitalWrite(4, 1); // Relay1 on
break;
case 'B': lcd.setCursor(0,1);
lcd.print("Relay 2 ON ");
digitalWrite(5, 1); // Relay2 on
break;
case 'C': lcd.setCursor(0,1);
lcd.print("Relay 3 ON ");
digitalWrite(6, 1); // Relay3 on
break;
case 'D': lcd.setCursor(0,1);
lcd.print("Relay 4 ON ");
digitalWrite(7, 1); // Relay4 on
break;
case 'E': lcd.setCursor(0,1);
lcd.print("Relay 5 ON ");
digitalWrite(8, 1); // Relay5 on
break;
case 'F': lcd.setCursor(0,1);
lcd.print("Relay 6 ON ");
digitalWrite(9, 1); // Relay6 on
break;
case 'G': lcd.setCursor(0,1);
lcd.print("Relay 7 ON ");
digitalWrite(10, 1); // Relay7 on
break;
case 'H': lcd.setCursor(0,1);
lcd.print("Relay 8 ON ");
digitalWrite(11, 1); // Relay8 on
break;
//-------------------------- Perintah Relay OFF
case 'a': lcd.setCursor(0,1);
lcd.print("Relay 1 OFF ");
digitalWrite(4, 0); // Relay1 off
break;
case 'b': lcd.setCursor(0,1);
lcd.print("Relay 2 OFF ");
digitalWrite(5, 0); // Relay2 off
break;
case 'c': lcd.setCursor(0,1);
lcd.print("Relay 3 OFF ");
digitalWrite(6, 0); // Relay3 off
break;
case 'd': lcd.setCursor(0,1);
lcd.print("Relay 4 OFF ");
digitalWrite(7, 0); // Relay4 off
break;
case 'e': lcd.setCursor(0,1);
lcd.print("Relay 5 OFF ");
digitalWrite(8, 0); // Relay5 off
break;
case 'f': lcd.setCursor(0,1);
lcd.print("Relay 6 OFF ");
digitalWrite(9, 0); // Relay6 off
break;
case 'g': lcd.setCursor(0,1);
lcd.print("Relay 7 OFF ");
digitalWrite(10, 0); // Relay7 off
break;
case 'h': lcd.setCursor(0,1);
lcd.print("Relay 8 OFF ");
digitalWrite(11, 0); // Relay8 off
break;
//----------------------------koneksi
case 'R': lcd.setCursor(0,1);
lcd.print("Koneksi OK ");
break;
case 'r': lcd.setCursor(0,1);
lcd.print("Koneksi OFF ");
break;
}
}`Preformatted text`

Thanks, Daniel

Current Sketch:

// Found Home Automation System at https://harshsharmatechnicals.com/2018/01/30/code-and-circuit-diagram-for-home-automation-system-using-arduino/

// 1) Home Automation System
// CODE :-

#include <ezButton.h> // Debounce added in an effort to stop relays from
                                        // run-away effect.
ezButton button1 (3);  // create ezButton object that attach to pin 3;
ezButton button2 (4);
ezButton button3 (5);
ezButton button4 (6);
ezButton button5 (7);
ezButton button6 (8);
ezButton button7 (9);
ezButton button8 (10);


#include <LiquidCrystal.h>
const int rs = 14, en = 15, d4 = 16, d5 = 17 ,  d6 = 18, d7 = 19;


LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //make these pins output
int output;

// ****** Home Automation Code starts here *****************
//****** With codes added to use Liquid Crystal  ****

#define relay1 3   //Connect relay1 to pin 3
#define relay2 4   //Connect relay2 to pin 4
#define relay3 5    //Connect relay3 to pin 5
#define relay4 6    //Connect relay4 to pin 6
#define relay5 7    //Connect relay5 to pin 7
#define relay6 8    //Connect relay6 to pin 8
#define relay7 9    //Connect relay7 to pin 9
#define relay8 10    //Connect relay8 to pin 10

char b;
char c;

// ************************** Setup begins

void setup()
{
  Serial.begin(9600);     //Set rate for communicating with adelphi

// Debounce added in an effort to stop relays from Run-away. 

  button1.setDebounceTime(500);   // set debounce time to 100 milliseconds
  button2.setDebounceTime(500);
  button3.setDebounceTime(500);
  button4.setDebounceTime(500);
  button5.setDebounceTime(500);
  button6.setDebounceTime(500);
  button7.setDebounceTime(500);
  button8.setDebounceTime(500);

  pinMode(relay1, OUTPUT); //Set relay2 as an output
  pinMode(relay2, OUTPUT); //Set relay1 as an output
  pinMode(relay3, OUTPUT); //Set relay2 as an output
  pinMode(relay4, OUTPUT); //Set relay1 as an output
  pinMode(relay5, OUTPUT); //Set relay2 as an output
  pinMode(relay6, OUTPUT); //Set relay1 as an output
  pinMode(relay7, OUTPUT); //Set relay2 as an output
  pinMode(relay8, OUTPUT); //Set relay1 as an output


  digitalWrite(relay1, LOW); //Swtich relay1 off
  digitalWrite(relay2, LOW); //Switch relay2 off
  digitalWrite(relay3, LOW); //Swtich relay3 off
  digitalWrite(relay4, LOW); //Switch relay4 off
  digitalWrite(relay5, LOW); //Swtich relay5 off
  digitalWrite(relay6, LOW); //Switch relay6 off
  digitalWrite(relay7, LOW); //Swtich relay7 off
  digitalWrite(relay8, LOW); //Switch relay8 off


  lcd.begin(16, 1);
  lcd.setCursor(0, 1);
  lcd.print("ok Daniel");
  delay(4000);
  do
    b = Serial.read();

  while (b != 'R');
  lcd.setCursor(2, 1);
  lcd.print("What? OK");
}
void loop()    //       ******************  void loop begins
{
  Serial.begin(9600);
  b = Serial.read();
  //  switch (b);
  while (Serial.available()) //Check if there are available bytes to read
  {
    delay(50); //Delay to make it stable
    char c = Serial.read(); //Conduct a serial read
    c = Serial.read();
    if (c == '#') {
      break; //Stop the loop once # is detected after a word
    }
    inputs += c; //Means inputs = inputs + c
  }
  if (inputs.length() > 1)
  {
    Serial.println(inputs);

    if (inputs == "A")          //Relay 1
    {
      digitalWrite(relay1, LOW);     //*******
    }
    else if (inputs == "a")     //*******
    {
      digitalWrite(relay1, HIGH);     //Relay 1
    }
  }
  else if (inputs == "B")        //Relay 2
  {
    digitalWrite(relay2, LOW);
  }
  else if (inputs == "b")
  {
    digitalWrite(relay2, HIGH);    //Relay 2
  }
  else if (inputs == "C")        //Relay 3
  {
    digitalWrite(relay3, LOW);
  }
  else if (inputs == "c")
  {
    digitalWrite(relay3, HIGH);   // Relay 3
  }
  else if (inputs == "D")
  {
    digitalWrite(relay4, LOW);
  }
  else if (inputs == "d")
  {
    digitalWrite(relay4, HIGH);
  }
  else if (inputs == "E")
  {
    digitalWrite(relay5, LOW);
  }
  else if (inputs == "e")
  {
    digitalWrite(relay5, HIGH);
  }
  else if (inputs == "F")
  {
    digitalWrite(relay6, LOW);
  }
  else if (inputs == "f")
  {
    digitalWrite(relay6, HIGH);
  }
  else if (inputs == "G")
  {
    digitalWrite(relay7, LOW);
  }
  else if (inputs == "g")
  {
    digitalWrite(relay7, HIGH);
  }
  else if (inputs == "H")
  {
    digitalWrite(relay8, LOW);
  }
  else if (inputs == "h")
  {
    digitalWrite(relay8, HIGH);
  }
  inputs = "";
  return;

}

//break // if (inputs.length() .0;}`Preformatted text``Preformatted text`

I am not going to search though that mess of code, but did you put in serial print statement next to the places where you turn on a relay so you you can trace the logic?
Please explain exactly what the relays are doing. Your description does not convey any possible thing a relay can do, they only turn on or turn off.
Paul

You use the same pins for all your ezButtons as you do for your relays but those buttons are never actually used in your code...

You might want to read up on using arrays in C++ as it will simplify your code tremendously.

Please edit your post and select all code and click the </> button; next save your post.

void loop() // ****************** void loop begins
{
Serial.begin(9600);

I don't see a reason to have Serial.begin in loop(); get rid of it.

b = Serial.read();

Read some data, regardless if something is available or not and don't use the result by the looks of it.

char c = Serial.read(); //Conduct a serial read
c = Serial.read();

Read a character and throw it away by reading the next character which might or might not be there.

Stopping now till you have fixed your post (and I have access to a PC.

The 8-relay module cannot be powered by the Arduino ... there's not enough capacity to drive more than a few relays simultaneously.

If you're not already using one, I suggest using a separate 5VDC supply (1A minimum) and then follow this diagram to take advantage of the built-in opto-isolation feature:

The only thing wrong with that diagram is the grounds are not tied together (unless by good fortune or accident).

The Arduino outputs need a ground reference to be relevant to the relay module…

Worst case with cheap floating chinesium plug packs, the logic outputs could be at any voltage… don’t use wet hands !

Thanks for all the input. I will work on this tomorrow. I just came in from work.
Dan

Please re-read my post from the begining. The run-away relays is the main problem.

(or by design).
A common ground will disable opto-isolation. The relay module's opto-driver is just an IRLED and visible LED driven by VCC and an Arduino output. Output LOW turns ON the relay. Output HIGH or I/O configured as Input turns OFF the relay.

Yeah, I know. This happens when there's not enough power available (overload), i.e with all relays ON. The circuit referenced in the first post isn't ideal ... a 4-relay module and definitely an 8-relay module needs a separate supply. I take it that your 4.95V measurement wasn't with all relays energized, since they're oscillating and the multimeter wouldn't give steady or accurate readings.

Sorry, not obvious, the pins aren’t identified.

I have read your post. In working with relays for more than 60 years, I have never had one run away.
Paul

1 Like

The grounds are connected internally.

What the relays will do is to be determined. They are only switches so I have nothing attached to them yet.

Thanks, I will do that. At the moment they are not a problem, I am learning the use of different elements as I go. I have intergrated different skitches trying to find the problem with the run-away relays. The original sketch I used ran fine except when all relays where turned on, Then they would go wild. Faster and faster, until I shut them off or pulled the power.
I would like to know now, before the run-away problem, what is preventing the relay board getting signal. There is a signal going in on the Arduino-Uno board because I can see the TX light flashing when I press a button.
Thanks for the help.

The Serial.begin(9600) is what connects the board to my computer, and the Dephi Kontrol program to the board.

I wasn't clear: I understand HIGH / LOW. What I don't understand is (like in a previous sketch, which I will try to get it working and load here to compare this one already loaded.), when I first run the sketch, all the relays are on instead of off. If the code is reversed, the it doesn't work.

I did read with all energized. Also, when I don't use the external power, when using the arduino power, all relays work as expected, i.e. all on - never going into run-away mode. But when the external power is connected...

So, what do you think might be the problem? I've used two differ boards, two different sketches, and two different relay boards, with and without exteral power. All the basic wiring is correct - (my former trade was Electrician).

Please look at my first post again. I have changed the site I referenced for the original code. With this code load, the relays work up to the point I turn all of them on. Then they "run-away".

I don't think it is the code because i've tried other codes as well.

(As someone suggested, all grounds are connect with a wire, but it does not make a difference.)Preformatted text

Hi,
If you have new or edited code to post, please do it in a new post, going back and editing previous posts makes the thread flow confusing.

Can you please post an image(s) of your project please?
What external power supply are you using and where have you connected it to your project?

Have you measured the 5V pin on your controller when the relay problem occurs?
What model Arduino are you using?

Can you please post a circuit diagram, showing pin and component labels as well as your power supply connections?

Thanks.. Tom... :smiley: :+1: :coffee: :australia: