Probleme on first Project

hi everybody, i just started my journy into electronique and i have trouble with my circuit.

ok so to start, my project is a arduino bar, so basicaly a have 4 push botton that control 4 relay to control 4 pump, the probleme is that i am running into noise, when i press 1 button, randomly multiple relay whould active. so i look on the internet for solution, first i put flyback diode on each pump and it help but i was still having the same probleme, then i protected the arduino by seperating the alimentation for the relays with a buck converter , but it didnt change a thing. so this is where i am stuck right now. so that where i need help, what could i add or modify to save my project, i will give you all the electronique, code and a drawing i did of the circuit

PS: its a arduino mega

buck converter

4 relay board

12v alim

pump

426508624_1587074095439232_6298174811215278018_n

const int PB_1 = 2; // Button1
const int RLY_1 = 12; //relay 1
const int PB_2 = 3; // Button2
const int RLY_2 = 11; //relay 2
const int PB_3 = 4; // Button3
const int RLY_3 = 10; //relay 3
const int PB_4 = 5; // Button4
const int RLY_4 = 9; //relay 4



//VARIABLES
int buttonState1 = 0; 
    // current state of the button
bool relay1 = false;
int buttonState2 = 0; 
    // current state of the button
bool relay2 = false;
int buttonState3 = 0; 
    // current state of the button
bool relay3 = false;
int buttonState4 = 0; 
    // current state of the button
bool relay4 = false;

//MILLIS
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
const unsigned long interval1 = 2000;
const unsigned long interval2 = 3000;
const unsigned long interval3 = 4000;
const unsigned long interval4 = 5000;




void setup()
{
  pinMode(PB_1, INPUT);
  digitalWrite(PB_1, HIGH); // pull-up
  pinMode(PB_2, INPUT);
  digitalWrite(PB_2, HIGH); // pull-up
  pinMode(PB_3, INPUT);
  digitalWrite(PB_3, HIGH); // pull-up
  pinMode(PB_4, INPUT);
  digitalWrite(PB_4, HIGH); // pull-up

  pinMode(RLY_1, OUTPUT);
  digitalWrite(RLY_1, HIGH);
  pinMode(RLY_2, OUTPUT);
  digitalWrite(RLY_2, HIGH);
  pinMode(RLY_3, OUTPUT);
  digitalWrite(RLY_3, HIGH);
  pinMode(RLY_4, OUTPUT);
  digitalWrite(RLY_4, HIGH);
  Serial.begin(9600);
 }


void loop(){

    // read the pushbutton input pin:
    buttonState1 = digitalRead(PB_1);
    buttonState2 = digitalRead(PB_2);
    buttonState3 = digitalRead(PB_3);
    buttonState4 = digitalRead(PB_4);
    unsigned long currentMillis = millis();

    // if button is pressed, turn relay on (if it wasn't already on), and reset the timer
    if( buttonState1==HIGH ) // no need to check for previous state, in this specific case
    {
        previousMillis1 = currentMillis;
        digitalWrite(RLY_1, LOW);
        
        relay1 = true;
    }

     if( relay1 )
    {
        // turn red led on, if close to turning off the relay
        
        // if enough time has elapsed, turn of the relay
        if (currentMillis - previousMillis1 >= interval1) 
        {
            // .. turn of relay
            digitalWrite(RLY_1, HIGH);
            relay1 = false;
        }
    } 

if( buttonState2==HIGH ) // no need to check for previous state, in this specific case
    {
        previousMillis2 = currentMillis;
        digitalWrite(RLY_2, LOW);
        
        relay2 = true;
    }

     if( relay2 )
    {
        // turn red led on, if close to turning off the relay
        
        // if enough time has elapsed, turn of the relay
        if (currentMillis - previousMillis2 >= interval2) 
        {
            // .. turn of relay
            digitalWrite(RLY_2, HIGH);
            relay2 = false;
        }
    }

    if( buttonState3==HIGH ) // no need to check for previous state, in this specific case
    {
        previousMillis3 = currentMillis;
        digitalWrite(RLY_3, LOW);
        
        relay3 = true;
    }

     if( relay3 )
    {
        // turn red led on, if close to turning off the relay
        
        // if enough time has elapsed, turn of the relay
        if (currentMillis - previousMillis3 >= interval3) 
        {
            // .. turn of relay
            digitalWrite(RLY_3, HIGH);
            relay3 = false;
        }
    }
   
   if( buttonState4==HIGH ) // no need to check for previous state, in this specific case
    {
        previousMillis4 = currentMillis;
        digitalWrite(RLY_4, LOW);
        
        relay4 = true;
    }

     if( relay4 )
    {
        // turn red led on, if close to turning off the relay
        
        // if enough time has elapsed, turn of the relay
        if (currentMillis - previousMillis4 >= interval4) 
        {
            // .. turn of relay
            digitalWrite(RLY_4, HIGH);
            relay4 = false;
        }
    }

    } 

