Arduino relay problems

Hi, I have a project that I have been working on for a while. I have wired everything as best I can, but I have some problems that I don’t understand. I am using an arduino uno to control nine relay modules (4 double and one single). I power everything with a power supply (12v 30A). The relays are powered from this through a voltage regulator (5 volt) and the arduino with a jack from the power supply.

When I power it up it is almost like one of the relays are floating or something, it flickers on and off when it should be on. When it’s off it stays off, so it is when it receives signal from the arduino.

Any ideas?

using a voltmeter connect the black lead to ground and the red to whatever pin your using. If it says the relay is on it should read 5 volts if it is off it should read 0.

The relay is getting power, it is when it’s getting signal from the arduino it’s starting to flicker. And it’s only one of the two relays on a dual module that flickers.

Post your code, and your wiring!
I just hope you are not using pin 13 to drive the RELAY! - It will flicker at boot up time.

Here is the code and a not so good drawing of the wiring :rofl:

int sensorInterrupt   = 0;  // interrupt    0
int sensorInterrupt2  = 1;  // interrupt    1
int sensorPin         = 2;  //Digital Pin   2
int sensorPin2        = 3;  //Digital Pin   3
int arm               = A0; //Analog  Pin   A0
int fyller            = A1; //Analog  Pin   A1
int gass              = A2; //Analog  Pin   A2
int ol                = 7;  //Digital Pin   7
int ol_2              = 8;  //Digital Pin   8
int trykk             = A3; //Analog  Pin   A3
int korker            = A4; //Digital Pin   A4
int olmengde          = 11; //Digital Pin   11
int sidearm           = A5; //Digital Pin   A5
int knapp             = 4;  //Digital Pin   4
int IRSensor          = 5;  // connect ir sensor to arduino pin 2
int transport         = 12; // conect Led to arduino pin 12

int teller            = 0;  //Teller antal looper

boolean state = HIGH;

float calibrationFactor = 100; //You can change according to your datasheet
float calibrationFactor2 = 100; //You can change according to your datasheet

volatile int pulseCount   =0;
volatile int pulseCount2  =0;   
volatile int mengde = 30;

float flowRate    = 0.0;
float flowRate2   = 0.0;

unsigned int flowMilliLitres  =0;
unsigned int flowMilliLitres2 =0;

unsigned long totalMilliLitres  = 0;
unsigned long totalMilliLitres2 = 0;
unsigned long oldTime   = 0;



void setup(){

  pinMode(sensorPin2,       INPUT_PULLUP);
  pinMode(sensorPin,        INPUT_PULLUP);
  pinMode(trykk,            OUTPUT);
  pinMode(knapp,            INPUT_PULLUP);
  pinMode(arm,              OUTPUT);
  pinMode(gass,             OUTPUT);
  pinMode(ol,               OUTPUT);
  pinMode(ol_2,             OUTPUT);
  pinMode(olmengde,         INPUT_PULLUP);
  pinMode(fyller,           OUTPUT);
  pinMode(sidearm,          OUTPUT);
  pinMode(korker,           OUTPUT);
  pinMode(IRSensor,         INPUT_PULLUP); // sensor pin INPUT
  pinMode(transport,        OUTPUT); // Led pin OUTPUT

  digitalWrite(trykk,       HIGH);
  digitalWrite(arm,         HIGH);
  digitalWrite(gass,        HIGH);
  digitalWrite(fyller,      HIGH);
  digitalWrite(sidearm,     HIGH);
  digitalWrite(korker,      HIGH); 
  digitalWrite(ol,          HIGH);
  digitalWrite(ol_2,        HIGH);
  digitalWrite(transport,   LOW );


  attachInterrupt(sensorInterrupt, pulseCounter, FALLING); 
  attachInterrupt(sensorInterrupt2, pulseCounter2, FALLING); 

  Serial.begin(9600);


}


