Blynk.virtualWrite not working when startup

Read this before creating new topic:

  1. Search forum for similar topics
  2. Check
  3. Add details :
    • Hardware model + communication type. For example: ESP32
    • Smartphone OS (iOS or Android) + OS version
    • Blynk server region
    • Blynk Library version
    • Post your FORMATTED sketch code. Remove your AUTH TOKEN from code. Don’t post screenshots
    • Post your serial monitor output when experiencing some issues

:warning: If you don’t format your code, your topic can be deleted by moderators.

Here is a correct example of code formatting:


#include "BlynkEdgent.h"
#include <NTPClient.h>
#include <WiFiUdp.h>

#define RelayPin1 5 

#define SwitchPin1 16  

#define VPIN_BUTTON_1   V1

int toggleState_1 = 1;  //Define integer to remember the toggle state for relay 1
void relayOnOff(int relay) {
  switch (relay) {
    case 1:
      if (toggleState_1 == 1) {
        digitalWrite(RelayPin1, LOW);  // turn on relay 1
        toggleState_1 = 0;
        Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
        Serial.println("Device1 ON");
      } else {
        digitalWrite(RelayPin1, HIGH);  // turn off relay 1
        toggleState_1 = 1;
        Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
        Serial.println("Device1 OFF");
void manual_button() {
  //Manual Switch Control
  if (digitalRead(SwitchPin1) == LOW) {
  toggleState_1 = param.asInt();
  digitalWrite(RelayPin1, toggleState_1);
void setup()
  void setup() {
  pinMode(RelayPin1, OUTPUT);
  pinMode(SwitchPin1, INPUT_PULLUP);
Blynk.virtualWrite(VPIN_BUTTON_1,  toggleState_1);

void loop() {

As you’ve discovered, it doesn’t work like that.

You need to add the BLYNK_CONNECTED() function and put your Blynk.virtualWrite() in thee, like this…

  Blynk.virtualWrite(VPIN_BUTTON_1,  toggleState_1);

You also need to take the blocking delay out of your manual_button function, and I’d reccomend you don’t use while either.
You also need to stop calling manual_button from your void loop.

You also need to use your NTP client properly. You cannot keep getting the NPT time in your void loop. If you do it properly you only need to be getting the NPT time once per day.
It’s also better to use Blynk’s RTC instead of NPT.


Sincerely thank you Pete. this is the first time using blynk so these silly errors will be inevitable, it worked.
As you said I shouldn’t have added manual_button() inside void loop(), but it won’t work if it’s not in there. thanks.

Obviously not, with your current code structure.
You either need to use a BlynkTimer to call your function or attach an interrupt to the button.


If manual_button() is in void loop() will there be any serious problem. I’m really curious.

Yes, that’s why I said that it can’t be there.

Read this…


Thanks Pete, have a nice day.