Could you take another picture?

Try to get the whiteboard perpendicular to the axis of the lens.

Try to get the image focused.

Use a high resolution.

The code looks fine, without the relays it looks like should be fine.

Be sure we can see the power source(s) and how power gets to all the parts that need power.

a7

buttonState1 = digitalRead(PB_1);
// if button is pressed, turn relay on (if it wasn't already on), and reset the timer
    if( buttonState1==HIGH )
pinMode(PB_1, INPUT);
  digitalWrite(PB_1, HIGH); // pull-up

It's unclear how you have wired your buttons, but with INPUT_PULLUP they would be switched to ground and read LOW when pressed.

yes i saw it was poor quality here you go it should be better

1 Like

i use this wirering because my botton only have 2 pin and not 4

Gikfun_normal_open_2_pin_press_switch_bb

1 Like

If you have a separate power supply for the Arduino, don't connect the buck regulator 5 volt output to the Arduino board - all you need is a common ground.

a7

so if i understand i should swap the place of the resistor and push botton to protect the arduino


Please confirm if you wired the relay module GND to Arduino GND

1 Like

Yes. @falconts the first thing to try next is to lose the lines that enable the internal pull-ups.

Sooner or later, you shoukd switch to pull-down configuration for the buttons, use you resistors wired to pull the input up, or lose thise too and use INPUT_PULLUP pin mode, the modern equivalent to turning them on with a digitalWrite statement.

You'd have to switch to looking for LOW to mean pressed, ppl sometimes

# define PRESST     LOW
# define NOTPRESST  HIGH

up top for one place to fix the sense of all buttons.

If there is any chance of noise or interference, I would say go ahead and use the real resistors; internal pull-up is considered weak. I'm old fashioned and in real projects always spend big on a few resistors for real pullup, usually 10K.

a7

ho yes i did i forgot to draw it

i dont know if i understand

my arduino is power by the original 9v arduino cable
i used the buck convertor because a was scared there was some noice comming from the coil of the relay, thats why i put a buck convertor, so that the noice stay in the power side of the circuit

Thank you for confirming that your Arduino has its own supply.

Therefor not only do you not need to, you def do not want to inject 5 volts from the buck converter on the Arduino 5 volt pin.

I don't know how to make it clearer.

Between your relay board and the Arduino, you need the logic lines and the common ground.

Do not connect the buck regulator 5 volt output except to the relay board it is powering.

Yes, perfect. By hooking up the wire I want you to lose, you defeat the purpose of doing that. Noise gets right over there...

a7

ok so if i understand, this is the way i should do it

Are you connecting the buttons to 3.3V?

You should connect them to 5V

The pins in drawing and the code do not match.

When you use Serial you should refrain from using pins 0 and 1

Yes, for the relays and the Arduino that is what and all you want.

a7

yes i did but if its a probleme i can switch it to 5v

i dont understand because i dont use pin 0 and 1

i am using 2,3,4 and 5 for the push botton and 9,10,11 and 12 for the relays

The first diagram looks like, well, the numbers are illegible. 1, 4, 3, 5?

In a later diagram, you use the relay numbers 1, 2, 3, 4 instead of the actual Arduino pins.

So you prolly have it wired correctly, but the schematic is somewhere between wrong and misleading.

On the Arduino side, the pin numbers should be used. On the relay board side, 1..4 should be used.

Penmanship counts, following conventions helps too. :expressionless:

We get there step by step.

a7