Problem with relay

Good morning, i'm building a machine and came across the necessit of using a incremental encoder because of a belth, i used the arduino uno to program, in a determined moment the arduino turn on a relay to open a compressed air valve, but sometimes the arduino restart, i think it's because the relay, so i add 2 power supplies, one for the arduino e one for the relay, to isolate both i'm using a transistor IRF3205 and in the relay coil i added a capacitor 104 and a diode 1N4733A, but even with that the arduino is restarting, what should i do?


This is basic the circuit, i didn't find all the exact components, so i used the avalible ones.

If you disconnect your arduino from the circuit, then run your code, does your code run without restarting the arduino?

If the arduino continues to reset without being connected to the circuit, copy and paste your code in this thread. Be sure to format the code and click the < CODE > button and paste the code where it says type or paste your code here

Yes, it run without restarting the arduino, even if i just disconnet the output of the relay that goes to the air valve, it works without a problem.

Maybe your circuit is pulling the arduino into "brownout" (is the digital pin sourcing more than it safely should) - verify your circuit is connected correctly and works as intended before reconnecting it to your arduino.

[edit]
I think the ground (or power) path of the relay coil should be interrupted by the IRF3205, which is controlled by the arduino. Your drawing looks like the coils are always connected to power, and the IRF3205 is trying to switch ground on the COM. This is the LUZ-5-R drawing
relay
.

Please post clear close up images of your wiring.

Translation - the schematic is a fiction. Please take a pen and paper, draw it as it really is, and post a photo of that. Include absolutely everything, power supplies too.

Hi, you need N-ch mosfet for the relay switch, and it`s a P-ch on the schematic, if its correct?

It's a fiction. Google the part number.

Here is the schematic

And this is the code that i'm using it

volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
const int pinrelay = 5;
double test = -300;
const int multp = -300;
long RunTime = 0;
unsigned long TimeNow;
unsigned long OnTime;
int relayOn = 0;

void setup() {
  Serial.begin (9600);

  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 
  
  pinMode(3, INPUT_PULLUP); // internal pullup input pin 3
  
  pinMode(pinrelay, OUTPUT); // output pin rele
  
   //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
   
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);
  digitalWrite(pinrelay, LOW);
  Serial.println (test);
  }
   
  void loop() { 
  if( counter != temp ){
    temp = counter;
    RunTime = millis();
    if (counter <= test){
      Serial.print ("Counter: ");
      Serial.println (counter);
      digitalWrite(pinrelay, HIGH);
      OnTime = millis();
      Serial.println ("Relay On");
      test = test+multp;
      Serial.print ("Test: ");
      Serial.println (test);
      relayOn = 1;
    }
  }
  
  TimeNow = millis();
  
  if( relayOn == 1 && TimeNow - OnTime >= 800 ) { //turn off the relay if on for 0,8s
    digitalWrite(pinrelay, LOW);
    Serial.println ("Relay Off");
    relayOn = 0;
  }
  
  if (TimeNow - RunTime > 20000) { //If the encoder stop for 20s, reset the counter
    RunTime = TimeNow;
    counter = 1;
    Serial.print ("Restart Counter: ");
    Serial.println (counter);
    test = multp;
  }
  }
   
  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  }else{
  counter--;
  }
  }
   
  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  counter--;
  }else{
  counter++;
  }
  }

What is powering the Arduino?

Why do you have a flyback diode on a relay module? Those should already have one on board. It's time to post images of your actual hardware.

Sorry about the long time to a response, but i'm powering through the usb port. I changed the schematic a little, just to change the relay module to just the relay.

This is the new schematic

This is the actual hardware


The red and black wires from the arduino are the VCC and GND from the encoder, the brown and white are the D2 and D3 from the encoder.
The white wire coming from the right of the image is from the other power supplie of 5V.

Now this is the output without the air valve being connected to the relay, it works fine.


w

And this is with the air valve connected, the arduino is restarting sometimes.

If the trouble happens only with the relay contact side connected, try putting a snubber circuit on the valve.

What is powering the valve?