void loop() {

    if(digitalRead(olmengde) == LOW){
  mengde = 50;
}
else{
  mengde = 30;
}

totalMilliLitres  = 0;
totalMilliLitres2 = 0;

if(!digitalRead(IRSensor) && state){
  teller++;
  state = LOW;
  Serial.print("flasker passert: ");
  Serial.println(teller);
  delay(100);
}

if (digitalRead(IRSensor)){
  state = HIGH;
  delay(50);
}

if(teller == 2){
 delay(1000);
    digitalWrite(transport, HIGH);
    Serial.println("transportbånd stopper");
    delay(1000);
    digitalWrite(arm, LOW);
    Serial.println("arm ut");
delay(3000);
   if(digitalRead(knapp) == LOW){
digitalWrite(arm, HIGH);
Serial.println("arm inn");
delay(2000);
digitalWrite(transport, LOW);    
Serial.println("transportbånd starter");    
teller = 0;   
return;
}
 else {}


    digitalWrite(fyller, LOW);
    Serial.println("fyller ned");
delay(2000);
    digitalWrite(gass, LOW);
    Serial.println("gass på");
delay(5000);
    digitalWrite(gass, HIGH);
    Serial.println("gass av");
delay(1000);
    digitalWrite(ol, LOW);
    digitalWrite(ol_2, LOW);
    Serial.println("øl 1 og øl 2 åpen");




while(totalMilliLitres < mengde || totalMilliLitres2 < mengde){

if((millis() - oldTime) > 1000){

flowRate =  ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
flowRate2 = ((1000.0 / (millis() - oldTime)) * pulseCount2) / calibrationFactor2;

oldTime = millis();

flowMilliLitres  = (flowRate  / 60) * 1000;
flowMilliLitres2 = (flowRate2 / 60) * 1000;

totalMilliLitres  += flowMilliLitres;
totalMilliLitres2 += flowMilliLitres2;

Serial.print("Output Liquid Quantity:  ");        
Serial.print(totalMilliLitres);
Serial.println("mL"); 

Serial.println(" ");

Serial.print("Output Liquid Quantity 2: ");        
Serial.print(totalMilliLitres2);
Serial.println("mL"); 


if (totalMilliLitres >= (mengde)){      
  digitalWrite(ol, HIGH);
  Serial.println("øl av ");
}
  else{}

 
if (totalMilliLitres2 >= (mengde)){      
  digitalWrite(ol_2, HIGH);
  Serial.println("øl 2 av ");
}
    else{}

pulseCount  = 0;
pulseCount2 = 0;

  }

}

delay(5000);
    digitalWrite(trykk, LOW);
    Serial.println("trykk åpen");
delay(5000);
    digitalWrite(fyller, HIGH);
    Serial.println("fyller opp");
delay(1000);
    digitalWrite(arm, HIGH);
    digitalWrite(trykk, HIGH);
    Serial.println("arm inn");
    Serial.println("trykk stengt");
delay(500);
    digitalWrite(sidearm, LOW);
    Serial.println("sidearm ut");
delay(2000);
    digitalWrite(sidearm, HIGH);
    digitalWrite(korker, LOW);
    Serial.println("sidearm inn");
    Serial.println("korker ned");
delay(3000);
    digitalWrite(korker, HIGH);
    Serial.println("korker opp");
delay(1000);
teller = 0;
digitalWrite(transport, LOW);
    Serial.println("transportbånd starter");



}

  else{}



 }
void pulseCounter()
{
  pulseCount++;
}
void pulseCounter2()
{
  pulseCount2++;
}

Which output pins are the relay inputs connected to?

Your diagram is fine.

Question:
Are there two outputs to your 12V power supply? If so are the common of each connected together.

Hi,
What is your 12V to 5V regulator?
Can you post specs/data please?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

There are three outputs on the power supply, all common are connected and all +12v are connected.

They are connected as on the drawing.
Some on analog pins A0-A5 and some on digital pins 7,8 and 12.

Here is a picture of the voltage regulator.

Another thing I find kind of odd. I have a prewired led diode for connecting to 12V, when I connect the ground wire of the diode to pin 11 of my Arduino, and the other wire to 12V it gives of a dim light. and that pin is set to input_pullup. it should not be ground or negative voltage on that pin shoul it??

Anyone with any ideas?

Hi,
What you are doing is connecting the anode to 12V and the cathode end to an input pin with it’s pull-up turned ON.

This means that the input pin is connected to 5V via a resistor inside the controller.
So you have a voltage difference of 12 - 5 == 7V with a high value series resistor.
So the LED glows dimly.

You would be better to connect the LED to 5V instead of 12V to protect the 5V tolerant pins.

Tom… :grinning: :+1: :coffee: :australia:

Ok, thank you. That answers that question :grinning:

But what about the main issue? Should I try a different power supply, or could it be a bad relay? Or something else?

In setup(), set the relay outputs HIGH first, THEN set them as OUTPUTS:

digitalWrite(relay1_pin,HIGH);
digitalWrite(relay2_pin,HIGH);
pinMode(relay1_pin,OUTPUT);
pinMode(relay2_pin,OUTPUT);

You must be connect the grounds (actually commons) of each 12V output together. Is there a reason you used two 12V outputs instead of one?

All the common outputs of the power supply are connected together.

You should use a separate power supply for the Arduino (not a buck regulator), but a supply that has offers isolation from the 12V supply. That way, you can take advantage of the opto isolation feature that your relay modules hopefully have. I’m unsure about the single relay … probably doesn’t have opto isolation. Anyways, if all your relays have opto isolators and you connect a separate isolated supply to the Arduino, then here’s the highlighted wires to remove (no GND connection from any of the relays to Arduino) …

image

Hi, again. Still some issues with my project. I was wondering if anyone could take a look at my code and see if there is anything that could cause a problem.

Now I have a problem with the relay conected to pin 7 and 8, sometimes it does what it is supposed to and sometimes not.

Hi,

You mean outputs

int ol                = 7;  //Digital Pin   7
int ol_2              = 8;  //Digital Pin   8

In your code you serial print when ol and ol_2 are operated.
Does the monitor tell you that they should be operating, or do you get no notification?

Can I also suggest you change your serial speed from 9600 to 115200baud.

Tom… :grinning: :+1: :coffee: :australia: