Blynk and physical switch to control a relay

HI all,

So i have a project where i have a relay, physical switch and Blynk app. The idea is to to be able to control the relay with both, switch and the app.

I have a 2-way switch (ON/ON), middle pin connected to ground and two other pins to 5V. so if its in one position the relay is ON, i get a LED update in blynk, then i should be able to turn OFF the relay from blynk and by switching again on a physical switch to turn ON the relay again, and vice versa, if i turn ON the relay with the app, to turn it OFF with the switch.

My main control loop looks like this, but this doesnt really work

        void control()
  relayState = digitalRead(relayPin);
  switchState = digitalRead(switchPin);
  if (switchState != lastSwitchState){
    digitalWrite(relayPin, !relayState);

    if (switchState = lastSwitchState){
    digitalWrite(relayPin, relayState);

relayState = digitalRead(relayPin);
  if (relayState == LOW) {
    WidgetLED led2(V2);;  
  } else if (relayState == HIGH) {
    WidgetLED led2(V2);

Also, it would be great if the physical switch could update blynk switch, so i do not have to use the LED in the app to see the status of a relay.

Any tips would be highly appreciated.

Many thanks,

P.S. could not find the “code” formatting, pologies.

You need triple backticks at the beginning and end of your code. They look like this:
Copy and paste them if you need to, edit the post using the pencil icon at the bottom.


This description of your wiring makes no sense. You are implying that the middle (common) pin, which is connected to GND will be connected to one or other of the 5V supplies, causing a short circuit.

One way of wiring this type of switch is to connect the middle (common) pin to a GPIO on your MCU, and the other two pins to +5V and GND. Depending on the position of your switch your GPIO will either be HIGH (connected to +5V) or LOW (connected to GND).
In reality this has one more wire than necessary, as you could pull your GPIO HIGH using INPUT_PULLUP, so only the GND and GPIO connection go to the switch. If GND and GPIO are connected then the GPIO pin is pulled LOW, if they aren’t connected then the GPIO pin is HIGH anyway because of the INPUT_PULLUP command.

It’s also better to use a CHANGE interrupt with this type of switch, (along with some denounce code) rather than keep polling the switch on a frequent basis.

Even better, is the use of a push-button switch rather than a changeover switch. physical changeover switches are often used to indicate On/Off depending on their position (usually Up for off and Down for on), unless of course you are using this for domestic light switching in which case it’s normal practice with multi-way switching for the orientation of the switch to have no relationship with the status of the lights.

In terms of code, the Sync physical Button example in the Sketch Builder is a good starting point…

This is written for a momentary push button as opposed to a changeover switch, but won’t be too difficult to change once you decide on a workable wiring setup.