[SOLVED] Button Widget Virtual Pin

Make sure you’ve set your button widget in a switch mode.

Timers are good to keep widgets updated if your pin can be physically affected. But when the pin is affected in your code, you don’t have to wait for a timer callback.

Obviously, you also should keep widget state in your code to avoid networking overload issues.

So, your code may look like this:

const int btnPin = 4;
int btnState = LOW;

void updateButtonWidgetIfNeeded() {
  int newBtnState = digitalRead(btnPin);
  if (btnState != newBtnState) {
    btnState = newBtnState;
    Blynk.virtualWrite(V20, btnState);
  }
}
// ..
BLYNK_WRITE(V21) {
    digitalWrite(btnPin, param.asInt());
    updateButtonWidgetIfNeeded();
}
// ..
timer.setInterval(1000L, updateButtonWidgetIfNeeded);