Need help with flow sensor

Hi Dmitriy!

I am new here and i think I have the same problem as @dronecz described in 2015. I have one digital Button on GP16 (in switch mode) and a LED on D0 and GND. When I push the button the LED is changing status, so in this case everything works fine. But when I try to Change LED status on hardware-side (with a if-function) the digital button does not notice it and stays in ist current position.
Do you think my issue fits to the one in here?
Furthermore I do not really understand how to use the Blynk.syncAll() or do not really know if it will solve my problem at all.

Hope you can help me. Looking forward to your response.

best regards,

This is a 4 year old topic and much has changed in that time.
In addition, you’ve not shared any code that explains anything about how you attempted to achieve this…

My guess is that you haven’t included any pinMode statements in your code, and/or referenced the physical pins of your board incorrectly.

I’d suggest that you share your code (correctly formatted with triple backticks at the beginning and end).


Hey Pete! I pasted my Code below. I want to use an Input Numeric Widget on V6 to adjust a certain quantity. When the adjusted amount of water flowed threw the water flow sensor, the LED goes HIGH (code: totalLitres == quantity). Till this point erverything works fine.
I also want to turn the LED ON and OFF manually by pushing the digital button, whenever totalLitres != quantity. This would be no problem, if the digital Button on Blynk would change its status. But as soon as the LED turns on automatically (due to the if-command desribed above) the Widget for the digital Button on Blynk does not synchronize its status.
I read stuff about ways to synchronice digital buttons by implementing certain codes. But to be honest I don’t really get it.

If my desciption isn’t understandable, please let me know.

further Information:

  • ESP8266
  • Blynk 0.6.1
  • water flow sensor on D2
  • LED on D0 (digital Button on Blynk on GP16)
  • Numeric Input Widget on V6


#define BLYNK_PRINT Serial  
#include <ESP8266WebServer.h>
#include <SimpleTimer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = " ";
char ssid[] = " ";
char pass[] = " ";

int quantity;
const int buttonPin = D2;
int addr = 0;
byte sensorInterrupt = 0;
float calibrationFactor = 7.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
float totalLitres;
SimpleTimer timer;

  quantity = param.asInt();

void currentquantity(){
  if(totalLitres == quantity){
    digitalWrite(D0, HIGH);

void showFlow()
  flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  oldTime = millis();
  flowMilliLitres = (flowRate/60) * 1000;
  totalMilliLitres += flowMilliLitres;
  totalLitres = totalMilliLitres/1000;
  unsigned int frac;
  Serial.print("Flow rate: ");
  frac = (flowRate - int(flowRate)) * 10;
  Serial.print(frac, DEC);
  Serial.print(" Current Liquid Flowing: ");
  Serial.print(" Output Liquid Quantity: ");
  Serial.print(" Output Liquid Quantity (L): ");
  Blynk.virtualWrite(V0, int(flowRate));
  Blynk.virtualWrite(V1, int(flowMilliLitres));
  Blynk.virtualWrite(V2, int(totalMilliLitres));
  Blynk.virtualWrite(V3, float(totalLitres));
  pulseCount = 0;
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

void pulseCounter() {

void setup() {
  Blynk.begin(auth, " ", " ");  
  pinMode(buttonPin, INPUT);
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  totalLitres       = 0;
  oldTime           = 0;
  flowRate          = 0.0;
  timer.setInterval(1000L, showFlow);
  attachInterrupt(digitalPinToInterrupt(buttonPin), pulseCounter, RISING);
  timer.setInterval(1000L, currentquantity);

void loop() {;;

@TimoR please edit your post (using the pencil icon at the bottom) and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:


1 Like

Your code and your description of your connections don’t seem to match, and your code seems to be contradictory.

In void setup you are attaching an interrupt to buttonPin, which is defined as D2 (GPIO4)…

You describe this as being your water flow sensor:

You have sensorInterrupt defined as GPIO0 (GPIO16) which is the pin you have the Blynk button widget attached to.
You’re detaching and attaching an interrupt to this pin in your showFlow function (without any initial attachment in void setup) when i think you should really be detaching and reattaching the sensor interrupt.

The simple solution is to use a virtual pin for your Blynk button widget, rather than a digital one.

Also, the syntax for your PulseCounter Interrupt Service Routine inst compatible with the later versions of the ESP Core, as its missing the ICACHE_RAM_ATTR attribute.

I’ve split your comments off into a new topic, as it’s clearly not the same issue that was being discussed 4 years ago.


So if I understood what you said, then I need to do the following steps:

  • change Definition of sensor Interrupt (not the same Pin as the LED)
  • delete detaching and attaching command from showFlow function and put it into void setup
  • update the syntax for my PulseCounter Interrupt Service Routine (do i need to install a new library for that?)
  • replace digital button with virtual one

Do I get it Right?

I already tried to replace the digital button with a virtual one, but everytime I removed the digital one from Blynk app, my LED didn’t work at all. It seemed that there must be a digital pin defined to the right GPIO?

Thanks for splitting off into the new topic. I am sorry for not noticing that it was the false topic.


No, I don’t think you did.

Your variable naming is confusing.
You have one sensor, which is the water flow sensor. It needs to be attached to a physical (digital) pin on your MCU and that pin needs top have an interrupt attached to it so that each time the senor’s hall-effect switch is activated by the impeller moving, the data pulse from the sensor is recorded.
GPIO2 is an appropriate pin to use for this.

Your button widget doesn’t need to be connected to a physical pin at all, so the variable buttonPin is redundant. The button widget doesn’t need an interrupt attaching to it either. When a button widget attached to a virtual pin changes state it triggers the BLYNK_WRITE(virtualpin) callback, in the same way as your slider widget does. The difference between the button and slider widgets is that the button widget has two states - off (0) and on(1). You need to capture the state with the param.asInt command and have two different actions (using an if statement) which do two different things depending on whether the button widget is now on or off. In your case this will be to do a digitalWrite of HIGH (on) or LOW(Off) to the physical pin that your physical LED is attached to.
Your physical LED needs to be connected to physical (digital) pin on your MCU and that pin needs to have a pinMode(OUTPUT) declaration in your void setup to allow the digitalWrite commands to that pin to work successfully.
BTW, GPIO0 isn’t a great pin to use for this - see this post for more information:

It is entirely sensible to stop data pulses from your flow meter triggering interrupts whilst you are performing flow rate calculations and writing these to Blynk. However, you are detaching/attaching the interrupt from the buttonPin (which will now be redundant anyway) and not the sensorPin.

I’d suggest that you google ICACHE_RAM_ATTR to understand how it should be used in your ISR function name.


Good Morning Pete,

I was following your advice and now everything works! I think the biggest Problem was the variable naming and using the digital pin for the LED instead of a virtual one. I also added the Blynk.syncVirtual() command to update the virtual pin to the status of the LED. Just to inform you, how I managed it.
Thanks again for your help! I owe you something! :smiley:

Best regards,

1 Like