Interrupts interfrence

Hi everyone,

I am trying to control a 5v DC water pump while reading the total amount of liquid volume passed from a YF-S401 flow sensor (on digital pin 2). I am using a 2 channel relay card (i only use one channel on digital input 11.) to control the dc pump. I opto isolated the relay with an another power source, giving power to pump with an other power source and arduino is connected to computer with USB port.

While running the code while pump power is off everything works fine, relay opens and flow sensor creates interrupts if i blow in the flow sensor. But if pump is connected to power, even though the flow sensor propeller is still i get interrupts fast.

Real information is that if i disconnect the signal cable from flow meter i still get interrupts. But if i disconnect the cable from pin2 also interrupts stops. My thought was cable act as a antenna but when i used ferret ring i doesn't work.

Here is my code;

// Created by Emir Yasin Öztekin.

#include "EEPROM.h"

int measurement_val_1 = 0;  // OLCUM DEGERI
int measurement_val_2 = 0;  // OLCUM DEGERI
int col_p = 3; // KOLON NUMARASI
int row_p = 1; // SATIR NUMARASI
int yuzler = 1; // YUZLER BASAMAGI
int onlar = 0; //ONLAR BASAMAGI
int birler = 0; // BIRLER BASAMAGI
//int deger = yuzler * 100 + onlar * 10 + birler;
int nozul_deger = 1;
int dolum_adet = 0;

// Variables
volatile byte pulseCount_1 = 0;

// Variables for volume calculation
float totalMilliLiters_1;

// -----------Pın tanımları------
// Flow meter parameters
// Pin configuration
int flowSensorPin_1 = 2; // 1. flow metre sarı kablo arduino pin 2 ye bağla kırmızı 5v siyah gnd

// KALIBRASYON DEGERI {0} ADRESINE KAYITEDILIR
int const calibrationAddress {0};  // Where to store the calibration value.
float calibrationFactor {0.45};// SCALE FACTOR

#define RELAY_1 11 //Pompa_1 1 NUMARALI ROLE 11 NUMARALI ARDUNIO PININE BAGLI
#define PEDAL 13//PEDAL 13 NUMARI DIGITAL PINE BAGLI

//SETUP FONKSIYONU
void setup() {
  // ROLE BAGLANTILARI
  Serial.begin(9600);
  pinMode(flowSensorPin_1, INPUT_PULLUP);
  pinMode(RELAY_1, OUTPUT);   // çıkış olarak ayarlandı
  pinMode(PEDAL, OUTPUT); // PEDAL INPUT PINI DIGITAL 13
  digitalWrite(RELAY_1, HIGH);
}
void loop() {
  if (digitalRead(13) == 1) // PEDAL BASMA SINYALINI ALINCA KOSUL SAGLANIR.
  {
    fill_run();
  }
  
}

void fill_run(){
  attachInterrupt(digitalPinToInterrupt(flowSensorPin_1), pulseCounter_1, RISING);
  int deger = yuzler * 100 + onlar * 10 + birler;
  // Initialize variables
  pulseCount_1 = 0;
  digitalWrite(RELAY_1, LOW);
  while (measurement_val_1 < deger){
    print_measurement_1();
  }
  digitalWrite(RELAY_1, HIGH);
  dolum_adet += 1;
  measurement_val_1 = 0;
  totalMilliLiters_1 = 0;
}

void print_measurement_1(){
  // Update total volume
  totalMilliLiters_1 = (pulseCount_1 * calibrationFactor);
  measurement_val_1 = totalMilliLiters_1;
  Serial.println(pulseCount_1);
  Serial.println("-------------------");
  delay(100);
 }

void pulseCounter_1() {
  pulseCount_1++;
}

I forgot that i have already tried internal pull-up and also tried 10k ohm resistor as pull-up resistor.

Move this line into setup().

Also, interrupt is adding to pulseCount_1 but fill_run is resetting it to zero right away.

Doesn't matter. Already tried.

Yes i am aware of this. It is suppose to reset the count.

Problem is when i plug a cable on pin 2 even i dont connect it to signal cable of Flow sensor. And the other problem is dc pump. For the record if i Use a 220VAC pump with a SSR problem goes away.

I think you should illustrate your wiring, power supplies included.


This is the best i can do for right now.

Too many odds here. You should clean your wiring to start with. Remember, all gnd:s have to be connected together.