💬 Water Meter Pulse Sensor

  • Admin

    This thread contains comments for the article "Water Meter Pulse Sensor" posted on MySensors.org.

  • I get error on compiling.
    "exit status 1
    call of overloaded 'set(volatile long unsigned int&)' is ambiguous"

    The following line gets red marked in Arduino IDE
    send(lastCounterMsg.set(pulseCount)); // Send pulsecount value to gw in VAR1

  • Admin

    Compiles fine here in 1.6.12..

  • Running 1.6.11, will try and update.

  • Should have said that I was trying to run it on a Node MCU 0.9. I can compile fine for Arduino Nano but not for Node MCU. Also I got another error now when I upgraded to 1.6.12.

    In file included from C:\Users\xxxxx\Documents\Arduino\libraries\MySensors-development/MySensors.h:337:0,
                     from C:\Users\xxxxxxx\AppData\Local\Temp\untitled921979828.tmp\sketch_oct14a\sketch_oct14a.ino:44:
    C:\Users\xxxxxxx\Documents\Arduino\libraries\MySensors-development/core/MyMainESP8266.cpp:4:22: fatal error: Schedule.h: No such file or directory
     #include "Schedule.h"
    compilation terminated.
    exit status 1
    Error compiling for board NodeMCU 0.9 (ESP-12 Module).```

  • Admin

    Please update NodeMCU in board manager to rc3.

    Hmm .. I can see the "overloaded 'set(volatile long unsigned int&)' is ambiguous" error here as well... I wonder why it behaves differently from Arduino...

  • Admin

    Works if you replace

    volatile unsigned long pulseCount = 0;


    volatile uint16_t pulseCount = 0;

  • Great! Thanks for the help 🙂
    Now I just need to figure out a good placement for it and get values to Domoticz.
    But this helped me a lot to get started.

  • It is not clear to me from the picture if the water meter pulse output leads go to gnd and VCC or VCC and DO? The water-meter pulse output is only two leads.

    I am going to MySensors with great hope on this water meter sensor. I have the Vera gateway working fine. Now the test with my first and most important sensor!

    Thanks in advance

  • Mod

    @vikram0228 I haven't built this node, but the pictures and the connections table on the page describes three leads. Do you have a different sensor?

  • Water Meters with pulse output have two leads. They generate a simple pulse.

    The MySensors circuit also should also work with two leads - just need to know which two. The three leads are for water meters with no pulse output and where you need an optical sensor to sense the moving wheel.

  • Hi.
    Cant get my watersensor to send any values.
    I have debug enabled and it seems like it communicates fine with the gw but no flow data are beeing reported to the gw or the serial monitor.
    TSP:MSG:SEND 4-4-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=ok:
    Anyone else experienced this problem?

  • I wanted to operate my water pulse meter on batteries and also get the water flow. The original design had the following issues with that:

    • Incorrect flow calc: micros() was used to calculate the flow, however micros() wraps every 70 minutes which looks like a huge flow (which is then discarded in code)
    • Volume calc: millis() wraps every 50 days which is not handled correctly either
    • Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
    • Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time. We now simply calculate the number of pulses per minute and deduct the flow
    • I also had issued with the data transport reliability, so I added error counters (which show up on the Gateway as distance sensors)
    • I also wanted to provide a measurement counter to the gateway (that counts up each time a message is sent)
    • The sensor will reboot itself when too many errors occur

    So I modified the circuit of the IR sensor:

    • Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
    • We will wake up every 500 millisecond to turn on the IR LED connected to PIN 8. Pin 8 also powers the photo transistor that measures the reflection
    • I removed the power from the opamp circuit that is linked to the photo transistor
    • The voltage from the photo transistor is then read using an analog read on A1. Based on a threshold value we will deduct if the mirror on the water meter is in view
    • Pin 7 is connected to a learning switch which will turn the device in a specific mode and the min/max values on A1 are used to calculate the value of the threshold (which is then stored in the EEPROM)
    • After 30 seconds in learning mode, the new threshold is established and the LED on Pin 6 will show the actual on/off mirror signals, so you can see the pulses are correctly counted
    • switch back the DIP switch on Pin 7 to bring back normal mode
    • The circuit also contains the battery voltage sensor circuit (I am using a 1.5V battery and step up circuit). So the resistors used are 470k from + pole of battery to the A0 input and 1 M ohm from A0 to ground
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik Ekblad
     * Version 1.1 - GizMoCuz
     * Version 1.2 - changed BM: using low power separate circuit for infra red on pin 8 + analog A1
     * Incorrect flow calc: micros() was used to calculate the flow, however micros() is wraps every 70 minutes which looks like a huge flow (which is discarded)
     * Volume calc: millis() wraps every 50 days which is not handled correctly
     * Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
     * Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time
     * Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
     * We will wake up every second to turn on the IR LED (connected to PIN 8). Pin 8 also powers the photo transistor that measures the reflection
     * The voltage from the photo transistor is then read using an analog read on A1. Based on a treshold value we will deduct if the mirror is in view
     * Pin 7 is connected to a learning switch which will turn the device in continous mode and the min/max values on A1 are used to recalc the treshold
     * during a 30 second period. After this period the new treshold is established and the LED on Pin 6 will show the actual on/off mirror signals
     * http://www.mysensors.org/build/pulse_water
    // BOARD: PRO MINI 3.3V/ 8Mhz ATMEGA328 8Mhz
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 10                 // hard code the node number
    #include <SPI.h>
    #include <MySensors.h>  
    #define SENSOR_POWER 8                // pin that will provide power to IR LED + sense circuit
    #define IR_SENSE_PIN  A1              // input for IR voltage
    #define BATTERY_SENSE_PIN  A0         // select the input pin for the battery sense point
    #define LEARN_SWITCH_PIN 7            // switch (SW1 on battery module) to turn on learning mode (low==on)
    #define LEARN_LED_PIN 6               // LED feedback during learning mode (LED on battery module)
    #define LEARN_TIME 30                 // number of seconds we will keep learn loop
    #define PULSE_FACTOR 1000             // Nummber of blinks per m3 of your meter (One rotation/1 liter)
    #define MAX_FLOW 80                   // Max flow (l/min) value to report. This filters outliers.
    #define CHILD_ID 1                    // Id of the sensor child (contains 3 subs: V_FLOW, V_VOLUME, VAR1)
    #define CHILD_PINGID 2                // ID of ping counter
    #define CHILD_ERRID 3                 // ID of error counter
    #define CHECK_FREQUENCY 500           // time in milliseconds between loop (where we check the sensor) - 500ms   
    #define MIN_SEND_FREQ 60              // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (30 seconds)
    #define MAX_SEND_FREQ 1200            // Maximum time between send (in multiplies of CHECK_FREQUENCY). We need to show we are alive (600 sec/10 min)
    #define IR_ON_SETTLE 2                // number of milliseconds after we turned on the IR LED and we assume the receive signal is stable (in ms)
    #define EE_TRESHOLD 10                // config addresses 0 + 1 used for treshold from learning (loadState() returns only uint8 value)
    #define TRESHOLD_MARGIN 3             // additional margin before we actually see a one or zero
    #define RESETMIN 5                    // number of cycle times (either 30 sec of 10 min) we consistently need to have transmission errors before we perform hard reset
    MyMessage volumeMsg(CHILD_ID,V_VOLUME); // display volume and flow on the same CHILD_ID
    MyMessage flowMsg(CHILD_ID,V_FLOW); // flow
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    MyMessage pingMsg(CHILD_PINGID,V_DISTANCE); // use distance to keep track of changing value
    MyMessage errMsg(CHILD_ERRID,V_DISTANCE); // use distance to keep track of changing value
    double ppl = ((double)PULSE_FACTOR / 1000.0);    // Pulses per liter
    unsigned int oldBatteryPcnt = 0;          // check if changed
    unsigned int minsendcnt = MIN_SEND_FREQ;  // counter for keeping minimum intervals between sending
    unsigned int maxsendcnt = MAX_SEND_FREQ;  // counter for keeping maximum intervals between sending 
    unsigned int treshold = 512;              // threshold value when to swap on/off for pulse
    unsigned long pulseCount = 0;             // total volume of this pulse meter (value stored/received on gateway on pcReceived)
    unsigned long oldPulseCount = 0;          // to see if we have received something
    boolean pcReceived = false;               // received volume from prior reboot
    boolean onoff = false;                    // sensor value above/below treshold 
    unsigned int intervalcnt = 0;             // number of cycles between last period (for flow calculation)
    double flow = 0;                          // maintain flow
    double oldflow = 0;                       // keep prior flow (only send on change)
    unsigned int learntime=LEARN_TIME*2;      // timer for learning period
    unsigned int learnlow = 1023;             // lowest value found during learning
    unsigned int learnhigh = 0;               // highest value found during learning
    boolean learnsaved = false;               // have saved learned value
    unsigned long pingcnt = 0;
    unsigned long errcnt = 0;                 // error count
    unsigned int errcnt2 = 0;                 // error counter set to 0 when sending is ok
    void(* resetFunc) (void) = 0;//declare reset function at address 0 (for rebooting the Arduino)
    void setup() {    
      // make sure a few vars have the right init value after software reboot
      pingcnt = 0;
      pcReceived = false;
      pulseCount = oldPulseCount = 0;
      // setup hardware
      pinMode(SENSOR_POWER, OUTPUT); 
      digitalWrite(SENSOR_POWER, LOW);
      pinMode(LEARN_LED_PIN, INPUT);      // default is input because this pin also has SW2 of battery block
      // Fetch last known pulse count value from gateway
      request(CHILD_ID, V_VAR1);
      // Fetch threshold value from EE prom
      treshold = readEeprom(EE_TRESHOLD);
      if (treshold<30 || treshold>1000) treshold = 512;   // wrong value in EEprom, take default
      Serial.print("Treshold: ");
      // use the 1.1 V internal reference for the battery and IR sensor
    #if defined(__AVR_ATmega2560__)
      analogRead(IR_SENSE_PIN); // settle analogreference value
      wait(CHECK_FREQUENCY); // wait a bit
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Water Meter", "1.2");
      // Register this device as Waterflow sensor
      present(CHILD_ID, S_WATER);      
      present(CHILD_PINGID, S_DISTANCE); 
      present(CHILD_ERRID, S_DISTANCE);
    void loop() {
      if (digitalRead(LEARN_SWITCH_PIN)==LOW) {
        pinMode(LEARN_LED_PIN, OUTPUT);
        digitalWrite(SENSOR_POWER, HIGH);
        intervalcnt = 0;
      } else {
        learnlow = 1023;
        learnhigh = 0;
        pinMode(LEARN_LED_PIN, INPUT);
    void learn_loop() {
      // will run into this loop as long as we are learning
      unsigned int sensorValue = analogRead(IR_SENSE_PIN);
      Serial.print("IR: ");
      if (learntime>0) {
        // still learning
        learnsaved = false;    
        digitalWrite(LEARN_LED_PIN, !digitalRead(LEARN_LED_PIN));  // blink led
        if (sensorValue < learnlow) {
          learnlow = sensorValue;
          Serial.println(" Lowest");
        } else if (sensorValue > learnhigh) {
          learnhigh = sensorValue;
          Serial.println(" Highest");
        } else Serial.println();
      } else {
        if (!learnsaved) {
          treshold = (learnhigh + learnlow)/2;
          Serial.print("Treshold: ");
          storeEeprom(EE_TRESHOLD, treshold);
        learnsaved = true;
        // just display using LED
        digitalWrite(LEARN_LED_PIN, sensorValue>treshold);
        Serial.println((sensorValue>treshold ? " on" : " off"));
    void normal_loop() { 
      unsigned long start_loop = millis();    // to allow adjusting wait time
      // we start doing a measurement
      digitalWrite(SENSOR_POWER, HIGH);
      unsigned int sensorValue = analogRead(IR_SENSE_PIN);
      digitalWrite(SENSOR_POWER, LOW); 
      #ifdef MY_DEBUG_DETAIL
      Serial.print("IR: ");
      boolean nowvalue = onoff;
      if (onoff && (sensorValue<treshold-TRESHOLD_MARGIN)) nowvalue = false;
      if (!onoff && (sensorValue>treshold+TRESHOLD_MARGIN)) nowvalue = true;
      if (nowvalue != onoff) {
        // we have a pulse, only count on upwards pulse
        onoff = nowvalue;
        if (onoff) {
          #ifdef MY_DEBUG
          Serial.print("p: ");
    // Only send values at a maximum frequency or woken up from sleep
      if (minsendcnt>0) minsendcnt--;
      if (maxsendcnt>0) maxsendcnt--;
      // send minimum interval when we have pulse changes or if we had some flow the prior time or send on timeout
      if ((minsendcnt==0 && (pulseCount != oldPulseCount)) || (minsendcnt==0 && oldflow != 0) || maxsendcnt==0) {
        if (!pcReceived) {   //Last Pulsecount not yet received from controller, request it again
          Serial.print("Re-request var1 ..");
          request(CHILD_ID, V_VAR1);
    // Prevent flooding the gateway with re-requests,,, wait at least 1000 ms for gateway (cannot be sleep or smartSleep
        minsendcnt = MIN_SEND_FREQ;
        maxsendcnt = MAX_SEND_FREQ;
        sensorValue = analogRead(BATTERY_SENSE_PIN);
        int batteryPcnt = sensorValue / 10;
        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/1e6)*1.1 = Vmax = 1.67 Volts
        // 1.67/1023 = Volts per bit = 0.00158065
        Serial.print("Battery %: ");
        if (oldBatteryPcnt != batteryPcnt) {
          oldBatteryPcnt = batteryPcnt;
        double volume = ((double)pulseCount/((double)PULSE_FACTOR));      
        flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;  // flow in liter/min
        #ifdef MY_DEBUG
        Serial.println(volume, 3);
        bool b = send(lastCounterMsg.set(pulseCount));  // Send  pulsecount value to gw in VAR1
        if (b) errcnt2=0; else { errcnt++; errcnt2++; }
        b = send(volumeMsg.set(volume, 3));               // Send volume (set function 2nd argument is resolution)
        if (b) errcnt2=0; else { errcnt++; errcnt2++; }
        b = send(flowMsg.set(flow, 2));                   // Send flow value to gw
        if (b) errcnt2=0; else { errcnt++; errcnt2++; }
        b = send(pingMsg.set(pingcnt));                   // ensure at least this var has a different value
        if (b) errcnt2=0; else { errcnt++; errcnt2++; }
        b = send(errMsg.set(errcnt2+((float) errcnt2/100),2));    // ensure we always send error count
        if (b) errcnt2=0; else { errcnt++; errcnt2++; }
        oldPulseCount = pulseCount;
        intervalcnt = 0;
        oldflow = flow; 
        if (errcnt2>= (5*RESETMIN)) {
          resetFunc(); //call reset to reboot the Arduino
    // calculate how long it took to process all of this. then go to sleep for the remaining period
      unsigned long end_loop = millis();
      if (end_loop - start_loop < CHECK_FREQUENCY)
        sleep(CHECK_FREQUENCY - (end_loop > start_loop ? end_loop - start_loop : 0));
    void receive(const MyMessage &message) {
      if (message.type==V_VAR1) {
        unsigned long gwPulseCount=message.getULong();
        pulseCount += gwPulseCount;
        oldPulseCount += gwPulseCount;
        Serial.print("Received last pulse count from gw:");
        pcReceived = true;
    void storeEeprom(int pos, int value) {
        // function for saving the values to the internal EEPROM
        // value = the value to be stored (as int)
        // pos = the first byte position to store the value in
        // only two bytes can be stored with this function (max 32.767)
        saveState(pos, ((unsigned int)value >> 8 ));
        saveState(pos, (value & 0xff));
    int readEeprom(int pos) {
        // function for reading the values from the internal EEPROM
        // pos = the first byte position to read the value from 
        int hiByte;
        int loByte;
        hiByte = loadState(pos) << 8;
        loByte = loadState(pos);
        return (hiByte | loByte);

  • Mod

    Great work @bart59 !
    Do you have any indication on how long battery life you will get with this setup?

  • @mfalkvidd - I did measure the average current consumption at the time, but I not remember the exact value. I believe it was well below the 0.5 mA. The sensor has been up and running on the same single 1.5V AA battery for 30 days now and the batt percentage still shows 93%.

  • I hope this is not off topic but I'm trying to read our gas meter - but....I cannot use a photo sensor or a hall sensor or a reed switch (the meter is outside and there is no magnet in it). I was able to get some good data using a magnetometer (https://www.sparkfun.com/tutorials/301) using an arduino. But I tink from what I am told is the arduino isn't big enough to handle the code I need to use to change the data (x axis, y axis, z axis) into a pulse but will work on a pi.

    My question is am I missing something (hardware guy, not software)? Can I use a Pi for a sensor (it has the inputs and room), all the code on this site is for arduinos. Could I have the Pi output a pulse to an arduino? Am I over thinking this?

  • @dpcreel. I would be surprised the Arduino could not handle the HMC5883L magnetometer from Sparkfun. The I2C protocol is natively supported and you only need to analyse numbers coming from the 3-Axis to decide when the wheels inside the gas meter turn. I think the position of the sensor will be very critical, but the code should not be too complex. Arduino's are typically limited in handling much data (there is only 2000 bytes of RAM in the ATmega328), but your code should not need much RAM space. The program size can be 32 KB which should be enough.

    If you want to go for the PI, there are I2C libraries out there you could use and I would then bypass the Mysensors gateway alltogether and connect an ethernet cable to the PI and use the MQTT protocol to talk to your home controller.


  • @bart59 The Arduino works fine with the HMC5883L, I got some good data from it and was able to "see" the gas meter movements very well. I am not able to write code (hardware guy) to change this data to a pulse for MySensors or whatever it needs. I just want to be able to read my gas meter with the HMC5883L. I did find some code in python that works on the pi, that's why I mentioned it. I'm a hack at software.

  • Hi
    I am trying to use the subject code with pulse water meters and hope someone can help to figure out what needs to be adjusted to get more accurate data.

    My setup:
    Gateway ESP8266 (NodeMCU) + NRF24 -> Connected to Domoticz
    Sensor node: Arduino Uno + NRF24
    Default codes from Build. Test sensors so far work great.

    Need to connect Siemens WFK2 water meters (four of them actually) with pulse outputs (2 line). According to data sheet (link text) there are two types: reed output or NAMUR.

    To start with, I connected the reed output of the water meter via 10k resistor to +5V, GND and D3. Again, according to data sheet, for every 10l the water meter should give an impulse (e.g. connect the switch). Taking this into account I have adjusted pulse factor to 100 and based on Nominal Flow Rate (Max Flow Rate impossible in my case) limited Max Flow to 25 l/min. This setup works, but I definitely get wrong flow values (e.g. with constant flow of 6l/min serial monitor and domoticz report anything between 12-15l/min) and also wrong water usage m3 and litre usage in Domoticz.

    When trying to debug, found out following behaviour (must be connected to Pulse length an Qn from data sheet IMHO):
    with each turn it indeed switches on, e.g. shortens the contacts which generates pulse for the sensor, however it takes up to 2-4 litres until switch is in the off state.

    As a result you might have a situation when meter switches on, one closes the tap, and the signal is on for minutes/hours until tap is again opened and 2-4 litres have been used, after which signal will switch off.

    Do I understand correctly that the debouncer which should take care of similar situations is not ready for this?

    thanks in advance for support

  • Hi Aram

    The original code from the mysensors site does not handle your situation very well (indeed because the switch staying on or off for a long time). You can use my code (see my post further up this discussion). In my case I have a pulse every 1 liter.

    In your case you only have 1 pulse every 10 liters, which means you have to take a much longer period to calculate the flow correctly. Basically you have to set MIN_SEND_FREQ to a higher value. The flow is calculated based on the number of pulses in a given time period. Example: with 6 l/min you have to calculate this value only once every 5 minutes (=30 liter = 3 pulses from your Siemens meter) instead of every 30 seconds as I do. So if MIN_SEND_FREQ = 600 (every 5 minutes) your flow is calculated as:

    flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;

    In the example above (pulseCount-oldPulseCount) = 3 pulses
    ppl = 0.1
    intervalcnt = MIN_SEND_FREQ = 600
    ==> flow is 6 l/min



  • bart59,

    thanks for quick reply. I will try to adjust MIN_SEND_FREQ and use your code.
    however, if I understand correctly, with the current logic used its impossible to avoid misinterpretations of pulses in case of very very long on or off state. It will approximate the flow rate to more correct value during usage of water (this is good enough) and should report total usage somehow correctly (this one I would better get as precise as possible). I wonder if the code will capture the correct number of pulses in the case of very long on state.

    BTW, I was not able to find a readily available sketch to connect NAMUR output, which is basically 5kOhm off state and 1.5795 kOhm on state. I believe, I will have to calculate required pull up resistance to get a proper voltage divider for 5\3.3v, right?

  • My code basically measures the time between two upward pulses. You can modify the code to also count the downward pulses. The net effect is that you will get a count every 5 liter (on average), but if there is no flow, the first pulse will always be off by 1-4 liter because you do not know how far the rotation is completed.

    On NAMUR: you can actually use my code here too: I use analog input A1 to measure the voltage on the infra red sensor (which varies between 0 and 1.1 Volt). During the learn mode (set with a seaprate DIP switch) the code measures the input voltage for a period of 30 seconds while you turn open the water tap (you may want to increase the timing in your case) and then calculates the average between the lowest and highest voltage as the currect point there is a 1 or 0 coming from the pump (in my case it is an IR LED that is reflecting from a mirror into a photo sensor and the position of the mirror may change - resulting in different voltages).


  • Would anyone be able to help me get the hall sensor to work to work directly connected to an ESP8266 MQTT gateway? I was able to created the MQTT gateway, appended this sketch to the MQTT gateway sketch and connect the hall sensor DO to D12 (D3 is occupied), but when I subscribe to my mosquitto server no values are published. I am able to see the prefixes and they get published every 20 seconds as expected, but there are no sensor values. I plan on using this with home assistant, incase that helps with the final setup. If anyone has any recommendations it would be greatly appreciated.

  • Can you please roughly say

    1. what is the current consumption overall
    2. what is the current consumption when measuring a pulse
    3. what is the current consumed when the data is transmitted

  • Mod

    @jagadesh-waran welcome to the MySensors community.

    The current consumption depends on which Arduino you are using and which radio you are using.

    Could you describe what your goal is?

    Maybe the page on battery power can be useful.

  • @mfalkvidd im using mini at 8mhz with a 3V battery, No LDO and an ESP8266

    I want to calculate the battery life say im using 3V 19000mah battery and if im consuming 100 gallons per day

    Could you please update me the life of the battery?

    Could you please update me the current consumptions at various intervals?

  • Mod

    @jagadesh-waran The easiest ways is to either measure the consumption on your devices, or look at the datasheets.

  • I am trying this node. I have it set up. but I am having the same problem as Dirtbag. I am not getting any values. I debugged and I am getting 1 and 0 for my sensor output, so my sensor is working when I trigger it with a magnet.
    Does this work with MQTT gateway?? am I missing something. Communication is fine between by ESP gateway and my node.?
    Thanks ahead of time for any help with this.

  • Hi. i'm having a strange problem using a arduino nano connecting a hall sensor to read my water meter. My water meter is dated from 1996 and i can't use the TCRT5000 sensor so i'm trying my luck with the hall sensor. My problem is that my arduino is always sending data.... even if i have the water turned off the most strange thing is that even when the arduino is not connected to the hall sensor it send's data. How can i fix this problem ?

  • Mod

    @mrc-core did you read this part of the instructions?

    You can also set the frequency that the sensor will report the water consumption by updating the SEND_FREQUENCY. The default frequency 3 times per minute (every 20 seconds).

  • @mfalkvidd Yes i did but has you can see even when the hall sensor is not connected the arduino sends data and the water increases in the the gateway. I can work on the SEND_FREQUENCY and see if it fix this problem.

    Thnaks for the replay

  • Have been working with the code and have found one strange thing. When my sensor sends data to the gateway "domoticz" the gateway sends a value to the sensor. The strange thing is that my arduino does not have any sensor connected and sow the data send is zero but my gateway sends akways a positive value and increases this value every time it sends data to the arduino.

    Even if i go to the arduino code and set the volume and pulseCount to 0 flash it to the arduino, load the value of zero to the gateway and then e flash the same code but this time i remove the volume and pulseCount to 0 the first time the arduino sends data to the gateway it always receive a value insted of zero since i dont have the hall sensor connected to the arduino.

    I'm using a arduino mini pro 3.3v on a easy pcb by soundberg81 and i have setup the digital pin2 to connect the hall sensor.

    Here's some log from my arduino:

    3 TSM:INIT
    4 TSF:WUR:MS=0
    13 TSF:SID:OK,ID=1
    14 TSM:FPAR
    51 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    1032 TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    1037 TSF:MSG:FPAR OK,ID=0,D=1
    2058 TSM:FPAR:OK
    2059 TSM:ID
    2060 TSM:ID:OK
    2062 TSM:UPL
    2065 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2172 TSF:MSG:READ,0-0-1,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2181 TSM:UPL:OK
    2182 TSM:READY:ID=1,PAR=0,DIS=1
    2187 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2320 TSF:MSG:READ,0-0-1,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2327 TSF:MSG:SEND,1-1-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2335 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    3517 TSF:MSG:READ,0-0-1,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    3526 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=11,pt=0,l=11,sg=0,ft=0,st=OK:Water Meter
    3535 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=12,pt=0,l=5,sg=0,ft=0,st=OK:2.4.B
    3544 TSF:MSG:SEND,1-1-0-0,s=1,c=0,t=21,pt=0,l=11,sg=0,ft=0,st=OK:Hall Sensor
    3551 MCO:REG:REQ
    3589 !TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=NACK:2
    5596 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=1,st=OK:2
    5665 TSF:MSG:READ,0-0-1,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    5670 MCO:PIM:NODE REG=1
    5673 MCO:BGN:STP
    5677 TSF:MSG:SEND,1-1-0-0,s=1,c=2,t=25,pt=0,l=0,sg=0,ft=0,st=OK:
    5683 MCO:BGN:INIT OK,TSP=1
    6484 TSF:MSG:READ,0-0-1,s=1,c=2,t=25,pt=0,l=1,sg=0:0
    Received last pulse count from gw:2
    35686 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:2
    35694 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.003
    65687 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:2.05
    65695 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:4
    65704 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.004
    95688 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:0.00
    95696 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:5
    95706 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.005
    125689 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:5
    125698 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.006
    155691 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:2.00
    155699 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:7
    155708 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.007
    185691 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:0.00
    185699 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:8
    185708 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.008
    215696 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:8
    215707 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.009
    245693 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:2.00
    245701 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:10
    245710 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.010
    275694 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:0.00
    275702 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:11
    275714 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.011
    305695 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:11
    305703 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.012
    335698 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:2.00
    335707 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=OK:13
    335718 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=OK:0.013
    365732 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=OK:0.00
    365776 !TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=0,st=NACK:14
    365788 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=1,st=OK:0.014
    376929 TSF:MSG:READ,0-0-255,s=255,c=3,t=20,pt=0,l=0,sg=0:
    376934 TSF:MSG:BC
    377041 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=21,pt=1,l=1,sg=0,ft=0,st=OK:0
    395735 !TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=NACK:5.36

    Again since i'm geting this problem i don't have my sensor connected to the arduino.

  • Mod

    @mrc-core if nothing is connected to the sensor pin, the pin will float and might create spurious interrupts. Connect the pin to gnd to make sure it doesn't get any false triggers.

  • @mfalkvidd Thanks. Have now connected the digital pin2 to gnd and in the last 1h the values are always 0.000 going to leave it this way until night since now i'm at work. I would like to find out what was creating the spurious interrupts??
    Other thing that ill find out tonight is when i connect the sensor to the arduino will it create rong data or will it only send real data only when the water meter starts running water...

    One more thing today at 8:00 when i did the pin2 to gnd the first "boot" over domoticz i got this value as you can see on the image below. Is this normal? Have been trying to deleted but it wont go away.

    Once again thanks for your help.

    0_1497430047720_Water data.png

  • @mrc-core
    The spurious interrups are caused by the open line. The Arduino has high impedance inputs and anything from a WiFi signal or the signal from a close by circuit can cause the value to swing between 0 and 3 Volt, triggering your input. See https://www.arduino.cc/en/Tutorial/DigitalPins

    On your high value in the graph: Domoticz water sensors require the sensor to post the total water volume. This means that the sensor needs to "know" the total of volumes from all the past measurements. In fact all the (wrong) past pulses you generated with the open input are stored by the sensor. There are 2 ways this storage mechanism can be realized in Arduino code:

    (1) keep the value in the Arduino EEPROM and
    (2) use a build in feature of Domoticz to store values on behalf of sensors.

    Mysesnsors uses method 2 as method (1) has 2 disadvantages: The code needs to continuously write to the EEPROM, because you never know when the Arduino will be rebooted. The EEPROM on the Arduino chip does not allow you to write to it that often (max 100,000 times or so) and the EEPROM is cleared when you update the software. The sketch used by MySensors uses method (2) by sending the pulsecount to the gateway using send(lastCounterMsg.set(pulseCount)) statement at the same time the volume is sent to Domoticz with send(volumeMsg.set(volume, 3)); At each reboot the sensor requests the value of VAR1 from the gateway using request(CHILD_ID, V_VAR1); The sensor does not send any data to the gateway until the gateway has received the counter past value. The receive() function is called by the Mysensor library when data comes back from the gateway. There the pulseCount += gwPulseCount statement adds the value to the pulseCount.

    This means the sensor will not be able to "forget" the pulses that came from the period your digital input was not connected.

    There are 3 ways to fix this problem:

    • you can delete your sensor at the Domoticz side (throwing away all history): First power down your Arduino sensor, the go into Domoticz screen and remove the sensor from the Utility screen and then remove the sensor from the Setup->Hardware->MysensorsGW->Setup->Click on your water meter-> delete all Children-> delete the water meter sensor itself. Then power up your Arduino and you will see the sensor show up again.
    • as above: remove your sensor from the Utilities tab and change the code to give your sensor another new ID. At the top of your code you can put a statement like
    #define MY_NODE_ID 10

    This will be the ID number that shows in the hardware setup screen in Domoticz for your sensor. If you omit this code MySensors will create a new (unique) code for you. However in my system I prefer to allocate the numbers myself to each sensor. So a quick way to create a brand new sensor history in Domoticz is to simply change the MY_NODE_ID number (change from 10 to 11).

    • change yuor Arduino code to force a 0 in the VAR1 holding variable for your sensor. You can do that by adding a statement send(lastCounterMsg.set(0)); inside your loop() function (best place is here):
    // Only send values at a maximum frequency or woken up from sleep
       if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
          send(lastCounterMsg.set(0));  // <--- keep here shortly and remove later
           if (!pcReceived) {
               //Last Pulsecount not yet received from controller, request it again
               request(CHILD_ID, V_VAR1);

    You will compile and upload this code to your Arduino and let it run for a minute or so, then remove the line and upload the orginal code. You should now be able to remove the data point in Domoticz (by using ctrl-mouseclick on the graphic item)

    Hope this helps


  • @bart59 Thanks have done like you said and did clean the data from the gateway.
    All day until 1h ago i had left my arduino connected on my pc and i did like mfalkvidd said connecting the digital pin 2 to gnd and my arduino did not send any strange value to the gateway always seend volume 0.00 this was perfect.
    Now that i'm at home i had connect my arduino to the sensor 3.3v gnd and digital pin2 and in the first log there it was value increase on volume when my sensor was in front off me...

    I have a arduino nano pro 3.3v on easy pcb board and i'm using the hall sensor i dont see why i'm getting this problem...
    I have a WiFi AP inside home and my sensor is outside.

    Even the water flow says i have a 2.00 L/min when the sensor is not near the water meter.

  • New update.... have change the arduino mini pro for another one flash a new code a new sensor and again i'm getting volume data when the sensor is not near the water meter. I dont understand what's happening.

  • @mrc-core Just to make sure: are you connecting/disconnecting the data pin to your sensor when the Arduino is powered off and are you sure the connection is solid? If you connect things with power on you may accidently trigger a pulse a 100 times.... which will be sent to the gateway 20 secs later. Always power off stuff first and use a solid connection (soldering is always better) when making modifications. How did you power the hall sensor? Should be from the 3.3V VCC connector of the Arduino and not from the higher voltage or something separate. Can you upload a photo of your setup to allow us to see how you connect things?


  • Mod

    In addition to what bart59 said, make sure the hall sensor isn't near anything magnetic. It will be triggered by any magnetic material, not just the water meter.

  • @bart59 Yes i'm using the 3,3v from the arduino and i always connect everything with the arduino power off. Give me mor 5 minutes and i'll upload a photo from my sensor going to bring it inside home.
    @mfalkvidd i think i don't have anything magnetic on the place were i'm putting the arduino and the sensor at this time.

    Thanks for the replays.

  • Ok here's the photo off my sensor. On my code i'm using the digital pin2 since digital pin3 has a resistor for Dallas and dht sensors but i'm not using them at this time. This arduino is only for water meter.


  • I think i have fix my problem. I have been playing with the code and the sensor and notice that the power led and data led on the sensor were always on no mater the digital pin i was trying to use.
    This also happened with the analog pin A0.
    In one last attempt i decided to use the digital pin 3 with the resistor soldered on the board for sensors like dallas and dht11 or dht22.

    Now the first thing i had noticed was the data led was not on and now it only comes on when i passe a magnet under my sensor... it does not count any pulsecount if i passe the magnet over the sensor only under it.

    Counted the times i had passed the magnet under the sensor and my gateway received te exact number 30 times = 0.030 volume and now the volume is stop at this number no more ghost encrease numbers.

    Tomorrow morning i'm going to mount the sensor over the water meter to see if this finaly works.

    The only problem i'm getting now is some NACK over the transmissions... like this:
    6604747 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=3,st=OK:30
    604794 !TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=NACK:0.030
    634781 !TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=25,pt=5,l=4,sg=0,ft=1,st=NACK:30
    634828 !TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=35,pt=7,l=5,sg=0,ft=2,st=NACK:0.030

  • @mrc-core The NACK may be bad transmission. I typically connect a 5 cm piece of wire to the antenna of the send/receive module to increase the reception of data

  • @bart59 and @mfalkvidd Thanks for all the help. Today i toke the sensor to the water meter and for my luck my water meter is too old 1997 and no mater the position i put the hall sensor it just doesn't get any pulse at all 😞 at this time i really don't now what to use to get the data from my water meter...
    I have already tried also the TCRT5000 and also does not work. Here's an image from my water meter
    0_1497529305996_Sem Título.png

    The TCRT5000 when i put over the glass the green led stays always on but when i remove it from the glass and point it to the black part of my meter the green led goes off the sensor is working ok but the glass from my meter does not help at all.

    Any sugestions off other sensors i can try or use ?

  • @mrc-core You are right. This is the wrong type of water meter. I have been using 2 other brands watermeters: the Honeywell C7195x2001B (see http://ecc.emea.honeywell.com/downloads/EN2R9029.PDF) and the Caleffi series 316 (http://www.arbo.it/images/techsheets/316405.pdf). You local central heating installation guy may have them as they are used in boiler systems of central heating for the warm water supply. They are also sold on eBay. Both work more or less the same and can be operated on 5V. The number of pulses per liter are much higher (something like 500 pulses per liter), so they are more accurate. You will need to modify your code to work with this. Below the code of my sensor (which also measures multiple temperatures at the same Arduino chip):

     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * (c) 2017 Bart M - last edits: 15 May 2017
     * Note: using timer0 to generate a second interrupt for our 1 ms counters. timer0 is still also  used for delay()
     * Functions WTW Flow and Temperature
     * WTW flow
     *  - Flow of shower cold water
     *  - Flow of boiler cold water entry
     * Temperature 
     * - Temperature of outgoing water (Dallas with cable)
     * - Temperature of shower head (Dallas)
     * Flow meter using Honeywell C7195A2001B which has a hall sensor that pulses at 7 Hz per liter/min
     * we count only upgoing pulses, so in our case we get: 
     *   7 pulses per second at 1 l/min
     *   420 pulses per liter
     *   420000 pulses per m3
     * Flow meter Caleffi 316 which has a hall sensor that pulses at 8.8 Hz per liter/min (according to specs)
     * In reality the frequency is about 30% lower in my case, so we have to correct this with a factor of 1.3.
     *   8.8 pulses per second at 1 l/min
     *   528 pulses per liter
     *   528000 pulses per m3
     *   528000/1.3 = 406154
     * If we use 250 m3 per year we come to 105 x 10^6 pulses/year
     *`this is stored in an unsigned long that can hold 4295 x 10^6 -> volume overflow after 41 years
     *****************************************************************************************************  */
    // BOARD: PRO MINI 5V V/ 16Mhz ATMEGA328 16Mhz
    // type of flow meter
    #define CALEFFI
    // #define HONEYWELL
    // Enable debug prints to serial monitor
    #define MY_DEBUG 1
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    // No repeater 
    // node ID's
    #define MY_NODE_ID 27             // start naming my own nodes at number (set in comments if you want automatic)
    #define FLOW_ID 1                 // flow meter 1 (start here if more)
    #define NFLOWS 2                  // number of flow meters  
    #define TEMP_ID 3                 // Temperature (start here if there are more linked)
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DallasTemperature.h>
    #include <OneWire.h>
    // PIN connections where the flow meters are connected. We have 2 flow meters, so our array has 2 entries
    uint8_t FlowPins[NFLOWS] = {2, 3};
    #define TEMP_PIN 4                // temp sensor connected to pin4
    #ifdef CALEFFI
    // #define PULSE_FACTOR 528000        // Number of blinks per m3 of your meter Caleffi (from data sheet)
    #define PULSE_FACTOR 406154        // Nummber of blinks per m3 of your meter Caleffi (measured in my installation)
    #define PULSE_FACTOR 420000        // Nummber of blinks per m3 of your meter Honeywell
    // configs
    #define MAXFLOWERROR 50             // maximum number of l/min that we accept
    // delay times
    #define CHECK_FREQUENCY 1000         // time in milliseconds between loop (where we check the sensor) - 1000ms
    #define MIN_SEND_FREQ 10             // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (10 seconds)
    #define MIN_FLOW_SEND_FREQ 20        // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (30 seconds)
    #define MAX_SEND_FREQ 600            // Maximum time between send (in multiplies of CHECK_FREQUENCY). We need to show we are alive (600 sec/10 min)
    // one wire config
    #define MAX_ATTACHED_DS18B20 8
    // Motion message types
    MyMessage volumeMsg(FLOW_ID,V_VOLUME);
    MyMessage flowMsg(FLOW_ID,V_FLOW);
    MyMessage lastCounterMsg(FLOW_ID,V_VAR1);
    MyMessage tempmsg(TEMP_ID, V_TEMP);
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    volatile unsigned int numSensors = 0;
    double ppl = ((double)PULSE_FACTOR / 1000.0);    // Pulses per liter
    boolean pcReceived[NFLOWS];                      // received volume from prior reboot
    double oldflow[NFLOWS];                          // keep prior flow (only send on change)
    unsigned long oldflow_cnt[NFLOWS];               // only send when changed
    // updated in ISR
    volatile unsigned int mcnt = CHECK_FREQUENCY;   // decremented in ISR at 1000Hz. Cycles at one per second to update send counters
    volatile unsigned int tempsendcnt[MAX_ATTACHED_DS18B20];  
    volatile unsigned int maxtempsendcnt[MAX_ATTACHED_DS18B20];
    volatile unsigned int flowsendcnt[NFLOWS];
    volatile unsigned int maxflowsendcnt[NFLOWS];
    volatile bool flow_status[NFLOWS];              // status of flow (on or off cycle)
    volatile unsigned long flow_cnt[NFLOWS];        // counter volume for each flow sensor
    volatile unsigned int intervalcnt[NFLOWS];      // keep track of number of milliseconds since we had previous flow info
    void before() { 
      for (uint8_t i=0; i<NFLOWS; i++) {
        pinMode(FlowPins[i], INPUT);
      // Startup up the OneWire library
      for (uint8_t i=0; i<NFLOWS; i++) {
        oldflow[i] = 0;
        flow_status[i] = false;
        flow_cnt[i] = 0;
        oldflow_cnt[i] = 0;
        pcReceived[i] = false;
        flowsendcnt[i] = MIN_FLOW_SEND_FREQ;
        maxflowsendcnt[i] = MAX_SEND_FREQ;
        intervalcnt[i] = 0;
      for (uint8_t i=0; i<MAX_ATTACHED_DS18B20; i++) { 
         tempsendcnt[i] = 0;
         maxtempsendcnt[i] = MAX_SEND_FREQ;
    void setup() {
       // Timer0 is already used for millis() - we'll just interrupt somewhere
       // in the middle and call the TIMER0_COMPA_vect interrupt
       OCR0A = 0xAF;
       TIMSK0 |= _BV(OCIE0A);
       // Fetch last known pulse count value from gw
       for (uint8_t i=0; i<NFLOWS; i++) {
         request(FLOW_ID+i, V_VAR1);
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("WTW flow sensor", "1.2");
      // Register all sensors to gw (they will be created as child devices)
      numSensors = sensors.getDeviceCount();
      Serial.print("# temp sensors: ");
      DeviceAddress add;
      for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { 
         present(TEMP_ID+i, S_TEMP);
         sensors.getAddress(add, i);
      for (uint8_t i=0; i<NFLOWS; i++) {
        present(FLOW_ID+i, S_WATER);
    // function to print a device address of a Dallas temp sensor
    void printAddress(DeviceAddress deviceAddress) {
      for (uint8_t i = 0; i < 8; i++) {
        // zero pad the address if necessary
        if (deviceAddress[i] < 16) Serial.print("0");
        Serial.print(deviceAddress[i], HEX);
    void loop() {
      // we come here every 1000 ms (defined in CHECK_FREQUENCY)
      // now handle temperature
      if (numSensors>0) {
        for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
          // Fetch and round temperature to one decimal
          float temperature = static_cast<float>(static_cast<int> (sensors.getTempCByIndex(i) * 10.)) / 10.;
          // Only send data if temperature has changed and no error
          if (((lastTemperature[i] != temperature && tempsendcnt[i]==0) || maxtempsendcnt[i]==0) && temperature != -127.00 && temperature != 85.00) {
            // Send in the new temperature
            // Save new temperature for next compare
            tempsendcnt[i] = MIN_SEND_FREQ;
            maxtempsendcnt[i] = MAX_SEND_FREQ;
        for (uint8_t i=0; i<NFLOWS; i++) {
           if ((flowsendcnt[i]==0 && (oldflow_cnt[i] != flow_cnt[i])) || (flowsendcnt[i]==0 && oldflow[i] != 0) || maxflowsendcnt[i]==0) {
              if (!pcReceived[i]) {   //Last Pulsecount not yet received from controller, request it again
                 Serial.print("Re-request var1 for sensor ");
                 request(FLOW_ID+i, V_VAR1);
                 wait(2*CHECK_FREQUENCY); // wait at least 1000 ms for gateway (cannot be sleep or smartSleep)
              flowsendcnt[i] = MIN_FLOW_SEND_FREQ;
              maxflowsendcnt[i] = MAX_SEND_FREQ;
              double volume = ((double)flow_cnt[i]/((double)PULSE_FACTOR));      
              double flow = (((double) (flow_cnt[i]-oldflow_cnt[i])) * ((double) 60000.0 / ((double) intervalcnt[i]))) / ppl;  // flow in liter/min
              Serial.print("Flow meter:");
              Serial.println(volume, 3);
              intervalcnt[i] = 0;
              oldflow[i] = flow; 
              oldflow_cnt[i] = flow_cnt[i];
              send(lastCounterMsg.setSensor(FLOW_ID+i).set(flow_cnt[i]));     // Send  pulsecount value to gw in VAR1
              send(volumeMsg.setSensor(FLOW_ID+i).set(volume, 3));            // Send volume (set function 2nd argument is resolution)
              if (flow<MAXFLOWERROR) send(flowMsg.setSensor(FLOW_ID+i).set(flow, 2));                // Send flow value to gw
      // Serial.print(end_loop-start_loop);
    // Receive data from gateway
    void receive(const MyMessage &message) {
      for (uint8_t i=0; i<NFLOWS; i++) {
         if (message.type==V_VAR1 && message.sensor==FLOW_ID+i) {
            unsigned long gwPulseCount=message.getULong();
            flow_cnt[i] += gwPulseCount;
            oldflow_cnt[i] += gwPulseCount;
            oldflow[i] = 0;
            Serial.print("Received last pulse count for ");
            Serial.print(" from gw:");
            pcReceived[i] = true;
    // Interrupt on timer0 - called as part of timer0 - already running at 1ms intervals
      if (mcnt>0) mcnt--;
      for (uint8_t i=0; i<NFLOWS; i++) {
         if (mcnt==0) {
            if (flowsendcnt[i]>0) flowsendcnt[i]--;
            if (maxflowsendcnt[i]>0) maxflowsendcnt[i]--;
         bool val = digitalRead(FlowPins[i]);
         if (val != flow_status[i]) {
            flow_status[i] = val;
            if (!val) flow_cnt[i]++;      // we increment counter on down flank
      if (mcnt==0) {
         mcnt = CHECK_FREQUENCY;
         for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { 
            if (tempsendcnt[i]>0) tempsendcnt[i]--;
            if (maxtempsendcnt[i]>0) maxtempsendcnt[i]--;

    Good luck


  • @mrc-core If I do a search for "B93 315.04 lisboa" I get MSV Janz company turn up, but I don't understand Portugese. It is probably a magnetic drive even at 1997 fabrication, but perhaps some searching will yield clues or contact the manufacturer?

  • @zboblamont I have already read that pdf there they say this:

    "The design of the Magnetic Transmission reduces the number of components
    Operating in the water, increasing the reliability of the meter."

    They also say this in the pdf:

    "Any of these models can be supplied ready to receive the
    Pulse transmission (see tele-count catalog). Ex .: MSV 1520t"

    In this last information i made a search for MSV 1520t were i found again another pdf that talks about my water meter.
    Here's the link: http://resopre.pt/conteudo.php?fam=CONTADORES,CONT_DOMESTICOS&pag=AGUA_RESOPRE.PT&detail=693

    You can see 5 water meters on that link all off them say's Magnetic Transmission "Transmissão magnética" but has you can see none off them look like mine. Has i can understand mine is the more basic off them all i do belive that i have the first generation off this water meters.

    I have done one last test yesterday leaving a water tap open and passed the hall sensor all over the water meter from side to side and top to bottom and never got any impulse digital led light turning on.
    The led did turned on when i put my finger in the top off the sensor or in cables near the pins of the sensor.

  • @mrc-core Well, I did suggest contacting the manufacturer with the information on your meter, they are better able to advise or may even sell a sensor head to retrofit. Usually these manufacturers are very helpful, and at least you speak the language.
    I know that some manufacturers insist on factory building the sensor arrangement inside the meter (Zenner?) but you may be lucky.
    The magnetic drive is more to avoid mechanical connection between the metering section and the water flow, so no axle or sealing ring to leak !
    I have a plastic Elster, it has the same magnetic coupling as yours, but I was unable to sense any changing magnetic field when I checked it, but I had already ordered the specific read head for it so was not unduly concerned, and it should arrive in next few days. This sensor pulses when a small metal arc on the small dial passes beneath the sensor every 1 litre. It was around 30 euro from memory, so not a mad price.

  • Hello,
    i have got water meter with pulse output. So I have just arduino and RFM69W. Sleep mode false/true works well with standard bootloader. Optiboot with sleep mode false works also well. But I would like to run it on battery optiboot with true sleep mode which doens work correctly. Sensor doesnt receiceve last pulse count from gw.
    Any idea how to fix it?

    4 TSM:INIT
    4 TSF:WUR:MS=0
    12 TSF:SID:OK,ID=3
    14 TSM:FPAR
    145 TSF:MSG:SEND,3-3-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    743 TSF:MSG:READ,0-0-3,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    747 TSF:MSG:FPAR OK,ID=0,D=1
    2152 TSM:FPAR:OK
    2152 TSM:ID
    2154 TSM:ID:OK
    2156 TSM:UPL
    2164 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2179 TSF:MSG:READ,0-0-3,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2189 TSM:UPL:OK
    2191 TSM:READY:ID=3,PAR=0,DIS=1
    2201 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2254 TSF:MSG:READ,0-0-3,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2269 TSF:MSG:SEND,3-3-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2283 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2336 TSF:MSG:READ,0-0-3,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2351 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=11,pt=0,l=11,sg=0,ft=0,st=OK:Water Meter
    2367 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    2381 TSF:MSG:SEND,3-3-0-0,s=3,c=0,t=21,pt=0,l=0,sg=0,ft=0,st=OK:
    2387 MCO:REG:REQ
    2398 TSF:MSG:SEND,3-3-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2451 TSF:MSG:READ,0-0-3,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2457 MCO:PIM:NODE REG=1
    2461 MCO:BGN:STP
    2469 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2476 MCO:BGN:INIT OK,TSP=1

    2486 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2500 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2514 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2527 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2541 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2555 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2570 TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    2707 !TSF:MSG:SEND,3-3-0-0,s=3,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=NACK:

  • Has it ever received a pulse count?

  • When its about 2 metres far from gateway its received. Afterthat can be moved 10 metres away and it works. But after reset has to be moved close to gateway again.
    I run also power meter pulse sensor. This has got different issue. It falls asleep before receiving pulse count from gw.

  • @ladislav Sounds like a radio problem. Are you using a capacitor on the radio power supply pins? A capacitor on all radios in your network. I have a an Ethernet gateway in our basement and a node in our attic that is two floors above it and it works fine using a NRF24 for all radios and the radio power is on the default setting and it communicates fine.

  • I having problem to place the tcrt5000 on the correct place. Can anyone please show where to put it? Is it over the black (that spins every 0.5 l)?

    I have this type of water meter alt text

  • @smilvert I am afraid your water meter does not work. On many other watermeters the black spinning wheel is somewhat bigger and has a small mirror attached. The light from the IR LED is reflected by the mirror and the IR sensor picks up the reflection as a pulse every rotation (in your case 0.5 L). I suggest you buy another water meter that has a sensor build in and provides you with a direct signal. In my home I have separate water meters for each water consumer (such shower, hot water, kitchen, etc.).

    I am using a Honeywell C7195A2001B which has a hall sensor that pulses at 7 Hz per liter/min, so you will get 420 pulses per liter. These meters are typically used in boiler or central heating systems that also can provide hot water for your shower. You need to modify the source code a bit to handle the high pulse frequency, but the nice thing is that the measurement is very accurate.



  • @smilvert You could try a magnometer. I successfully used one on my gas meter that didn't have a magnet in it. Look up "gas meter using a magnometer" in this forum. The code is much different than using the standard water meter sketch.

  • @smilvert @dpcr
    I really doubt this type of water meter having a magnet to sense. Gas meters often are prepared to use a special reed swich to be mounted somewhere outside on the enclosure, and the magnet comes pretty close to the enclosure to be sensed.

    I somewhere found a different solution approach using a modified TCRT5000 board with green LED and a specific lightsensor (see here for details, german).

    The idea behind it is to use complementary (green) light to one of the red pointer wheels to improve contrast when the small pointer passes.

    I recently did some first test wrt. to this, but different to the mentioned solution, I used the original LED/sensor places to connect these two parts (and also changed the resistors values). Unfortunately, I wasn't able to test it "at the right place" until now. As far as I can see, the TCRT5000 inside just also consist of two seperate parts inside a housing. So you may try to do it like that: just pull of the plastic housing, change the LED (see TCRT datasheet which one) with a green one, focus LED and sensor a little more than normal and see, if you get reasonable results whith the trimming resistor.

    Glad for feedback, if this works...

  • @rejoe2 A magnetometer doesn't need a magnet to work, my gas meter doesn't have a magnet and it works fine. Most all smartphones have magnetometers in them to sense direction. However your solution looks to work as well and probably with less code.

  • @dpcr Thanks for clarification.
    Today there was a new post that may be of interest wrt. to the "green" LED sensing modification: https://forum.fhem.de/index.php?action=post;quote=702342;topic=71341.0;last_msg=702342
    This one seems to be close to a "professional" solution also wrt. to the other parts used (but most likely not on a TCRT5000-Base)...

  • I have a question and need some advise,
    is it possible to expand the sketch to two meters.
    In my case I want to combine a this Water Meter with a Gas Meter.
    Can both meters use an interrupt, each?

  • Mod

    @rpunkt welcome to the MySensors commu ity!

    it depends on which microcontroller you use, and what type of interrupt.

    Atmega328 can use pin 2 and 3 at the same time by merging the sketches. More/other pins can be used but that will require some code changes.

  • @smilvert
    I also have same meter as the picture. Yesterday I installed my sensor
    I took a TCRT5000 IR sensor, https://www.mysensors.org/store/water
    A cap from a soda bottle, cut it so it fits above left red wheel(X0,0001) which means one lap i 1 liter
    It works but it counts little bit wrong, I have change the

    if (interval<500000L)


    if (interval<2500000L)

    but anyway it counts about 3 times to much. I will check it later today


  • @flopp
    This didn't worked so well, I got a lot of errors everytime nRF was sending data, then I got one pulse.
    Anyone know how to protect the digital input to NOT fall when nRF sends?
    Wait, when I was writing this text 🙂 I just remember that nRF is using Pin 2, is that correct?
    I will try to use pin 3 instead, as interrupt pin

  • @flopp
    tried pin3, didn't help

    Can it be something wrong with LM393(pcb)?

  • @flopp Nice. You don't use a green led? I tried that and it seems to work better but then I struggle with the mounting.

    Need to try this again!

  • @smilvert said in 💬 Water Meter Pulse Sensor:

    You don't use a green led? I tried that and it seems to work better but then I struggle with the mounting.

    I am using IR to detect movements. Green LED is flashing eveytime the IR detects movement and Red LED is power for PCB.
    I am using Digital Output from PCB to pin 2 or 3 on my UNO. I have tried both with INPUT_PULLUP and without, also tried 20K between Digital Output and GND. nothing is helping

    How do you use green LED with water meter?

  • Hi all i just wanna see The OUTPUT of Serial Monitor for this project sow i can see results .

  • Mod

    Just enable the my_debug

  • How can i get the pulses from SonOff.... i cannot find any digital pin 3 in sonoff... im new to this please guide me