[SOLVED] Relay Control With Push Button and Button Widget

Okay so i know you want to solve this software wise, but i’m more of a hardware guy so to give you some kind of solution i would suggest using a Transistor in parallel to the physical switch. Like this:

I am assuming this is when using a physical switch instead of a physical button? Because, it works just fine in my application.

Try swapping the button widget so that it sends a 1 when off and a 0 when on.

Pete.

2 Likes

Oh yheah those arduino relays are weird sometimes. I have some that turn on with a 1 and others that turn on with a zero.

1 Like

Damn it man! It was my timer!!! I had set it to 100 ms and that lead to all the problems!!! Thanks to everyone for helping out!
Btw any suggestions for improving the code? or any changes suggestion?
P.S. There’s a slight lag now with button press.

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#include <SPI.h>
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h> // here is the SimpleTimer library

char auth[] = "auth"; //insert here your token generated by Blynk
SimpleTimer timer;   // allocate a name (timer) to the timer

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

int button1 = 12;
int button2 = 11;
int button3 = 10;
int button4 = 9;

int relayVButton1 = 0;
int relayVButton2 = 0;
int relayVButton3 = 0;
int relayVButton4 = 0;

int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int buttonState4 = HIGH;

boolean relayState1 = 1;
boolean relayState2 = 1;
boolean relayState3 = 1;
boolean relayState4 = 1;

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}
WidgetLED led1(11); //virtual led 
WidgetLED led2(12); //virtual led
WidgetLED led3(13); //virtual led
WidgetLED led4(14); //virtual led
WidgetLCD lcd(V31);

void setup() 
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(Serial, auth); 
  timer.setInterval(600L, somefunction);
 
  pinMode(relay1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  pinMode(relay2, OUTPUT);
  pinMode(button2,INPUT_PULLUP);
  pinMode(relay3, OUTPUT);
  pinMode(button3,INPUT_PULLUP);
  pinMode(relay4, OUTPUT);
  pinMode(button4,INPUT_PULLUP);
  digitalWrite(relay1, 1);
  digitalWrite(relay2, 1);
  digitalWrite(relay3, 1);
  digitalWrite(relay4, 1);
} 

void somefunction()
{
//Relay1
  if (digitalRead(button1) == LOW)
    {
      if (buttonState1 !=LOW)
      {
       relayState1= !relayState1;
      Blynk.virtualWrite(V1, relayState1);
      digitalWrite(relay1, relayState1);
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay 2
     if (digitalRead(button2) == LOW)
    {
      if (buttonState2 !=LOW)
      {
       relayState2= !relayState2;
      Blynk.virtualWrite(V2, relayState2);
      digitalWrite(relay1, relayState2);
            }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay3
   if (digitalRead(button3) == LOW)
    {
      if (buttonState3 !=LOW)
      {
       relayState3= !relayState3;
      Blynk.virtualWrite(V3, relayState3);
      digitalWrite(relay1, relayState3);
      
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay4
      if (digitalRead(button4) == LOW)
    {
      if (buttonState4 !=LOW)
      {
       relayState4= !relayState4;
      Blynk.virtualWrite(V4, relayState4);
      digitalWrite(relay1, relayState4);
      
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
  if (digitalRead(relay1) == LOW) //virtual led1
  {
   led1.on();
   }
   else
   led1.off();
       
  if (digitalRead(relay2) == LOW) //virtual led2
  {
   led2.on();
   
  }
   else
   led2.off();
   
  if (digitalRead(relay3) == LOW) //virtual led3
  {
   led3.on();
   
  }
   else
   led3.off();
     
   if (digitalRead(relay4) == LOW) //virtual led3
  {
   led4.on();
   
  }
   else
   led4.off();
  }

  


BLYNK_WRITE(V1)
{
   relayVButton1 = param.asInt(); // Get the state of the VButton
   digitalWrite(relay1, relayVButton1);
}
BLYNK_WRITE(V2)
{
    relayVButton2 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay2, relayVButton2);
} 
BLYNK_WRITE(V3)
{
    relayVButton3 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay3, relayVButton3);
} 
BLYNK_WRITE(V4)
{
    relayVButton4 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay4, relayVButton4);
} 
 
void loop() 
{
  Blynk.run(); 
  timer.run();                 // call the simple timer routine
 
}

@Dema323 , yah they are really weird! Mine turns on with a 0 !

1 Like

So did you end up using a Physical button or Physical switch?

Also, I though the idea was to not use the led widget, but i see you are monitoring the relay pin and turning the virtual led on/off. If you aren’t using them on the App, I would remove the code from your program.

How long is a “slight lag”?

Probably normal, the lag goes away after some time being connected i find.

I suspect the lag is do to the fact that the timer was changed from 100ms to 600ms. If I am interpreting the code correctly, it must go through one timer cycle with the button not pressed in order to reset buttonState back to high. Therefore, you must have the button not pressed for 600ms before you can press it again to change states. Additionally, the button may need to be held for up to 600ms (depending on where the timer is/was when button was pressed) in order for the digitalRead to be detected.

I believe this may be why the original timer interval was 100ms.

I run this code on one of my projects with the 100ms timer interval with no issues, and button lag has not been a problem.

1 Like

Yah i removed them later ! I ended up using a physical push button and the blynk switch button widget!

Yah the lag is due to the timer change , however my code doesn’t seem to work with timer set as 100ms.

With Timer set to 100ms i get sequential toggles with my physical button! From the app there was no sequential toggles!

Only after setting it to a value higher than 200ms my issue of sequential toggle resolve !

Probably due to switch bounce. Maybe try a different type of button and see if you get better results. If you can live with a 200ms timer interval, then no need. I suspect the lag is hardly noticeable at that rate.

Glad to hear you got it going. BLYNK on!

1 Like

They are separate, thus will not necessarily experience the same issue. The timer is controlling the loop cycle on the hardware. The app button works outside (independent) of that timer loop and utilises its own BLYNK_WRITE(vPIN) function loop.

1 Like

My 50ct: MyPIR-Sensor activate RGB Stripe controlled by Relay

1 Like

Got it thanks! It solved now!! Thank you guys! :smiley:

Can u send me the code Im using ardunio uno with HC-05 Bluetooth module, I can only control a relay using blynk(widget button), I would like to control a relay wall fixed physical push switch too ,plz send me your code it will more helpful.

@Karthik Welcome to the Blynk forum…

1st, this is a 6 month old topic, that has been resolved.

2nd, the code you are asking for is already posted above (and more like it is all over this forum)… you just need to fix the timer issue. And if you are unfamiliar with that, then you need to spend some time reading through the Documentation and Help Center, links at the top/right of this page… not asking for code handouts.

This forum is here to help you learn Blynk. Run through the links mentioned, search for other topics/projects & examples, read through this forum, try out the Examples from the Sketch Builder, and make some test projects of your own. If you have any issues, create your own topic with details and we will try to help.

Thank you.