Waterflow Help

Hello Everyone -

I’m at my wit’s end so I hope someone can help me.

I’m trying to implement a G1 Seedstudio waterflow sensor and use the MySensors radio/gateway model as my method of reporting to my home automation system (Vera).

The problem with MySensors, is it uses PIN2 for the radio so I need to find another way to count pulses from the SeedStudio sensor.

I have the basic sketch working that I found from the Seedstudio wiki. In place of the sensor, at the moment, I’m using a 10Hz function generator and have used a scope to make sure it’s accurate. With the attached sketch, I’m able to see 48L/min. However, this sketch uses PIN2 as the interrupt.

Can you help me figure out a better way to count the pulses?

Here’s the current code that I’m using to count the pulses successfully.


volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 3; //The pin location of the sensor

void rpm () //This is the function that the interupt calls
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
// The setup() method runs once, when the sketch starts
void setup() //
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan* 4.8); //(Pulse frequency x 60) / 5.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/min\r\n"); //Prints “L/hour” and returns a new line
Serial.print (NbTopsFan);
Serial.print (" NbTopsFan\r\n");

// Serial.print (NbTopsFan);

Also, for what it’s worth, here’s the sketch I’m using for the MySensor implementation…

// Use this sensor to measure volume and flow of your house watermeter.
// You need to set the correct pulsefactor of your meter (pulses per m3).
// The sensor starts by fetching current volume reading from gateway (VAR 1).
// Reports both volume and flow back to gateway.
// Unfortunately millis() won’t increment when the Arduino is in
// sleepmode. So we cannot make this sensor sleep if we also want
// to calculate/report flow.
// Sensor on pin 3

#include <Relay.h>
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>

#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
#define PULSE_FACTOR 480 // Nummber of blinks per m3 of your meter (One rotation/liter)
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
#define MAX_FLOW 400 // Max flow (l/min) value to report. This filetrs outliers.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child
unsigned long SEND_FREQUENCY = 20; // Minimum time between send (in seconds). We don’t want to spam the gateway.

Sensor gw;
Sleep sleep;

double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter

volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
volatile double flow = 0;
unsigned long oldPulseCount = 0;
unsigned long newBlink = 0;
double oldflow = 0;
double volume;
double oldvolume;
unsigned long lastSend;
unsigned long lastPulse;
unsigned long currentTime;
boolean metric;

void setup()

// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo(“Water Meter”, “1.0”);

// Register this device as Waterflow sensor
gw.sendSensorPresentation(CHILD_ID, S_WATER);

// Fetch last known pulse count value from gw
//pulseCount = oldPulseCount = atol(gw.getStatus(CHILD_ID, V_VAR1));
pulseCount = oldPulseCount = 0;

Serial.print(“Last pulse count from gw:”);
attachInterrupt(INTERRUPT, onPulse, RISING);
lastSend = millis();

void loop()
currentTime = millis();

// Only send values at a maximum frequency or woken up from sleep

if (SLEEP_MODE || currentTime - lastSend > 1000*SEND_FREQUENCY) {
// New flow value has been calculated
if (!SLEEP_MODE && flow != oldflow) {
// Check that we dont get unresonable large flow value.
// could hapen when long wraps or false interrupt triggered
if (flow<((unsigned long)MAX_FLOW)) {
gw.sendVariable(CHILD_ID, V_FLOW, flow, 2); // Send flow value to gw
oldflow = flow;

// No Pulse count in 2min 


if(currentTime - lastPulse > 120000){
	flow = 0;

// Pulse count has changed
if (pulseCount != oldPulseCount) {
  gw.sendVariable(CHILD_ID, V_VAR1, pulseCount);                  // Send  volumevalue to gw VAR1
  double volume = ((double)pulseCount/((double)PULSE_FACTOR));     
  oldPulseCount = pulseCount;
  Serial.print("Pulse count:");
  if (volume != oldvolume) {
    gw.sendVariable(CHILD_ID, V_VOLUME, volume, 3);               // Send volume value to gw
    Serial.println(volume, 3);
    oldvolume = volume;
lastSend = currentTime;


delay(300); //delay to allow serial to fully print before sleep
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SEND_FREQUENCY * 1000); //sleep for: sleepTime

void onPulse()
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
lastPulse = millis();
if (interval<500000L) {
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)

//flow = (60000000.0 /interval) / ppl;
flow = (60000000.0 /interval) * ppl;
lastBlink = newBlink;
  Serial.println(flow, 4);