How to reset VirtualPin count through a button widget?

  • Nodemcu - ESP8266
  • IR Sensor - MH Sensor Series
  • Arduino IDE
  • C++
  • Blynk Version: 1.0.1
  • Android Phone
  • Blynk IoT app

I currently have a button widget that is linked to a datastream (IR_Sensor) to VP5.
Button widget → Datastream (IR_Sensor) → IR_Sensor (virtual pin 5).

While in the app, when I press the button widget it clears the tallied number and shows “0”, but when I pass my hand by the sensor again, the count continues at the last number counted.

Here is a link to the video on imgur that shows how its acting:

Would anyone know if this is possible, or what am I missing to link everything correctly?

Cheers :blush:

#define BLYNK_TEMPLATE_ID           "TMPLU751iNIy"
#define BLYNK_DEVICE_NAME           "Bottle Counter Final"
#define BLYNK_AUTH_TOKEN            "X"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

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

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

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>

int sensorData = D1;
int counter = 0;
boolean state = true;
int count = 0;

BlynkTimer timer; 

void myTimerEvent(){
  sensorData = analogRead(D1); 
  if (sensorData == LOW){
    counter++ ;
    state = false;
    Blynk.virtualWrite(D1, counter);

  if (sensorData){
   state = true;

void setup(){
  Blynk.begin(auth, ssid, pass);
  timer.setInterval( 1000L , myTimerEvent); 

void loop(){; // all the Blynk magic happens here; // BlynkTimer is working...

You have nothing in your code that sets the count to 0 when the button is pressed.

You need to read the documentation. I suggest the part on How to process button input.

Here is a code extraction from that documentation.

BLYNK_WRITE(V1) // this command is listening when something is written to V1
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if (pinValue == 1){
   // do something when button is pressed;
  } else if (pinValue == 0) {
   // do something when button is released;
  Serial.print("V1 button value is: "); // printing value to serial monitor

You should use else.

Your video shows that you are using automations.

My advice would be to fully remove whatever it is you have in your automation, and handle the whole process in your sketch.

Your button widget needs to be connected to a different virtual pin/datastream, and you need a virtual pin handler (a BLYNK_WRITE(vPin) function) in your code fror that datastream, which resets the count variable to zero when the button widget is pressed.