Strange relay behaviour

Hello,

I work on a lawn watering project. I use Attiny44 to control 7 relays that switch on and off the valves. I use one of these optocoupler relay modules. When I power the circuit it works fine for a few seconds (i.e. the relays switch on and off in defined sequence), but then all the relays start to switch on and off at the same time very quickly. The LEDs on the relay module do not even manage to flash at full power. I scaled down my program to this:

int Relay1 =  6;        
int Relay2 =  7;        
int Relay3 =  8;        

int Relay4 =  4;        
int Relay5 =  9;        
int Relay6 =  10;       
int Relay7 =  5;        

void setup() 
{   
  pinMode(Relay1, OUTPUT);  
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);  
  pinMode(Relay5, OUTPUT);
  pinMode(Relay6, OUTPUT); 
  pinMode(Relay7, OUTPUT); 
  
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  digitalWrite(Relay3, HIGH);
  digitalWrite(Relay4, HIGH);  
  digitalWrite(Relay5, HIGH);  
  digitalWrite(Relay6, HIGH);  
  digitalWrite(Relay7, HIGH); 
}

void loop()
{   
        digitalWrite(Relay1, LOW);    // relay is "on" when pin is LOW      
        delay(1000);
        digitalWrite(Relay1, HIGH);          
        
        digitalWrite(Relay2, LOW);     
        delay(1000);
        digitalWrite(Relay2, HIGH);     

        digitalWrite(Relay3, LOW);
        delay(1000);
        digitalWrite(Relay3, HIGH);
        
        digitalWrite(Relay4, LOW);
        delay(1000);
        digitalWrite(Relay4, HIGH);
        
        digitalWrite(Relay5, LOW);
        delay(1000);
        digitalWrite(Relay5, HIGH);
        
        digitalWrite(Relay6, LOW);
        delay(1000);
        digitalWrite(Relay6, HIGH);
        
        digitalWrite(Relay7, LOW);
        delay(1000);
        digitalWrite(Relay7, HIGH);
    delay(3000);
}

but the problem persists. It is also strange, that immediatelly after power on the 7 relays (not the 8th one that is not used) flash once (switch on and off) very quickly before entering into the loop.

Did anyone come accross this situation? What might be causing problem?
Thanks for any hint.

Could be a power supply that isn't sufficient.
We need to see your complete wiring schematic and a good photo of the actual wiring.

.

OK, resoldered the whole thing. It now works like a charm. Thanks for the hint!
However there is still very quick single flash on 7 relays immediatelly after power on. Any idea why is this happening?

        digitalWrite(Relay1, LOW);    // relay is "on" when pin is LOW      
        delay(1000);

Your code turns the relay "on" in setup() by making it LOW.

Add pullup resistors so relays are off while the Arduino comes out of reset, the bootloader does its thing, and the sketch starts up to define the IO as outputs and drives them high. 10K should do it.

Can I use internal pullup resistors for this purpose?

No, they don't kick in until your code starts setup().
You need them high before then.

gv445:
Can I use internal pullup resistors for this purpose?

NO.

What do you have wired to the input pins/power pins on the optocoupler relay module ?
So you think the arduino is outputting a ground signal during power up? How about after hitting the reset button?

Will try. Thanks for advice!

gv445:
Did anyone come across this situation? What might be causing problem?

Surprising to relate, the relays are "flashing" on on initialisation simply because you are commanding them to do so in your code. Why else would they do so?

Adding pull-ups is total nonsense! :roll_eyes: Just change the code to do what you actually want.

Change

void setup() 
{   
 pinMode(Relay1, OUTPUT);  
 pinMode(Relay2, OUTPUT);
 pinMode(Relay3, OUTPUT);
 pinMode(Relay4, OUTPUT);  
 pinMode(Relay5, OUTPUT);
 pinMode(Relay6, OUTPUT); 
 pinMode(Relay7, OUTPUT); 
 
 digitalWrite(Relay1, HIGH);
 digitalWrite(Relay2, HIGH);
 digitalWrite(Relay3, HIGH);
 digitalWrite(Relay4, HIGH);  
 digitalWrite(Relay5, HIGH);  
 digitalWrite(Relay6, HIGH);  
 digitalWrite(Relay7, HIGH); 
}

to

void setup() 
{   
 digitalWrite(Relay1, HIGH);
 digitalWrite(Relay2, HIGH);
 digitalWrite(Relay3, HIGH);
 digitalWrite(Relay4, HIGH);  
 digitalWrite(Relay5, HIGH);  
 digitalWrite(Relay6, HIGH);  
 digitalWrite(Relay7, HIGH); 

 pinMode(Relay1, OUTPUT);  
 pinMode(Relay2, OUTPUT);
 pinMode(Relay3, OUTPUT);
 pinMode(Relay4, OUTPUT);  
 pinMode(Relay5, OUTPUT);
 pinMode(Relay6, OUTPUT); 
 pinMode(Relay7, OUTPUT); 
}

As I understand it, you should call "pinMode(Relay1, OUTPUT); ",
before you call "digitalWrite(Relay1, HIGH);".

I must have been mistaken. It looks like you don't need pinMode() anymore.

It looks like what is the trouble, is before you run "digitalWrite(Relay1, HIGH);", the arduino output is tri-state, and the relay board does not know what to do. So, if that is the case, a 10k pullup resistor on each pin should take care of that problem.

Only if you wish the output to pull low (since at reset, the output register is cleared to zero) during the time between the two operations.

What do you have wired to the input pins/power pins on the optocoupler relay module ?

I wired Attiny pins straight to the optocoupler module respective "In" pins. The description of the module says that I can connect microcontroler pins straight.

So you think the arduino is outputting a ground signal during power up?

I use attiny44, not arduino. I actually power both attiny44 and the optocoupler module from the same power source. Therefore I expect ground is there for the optocoupler module.

Only if you wish the output to pull low

the relays are "flashing" on on initialisation simply because you are commanding them to do so

On this optocoupler module pulling low switches relay on and vice versa.

Add pullup resistors ...

Not sure how to wire them. Tried to wire pullup resistor as for input pin - put 10K resistor between attiny pin and Vcc. But it doesn't work. Maybe because wiring is somewhat different for an output pin?

gv445:
Not sure how to wire them. Tried to wire pullup resistor as for input pin - put 10K resistor between attiny pin and Vcc. But it doesn't work. Maybe because wiring is somewhat different for an output pin?

It sounds like you wired them correctly. You are actually concerned about the input pins having a pullup resistor, but, sounds like, in your case, that was the same.

gv445:
On this optocoupler module pulling low switches relay on and vice versa.

That is correct.

So if you use the incorrect initialisation sequence, you are briefly pulling the outputs LOW in setup() before setting them HIGH.

So have you now tried the corrected code I gave you? :roll_eyes:

Have you changed your script as Paul__B suggested in post #9 ?
That may well be the key.

Yes, I changed my script according to Paul__B as suggested in post #9, but the result is the same. Also tried removing the pinmode commands altogether. No help. All the relays still flash momentarily. Maybe I shall try lower than 10K pullup ?

You say "after power on", how about after hitting the reset button on the arduino?
Does power on mean powering up the relay board as well as the arduino?