Updating buttons that control digital pins from a button that controls a virtual pin

Hello, everyone!

Just discovered Blynk a couple of days ago. I have an Uno that I have 4 relays hooked up to as a proof of concept. This thing will control 48 relays when its done. My Blynk project has 5 buttons for now. 1 for each relay and then the 5th button controls V0, which turns all relays on/off in loop.

I am having trouble understanding how to push pin state to the individual buttons after I turn all relays on/off with a V0. The code is pretty simple for now, as I just started working on it.

I’ve read a bunch of posts on how to sync virtual pins to digital pins but not other way around.

// #define BLYNK_DEBUG
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>

SoftwareSerial SerialBLE(10, 11); // RX, TX

char auth[] = "*****************";

int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int relay4 = 7;

int relayPins[] = {
  4, 5, 6, 7
int pinCount = sizeof( relayPins ) / sizeof( int );

// Virtual Pin 0 for turning all relays on/off with a single button
  if (param.asInt()) {
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
      digitalWrite(relayPins[thisPin], LOW);
      Serial.println((String)"Turning Pin #" + relayPins[thisPin] + " ON" );


  } else {
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
        digitalWrite(relayPins[thisPin], HIGH);
        Serial.println((String)"Turning Pin #" + relayPins[thisPin] + " OFF" );


void setup()
  // Debug console
  Serial.println("Waiting for connections...");

  Blynk.begin(SerialBLE, auth);


void loop()


Thank you!

digitalRead() and virtualWrite() with BlynkTimer at 500UL (500ms) intervals.

Thanks for the reply. Would you mind elaborating on this a little?

I am a little confused on how digitalRead() and virtualWrite() would push button updates to the Blynk buttons that control digital pins. Shouldn’t this be other way around?

I do not have any physical buttons.

Blynk.virtualWrite(vPin, digitalRead(pin));

Guys, I appreciate responses, but could you please explain how this is going to work?? I realize this is one of the most common questions here, but I am not trying to sync physical button to a virtual button.


I am trying to sync a button (switch mode) that controls V0 pin, to 4 other buttons(also in switch mode) that control pins D4, D5, D6 and D7

I simply answered your question… you said “push pin state” and “push button updates to the Blynk buttons”

I supplied the code that works… and doesn’t care if there are physical buttons or relays on said pins.

Perhaps you need to clarify your question…

@pruchai ok so no buttons just BLYNK buttons.

Blynk button V0 controls all the relays and you also have Blynk buttons for each individual relay.

In the V0 function all you need is:

Blynk.virtualWrite(Vx, y); // where x is v pin of individual button and y is 0 or 1

@Costas Thank you! I really appreciate the explanation. I’ve spent the last couple of days reading the docs and rewriting my code over and over again, and I haveve no trouble whatsoever changing buttons assigned to Vx pins when I press a button that a controls a Dx pin, but not other way around.

The only way I could update a button assigned to control a Dx pin when a Vx pin changes state, is to instead change the button to control another Vx pin instead and put some logic into that Vx pin to do digitalWrite() to a Dx pin I want. I am not sure if this is by design or not, but If I change the state of a button assigned to control a Dx pin by any means other than pressing the button inside the Blynk app, the state of the button in the app does not change, no matter what I do.

Things a complicated a bit further by relays I use. I have a bunch of single channel relay modules that work great, but a 4 channel relay block that I purchased earlier, always goes HIGH every time the Uno is rebooted. The only way I could avoid that, I completely remove pinMode() lines for those 4 particular pins. Single channel relays don’t do that.

This might be solved, or made worse, by a recent bug fix relating to digital pins. Fix will be released in the future but if you find the related thread you can pick up the master branch for testing.

1 Like

I will read this through a few more times and then reply to you.

I just updated my post to add a bit more clarity. Sorry it’s a bit late here and my thoughts don’t exactly line up with my fingers. But I do really appreciate your help!

Still don’t understand what you are tying to do.

To recap, just Blynk buttons no physical buttons.

Let’s use D1 and V1.

What do you want to do?

@pruchai or, since you are already using virtual pins (assuming that is all you are using, if not then that might explain your confusions), just add this to the beginning of your sketch…

#define BLYNK_NO_BUILTIN  // Disable the Apps direct Digital and Analog pin control

… and continue with your pinmode commands and BLYNK_WRITE(Vx) functions and digitalWrite() commands to control digital pins as normal.

  digitalWrite(pin, param.asInt()); // set Digital pin to Vpin state

Also be aware, some relays are triggered on LOW, not HIGH.

@Gunner the OP is working with digital and virtual pins but I haven’t worked out what they want to do with them.

Yes, I am starting to see what I think is happening… At a guess, four App buttons, directly set and controlling each of the digital pins… but I still don’t understand what the OP wants done with the virtual button in relation to them :thinking:

Hard to give good answers when the questions are so uninformative.

1 Like

You need to switch your buttons to virtual pins for that.

When you do:

the Button widget in your Blynk app does not know of this change. Button widget assigned to a physical pin just sends command when you touch it (unlike, say, Value Display widget - it has a refresh interval setting and triggers reading physical pin state accordingly.)

So, the best option for you would be to switch over virtual pins. With virtual pin, you can update Button Widget state yourself by combining:

digitalWrite(relayPins[thisPin], LOW);
Blynk.virtualWrite(buttonPins[thisPin], LOW);

And in the future, if you run into a case when a state of a physical pin is changed from outside of your sketch (say, with a physical button) you can use a timer to continuously check its state - this approach is described in Blynk’s SyncPhysicalButton example sketch.