Problem with relays

Hi!

I need some help for my project.
I would turn on 8 e27 light connected to the sector successively (one turn on, turn off, another turn on...).
So, I have 8 relay to controll lighting of the lamps. When bulb are not powered, relays work fine, but when I plug it to the sector, relays do whatever they wants and act randomly. Same behavior with the relays powered by arduino or by separate power supply.
Have you some ideas to help me a little bit?
Thanks :slight_smile:

Zalc

Have you some ideas to help me a little bit?

Post a schematic and the code that you are using. Post the specs or a data sheet for the relays and the e27(?) lamp. What does "sector" mean in this context? What power supplies did you try (volts and current capability) and how are they connected to the circuit?

Read the how to use this forum-please read sticky to see how to, properly, post code and some advice on how to ask a good question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Thanks for the answer!
I haven't found a data sheet for the relays on internet, but I have a picture (if it can help):


here a quick schematic of the relays :

And finally here is the code :

#define PIN_DS 8   //pin 14  75HC595    
#define PIN_STCP 9  //pin 12  75HC595
#define PIN_SHCP 10 //pin 11  75HC595
 
int opto = 13;
int pinBouton = 12;
  //définition des modes

//Nombre de registre a decalage utilise
//How many shift register
#define numberOf74hc595 1 
 
// nombre total de pin de registre a decalage
// number of total register pin
#define numOfRegisterPins numberOf74hc595 * 8
 
// cree un array contenant des booleen de la taille du nombre de pin 74hc595
// Create an array with boolean, size of pin of 74hc595
boolean registers[numOfRegisterPins];
 
void setup(){
  pinMode(PIN_DS, OUTPUT);
  pinMode(PIN_STCP, OUTPUT);
  pinMode(PIN_SHCP, OUTPUT);
  pinMode (pinBouton, INPUT); //mode lecture pour le bouton
 
  pinMode(opto, OUTPUT);
 
 // remise a 0 des registres
 //reset all register pins
  clearRegisters();
 // on applique les valeurs au registre a decalage
 // write value on shift register
  writeRegisters();
}               
 
// Mettre toutes les valeurs a 0 pour le resigtre
//set all register pins to LOW
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
} 
 
// affectation des valeurs engregistrés dans le tableau "registers" et application (visualisation) des valeurs a la fin
// set value recorded in array "registers" and display on the end
void writeRegisters(){
 
 // Tant que LOW les modifications ne seront pas affectés
 // Until LOW modification will not be apply
  digitalWrite(PIN_STCP, LOW);
 
 // boucle pour affecter chaque pin des 74hc595
 // loop for aplly all value for each pin 74hc595
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
 
    //doit etre a l'etat bas pour changer de colonne plus tard
    //need to be low for change column soon
    digitalWrite(PIN_SHCP, LOW);
 
    // recuperation de la valeur dans le tableau registers
    // catch value insinde array registers
    int val = registers[i];
 
    //affecte la valeur sur le pin DS correspondant a un pin du 74hc595
    //apply the value to a pin of 74hc595
    digitalWrite(PIN_DS, val);
    //colonne suivante
    // next column
    digitalWrite(PIN_SHCP, HIGH);
 
  }
  //applique toutes les valeurs au 74hc595
  // apply value to all pin of 74hc595
  digitalWrite(PIN_STCP, HIGH);
 
}
 
// enregistre une valeur pour un registre etat haut ou bas 
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

void loop()
{
    //lecture de l'Ă©tat du bouton et stockage dans etatBouton
  boolean etatBouton = digitalRead(pinBouton);
  //test des conditions
 if (etatBouton==HIGH)//test si bouton levé
  {  



  }
  
  if (etatBouton==LOW)//test si bouton appuyé
  {
 

 
//void loop(){


  setRegisterPin(0, HIGH);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
// appelle la fonction pour appliquer les valeurs
// call function to apply value
  writeRegisters();  

delay(1000);  
digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second
  
  
  
 

 
  setRegisterPin(0, LOW);
  setRegisterPin(1, HIGH);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
  writeRegisters();
delay(1000);
  
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second
 
 
 

 
  setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, HIGH);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
  writeRegisters();

delay(1000);
  
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

  setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, HIGH);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
  writeRegisters();

delay(1000);
 
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

 
  setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, HIGH);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
  writeRegisters();
delay(1000);
  
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

    setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, HIGH);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
  writeRegisters();

delay(1000);
    
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

  
    setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, HIGH);
  setRegisterPin(7, LOW);
  writeRegisters();

delay(1000);  
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

  
    setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, HIGH);
  writeRegisters();

delay(1000);
  
 digitalWrite(opto, HIGH);
delay(250);               // wait for a second
digitalWrite(opto, LOW);
delay(250);               // wait for a second

     setRegisterPin(0, LOW);
  setRegisterPin(1, LOW);
  setRegisterPin(2, LOW);
  setRegisterPin(3, LOW);
  setRegisterPin(4, LOW);
  setRegisterPin(5, LOW);
  setRegisterPin(6, LOW);
  setRegisterPin(7, LOW);
writeRegisters();


  
  }
 
 }

Mains switching transients are clearly reseting the Arduino - perhaps the mains wiring is too close to the low-voltage stuff, or the power supply to the Arduino isn't good quality?