Go Down

Topic: Arduino with Flow Sensor and Relay (Read 200 times) previous topic - next topic


Mar 07, 2018, 04:53 pm Last Edit: Mar 07, 2018, 05:01 pm by esoberano
Hello. I'm needing help on a project with a water flow sensor. Basically, I'm trying to activate a relay (to turn on a motor) when the Arduino board senses any water flow present for 15 seconds or more. When I run the code below, the relay turns on after 15 seconds.

Here's my code:

byte sensorInterrupt = 0;  
byte statusLed    = 13;
byte sensorPin       = 2;

const int threshold = 15000;   // threshold

float calibrationFactor = 4.5;
volatile byte pulseCount;  
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
unsigned int countstart = 0;
unsigned long starttime;
unsigned long endtime;
unsigned long elapsedtime;

void setup()
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
  pinMode(7, OUTPUT);// connected to relay

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
 totalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

void loop()
   while (1){
        if((millis() - oldTime) > 1000)    // Only process counters once per second
          flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
          oldTime = millis();
          flowMilliLitres = (flowRate / 60) * 1000;
          totalMilliLitres += flowMilliLitres;
          unsigned int frac;
          Serial.print("Flow rate: ");
          Serial.print(int(flowRate));  // Print the integer part of the variable
          Serial.print("\t");       // Print tab space

          Serial.print("Output Liquid Quantity: ");        
          Serial.print("\t");       // Print tab space
          pulseCount = 0;
          attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
          if( totalMilliLitres > 0 && countstart == 0)
          starttime = millis();
          countstart = 1;

          if ( totalMilliLitres > 0 )
          endtime = millis();
          elapsedtime = endtime - starttime;
          if (totalMilliLitres >= 0&&elapsedtime >= threshold){
          digitalWrite(7,HIGH); // Activate relay to turn on motor
          digitalWrite(7,LOW); // Deactivate relay to turn on motor
          totalMilliLitres = 0;
          countstart = 0;
          elapsedtime = 0;
          digitalWrite(statusLed, LOW); // Turn the LED off
          digitalWrite(7, LOW);// turn relay OFF

void pulseCounter()
  // Increment the pulse counter

I would appreciate any help. Thank you!!!


When I run the code below, the relay turns on after 15 seconds.
What did you expect?

I would appreciate any help.
You didn't ask a question or describe a problem. If you have a problem it may help to see a wiring diagram of your setup.


I was expecting the relay to turn on after the flow sensor picked up water flow for 15 seconds. After I ran the code, I had water flowing through the the flow sensor for 5 seconds and shut off the water. The relay still turned on 10 seconds later. I didn't want this to happen. Instead, I would like to have the relay turn on only after there is water flow for at least 15 seconds.


How do I modify the code to have the relay turn on after the flow sensor senses water flow for 15 seconds or more?


How do I modify the code to have the relay turn on after the flow sensor senses water flow for 15 seconds or more?
Provide more information about your hardware. How do you define water flow for one second? If the counter is increased by at least one in one second?

The current code just looks for one single pulse from the flow sensor and the starts the 15 second timer. Depending on the hardware setup this single pulse might always be detected at startup.


You're exactly correct.

I'm using a Seeed Studio 314150005, 1/2" flow sensor that was purchased from robotshop.com and a KY-019, 5V One 1 Channel Relay.


Please post code between code tags, not as boldface. The forum tends to eat quite some formatting, especially things that look like smilies and so. Also do fix your indentation, makes it a lot more readable as well (ctrl-T in the IDE does this automatically).

Then the flow sensor is a pulse based one. When there's flow, it produces pulses, and the frequency gives you the flow.

So now you have a problem: how long will you wait for the next pulse before you decide "no flow"? There's a minimum flow speed that you need before you can even detect it.

Second problem is the software. You have to design it well.
- start timer when the flow is detected (i.e. you receive a pulse).
- stop the timer when it takes too long between two pulses.
- switch on the relay if the time from the first pulse is 15 seconds, and there is still flow, i.e. no pulse time out before that.
- allow for the signal to be "active" at all times as the rotor may stop in a point that it is exactly in the middle of where the hall effect sensor is activated. So you have to look for pulse edges rather than the input being high.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

When do you want the relay to turn OFF?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....


How do I modify the code to have the relay turn on after the flow sensor senses water flow for 15 seconds or more?
to use  Blink Without Delay, you have to have a few things.
a register that holds the time the switch was pressed.
a flag that stops looking to see if the switch was pressed
a flag to see if the switch is being held down
a register that changes after your time
a registoer of your desired time.

this first part just looks at the switch
NewSwitch =  flow sesnor or load from some other variable
if (oldSwitch doesnot equal NewSwitch)  test if the switch is not closed last time through the loop
    if (the switch is closed this time through the loop)
        ClosedTime = millis()

this bit has duration count the time since the switch was closed.
duration =  millis() - ClosedTime 

this will start the pump if the switch has been closed for 15 seconds or longer
if  ( duration >= 15 seconds )
    then PumpStart = HIGH

this loads the status of the switch and this goes just before you end the loop scan
after everything else you do.
oldSwitch =  NewSwitch
} //   =========== END OF LOOP ========

this is NOT code, but a general idea of the steps to get the pump to turn ON.
you have to write the actual code.

and, it does not turn the pump OFF.

Go Up