Flow sensor using another brands code

Hi,

I'm very new to this. I've bought a flow sensor from the brand Gems. Can I use the same code that has been provided for other brands if they work in the same way?

if you were to provide a link to your "Gems" sensor and provide the code you have seen for the other sensors and a link to those too we could possibly answer your question...

1 Like

Thank you!

The flow sensor:

The code: https://www.instructables.com/How-to-Use-Water-Flow-Sensor-Arduino-Tutorial/

so your sensor has a pulsed output (you need to add a pullup resistor)

and you have a table that gives you information about the pulse per liter depending on the model

the code you linked to detects pulses too, you'll have to adjust the maths to transform the pulses you get into volume

1 Like

Remember that Hz is "cycles per second", so you need to count the pulses for one second and that will let you computer the flow for the current second.

1 Like

If the pulse frequency is 55Hz @ 1 liter per minute, then 1 liter of flow equals (55 * 60) 3300 pulses.

1 Like

Is this correct? I don't think I've taken into consideration the frequency?

int flowPin = 2;    //This is the input pin on the Arduino
double flowRate;    //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.

void setup() {
  pinMode(flowPin, INPUT);  //SET PIN2 as input
  attachInterrupt(0, Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
  Serial.bergin(9600);  //start serial
}

void loop() {
  // put your main code here, to run repeatedly:
   count = 0;      // Reset the counter so we start counting from 0 again
  interrupts();   //Enables interrupts on the Arduino
  delay (1000);   //Wait 1 second
  noInterrupts(); //Disable the interrupts on the Arduino

  //calculations
  flowRate = (count * 3.3);        //Take counted pulses in the last second and multiply by 3.3mL
  flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
  flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute
}

void Flow() {
  count++; //increnment the count by 1
}

Well you did when you do this

Because you measure in count the number of ticks during 1 second so count is actually your frequency in Hertz.

1 Like

You've got a mistake in this line:

 flowRate = (count * 3.3);        //Take counted pulses in the last second and multiply by 3.3mL

There are 3300 pulses per litre, or 3.3 pulses per ml.
Once you have counted pulses for one second, you need to divide by 3.3 to get the number of ml.

So you need to change that line to:

 flowRate = (count / 3.3);        //Take counted pulses in the last second and divide by 3.3 pulses per ml.

I only noticed the error, because I tested your code, using a function generator to simulate the pulses from the sensor, and found the results were out by approximately a factor of 10.

Also you might want to turn on the pullup resistor on pin 2:

  pinMode(flowPin, INPUT_PULLUP);  //SET PIN2 as input pullup
1 Like

The pullup resitor on my arduino is 20-50k Ohm. Would that be too much when I'm only supplying the sensor with 5V?

The internal pullup resistor will be fine. It saves you having to add a resistor externally.

1 Like

whilst a lower resistance value results in a stronger pull-up, providing better signal stability and noise immunity, you should probably be fine with the internal pull-up being activated.
If you have a scope you could check it out .


Is this the correct way to put the exteral resistor?

I see 7 wires leaving the picture. What are they connected to

Yes, that looks correct.

Have you tried it yet?

1 Like

No, I'm unsure if i can use it without water.

Try this modified code, you will have to add Serial.prints().

unsigned long timer, interval = 1000;
const byte flowPin = 2;    //This is the input pin on the Arduino
double flowRate;    //This is the value we intend to calculate.
volatile unsigned int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned int pps; // pulses per second


void setup() {
  pinMode(flowPin, INPUT);  //SET PIN2 as input
  attachInterrupt(0, Flow, FALLING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
  Serial.begin(9600);  //start serial
}

void loop() {
  // put your main code here, to run repeatedly:
  if(millis() - timer > interval){
     timer += interval;
    //count = 0;      // Reset the counter so we start counting from 0 again
     noInterrupts();   //Disable the interrupts on the Arduino
     pps = count;
     count = 0;
     interrupts(); //Enables interrupts on the Arduino

     //calculations
     flowRate = (pps / 3.3);        //Take counted pulses in the last second and multiply by 3.3mL
     flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
     flowRate = flowRate / 1000;      //Convert mL to Liters, giving you Liters / Minute
  }
}

void Flow() {
  ++count; //increnment the count by 1
}

Sorry, here's my corrected code after 3 edits, too many interrupts here. :grimacing:

unsigned long timer, interval = 1000;
const byte flowPin = 2;    //This is the input pin on the Arduino
double flowRate;    //This is the value we intend to calculate.
volatile unsigned int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned int pps; // pulses per second


void setup() {
  pinMode(flowPin, INPUT);  //SET PIN2 as input
  attachInterrupt(0, Flow, FALLING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
  Serial.begin(9600);  //start serial
}

void loop() {
  // put your main code here, to run repeatedly:
  if(millis() - timer > interval){
     timer += interval;
    //count = 0;      // Reset the counter so we start counting from 0 again
     noInterrupts();   //Disable the interrupts on the Arduino
     pps = count;
     count = 0;
     interrupts(); //Enables interrupts on the Arduino

     //calculations
     flowRate = (pps / 3.3);        //Take counted pulses in the last second and multiply by 3.3mL
     flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
     flowRate = flowRate / 1000;      //Convert mL to Liters, giving you Liters / Minute
  }
}

void Flow() {
  ++count; //increnment the count by 1
}
1 Like

What is this function generator you're using?