Help with code for read and sync app button

I really need help here. I have tried several things, but i just don’t get it to work. I have found code snippets that i have tried, but maby i have tried the wrong one.

I do not need you to write the exact code for me, but i want an example that works that i can fill out with my own things etc.

So, what do i want?

First i want to understand how it works, so let us keep it simple.
I use ESP32 board and have an sircuit on my breadboard that contains a led. The led is connectet to pin 24.

I simply want button to sync with the led state. If led is low, i want the button to show “NO” or virsa versa. If i unplugg the led on the breadboard, i want the button to show “OFF”. If i plug the led inn again i want the button to say “ON”.

I just want the button in Blynk app to update when led changes state.
Off course i also want the button to work as an button that can turn the led on/off.

Can somebody please give me a hand here?

I use Arduino IDE.

PS! I have tried several codes, but i just can’t get it to work. Therfore i do not have any code for the moment.

Welcome to the Blynk Community.
Please show us what have you achieved so far.


Have you tried this sketch ?

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Set your LED and physical button pins here
const int ledPin = 7;
const int btnPin = 8;

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH;

// Every time we connect to the cloud...
  // Request the latest state from the server

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);

// When App button is pushed - switch the state
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);

void checkPhysicalButton()
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    btnState = LOW;
  } else {
    btnState = HIGH;

void setup()
  // Debug console

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);

void loop()

I see “const int btnPin = 8;”

Is that digital or virtual pin?

Should i assign app button pin to pin 8?

GPIO7 is pin 20 and GPIO8 is pin 22 on the board. :fu: Check whether you have the same development board.

Connect push button to GPIO8 and LED to GPIO7
On the app tie virtual button to V2.

Everything is in the code, Its just that you need to spend some time and work it around.

“push button” is the app button, right?

I do not have any physical buttons. Just the app button and the led.

If you dont need any physical button to turn on or off the LED then you need not have to connect anything to this. const int btnPin = 8; forget this for the moment.

Open the app
Add button widget
Select “OUTPUT” as virtual and select V2 as mentioned here BLYNK_WRITE(V2)
And select “MODE” to Switch.
If the switching state are inverted just interchange the 0 and 1 on the “OUTPUT” section.

And you are good to go !!

Okay, let’s try to simplify this a bit…

Let’s say that you have a Blynk switch widget attached tpo pin V1 and your physical LED attached to the pin on your ESP32 that is labelled “24” and that this is actually GPIO2 (you’ll need to check this with your actual board layout, but GPIO2 is usually the onboard LED).

It sounds like you are turning this LED on and off within your cde already, and you want to mirror this with the Blynk switch widget ion V1. That means that you’ve already put a pinMode(2,OUTPUT) statement in your void setup, and the at somewhere in your code you are using a digitalWrite(2,LOW) command to turn the LED on (the built-in LED is usually active LOW) and a digitalWrite(2,HIGH) command to turn the LED off.
If that is the case then wherever you have these commands to switch the state of the LED, you also need a command to update the Blynk switch widget on V1. This is done with a Blynk.virtualWrite(V1,1) command to set the switch to the On state and a Blynk.virtualWrite(V1,0) command to set the switch to the Off state. So, your code will look like this:

digitalWrite(2,LOW); // Turn the LED On
Blynk.virtualWrite(V1,1);  // Set the Blynk switch to On

We now need to m,ale this work the other way, when the switch changes state we need to reflect this with the LED. Whenever the switch changes, it triggers a callback function called BLYNK_WRIRE(V1)
Within this function we can get the state of the switch (0 or 1 for Off and ON) using the param.asInt() command. Depending on whether this is a 1 or a 0 we can turn the LED On or Off using a digital.Write command, like this…

  int switch_state = param.asInt();
  if (switch_state == 1) // The switch is now On
    digitalWrite(2,LOW); // Turn the LED On
   digitalWrite(2,HIGH); // Turn the LED Off 

Does this help?


Thanks alot for wondefull explanation! :slight_smile: I will try this later today :slight_smile:

But what if i remove the jumper wire with that result that led turns off. Will the button in the app update?

What jumper wire?


What i really meant was wil the button show led state when Remove power from led?

If my explanation above doesn’t answer that question then I think you need to explain the scenario you’re thinking about in full detail.


I don’t think what you are asking is doable without some additional sensor to tell if the led is on or not. Unless I am misunderstanding your question, it sounds like you want to monitor the led, and have the app button mimic its state while also controlling the led.

In order to be able to tell if the LED is On/Off/Unplugged you would need to some way to check this, for example checking if there was current flowing through the wires. As just checking the state of that pin won’t tell the whole story, cause the pin state of the led (the output pin) may be written HIGH, but if it is unplugged the MCU wouldn’t know that. Also, the sensor that would monitor the led current, would also have to take precedence over the APP button. That is, if you pushed the app button ON, and then the sensor didn’t detect any flow in the wires, it would then need to change the APP button back to OFF.

Instead of monitoring the current, you could also check the brightness of the LED (e.g. Photo-resistor), but still you will need some way to check if the LED is actually on or not. The board itself is not capable of doing this.

I too was confused with what he asked. Its confusing, the way he is explaining. That is why i stepped away.
He would need some sought of current reading sensor. Once the LED(load) is removed the the current will be dropped to zero, This way we can say its the LED there or not.

Sorry for my bad explanation. I’m from Norway and my english is not the thing i’m most proud of.

But i have also missunderstood. My main problem regards to my first question (that was only an example) was that i have an relay i do not know the statement of via app button after power loss. The app button does not read the statement after power is back.

When the device re-connects to the Blynk server, whether at power-on or after re-connection, the BLYNK_CONNECTED callback function is automatically triggered.
Within that function you can have a command that synchronises the state of one or more virtual pins. The code could look lie this (once again, it assumes that your switch is on pin V1):


The Blynk.syncVirtual(V1) command actually causes the BLYNK_WRITE(V1) callback function to be triggered, so whatever code is in function will execute.

Does thais answer your question?


1 Like