Light control over wifi blynk app as well as physical on off button?

Here is the working code.

There is only 2 little things I would like to change if anyone is able help with some code knowledge.

  1. When the systems boots up after being plugged in, the relay is on the ON position, I would prefer if it could be possible to start in the OFF position.

2.The virtual LED that this code enables in the blynk app in virtual pin 12 is reverse to the actual status. Meaning when the light is off the led is on and when the light is on the led is off, it would be nice to have everything on sync.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
//#define BLYNK_PRINT Serial // Defines the object that is used for printing
//#define BLYNK_DEBUG        // Optional, this enables more detailed prints

#define VPIN V2

char auth[] = "xxxx"; //blynk auth token

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

void lightOn();
void lightOff();

boolean LampState = 0;
boolean SwitchReset = true;

const int TacSwitch = D5; 
const int RelayPin = D6; 

SimpleTimer timer;
WidgetLED VLED(V12);

void setup()      
  pinMode(RelayPin, OUTPUT);
  pinMode(TacSwitch, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(100, ButtonCheck);
void loop()
void ButtonCheck() {
  boolean SwitchState = (digitalRead(TacSwitch));
  if (!SwitchState && SwitchReset == true) {
    if (LampState) {
    } else {
    SwitchReset = false;
  else if (SwitchState) {
    SwitchReset = true;
void ToggleRelay() {
  LampState = !LampState;
  if (LampState) {
  else lightOff();
void lightOn() {
    digitalWrite(RelayPin, LOW);
    LampState = 1;
    Blynk.virtualWrite(VPIN, HIGH);;
void lightOff() {
    digitalWrite(RelayPin, HIGH);
    LampState = 0;
    Blynk.virtualWrite(VPIN, LOW); 
  int SwitchStatus = param.asInt();
    if (SwitchStatus == 2){
  else if (SwitchStatus){
  else lightOff();

Good work getting it sorted :slight_smile:
The random characters inteh Serial monitor will be a mis-matching baud rate.
You need to use 115200 as set in setup();

Simply add the following:

pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);

Just switch the; with VLED.on(); in your code where needed.

Thank you Jamin

The led is working well now. however, the relay is still on when the system turns on.

Sorry I should have noted that you should set it HIGH or LOW depending if your relay is Active HIGH/LOW…

Sounds like yours is active LOW, so set the example to HIGH instead.

pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);

That worked great! Thanks

I will put it all together and update you guys on the end result.

EDIT: I do have one question, I was never able to make the relay work on 5v again, it only works on 3.3v tried 2 power supplies so I know the problem is not there.

1 Like

Agai, that is most likely due to whatever form of power supply you are using. The 5v and 3.3v will be individually sourced from their respective regulators… either built into the MCU or from a separate DC-DC converter.

The only way to determine the problem will be with a meter, both testing the voltage as well as the current draw of the 5v line when powering the relay. Not something that can be remotely diagnosed.

You could just keep using the working 3.3v, however be aware that at that voltage, the relay will be drawing more current than advisable for long term use with the MCU regulator.

@Gunner Okay, I will test it with the meter later, however, I tried one of the board like the one you had for your set up. Also got one of these for the final set up, which gave me the same result. and as well as the mcu 5v pin. The only way it will click is by connecting it to a 3.3v pin, being either on the power supply board or the mcu.

The business end of a relay is a simple electromagnet… nothing more. It will take just about any voltage that the varnished coil wires will contain, but will require a specific current before overcoming the spring loaded lever (aka the “click”). The proper rating is whatever is shown on the top of the relay - usually 5v - and aprox 60-70mA is drawn when coil activated (for the common 5v micro-controlled relay boards that I have tested).

If it will “click” on 3.3v but not on 5v, then that means that for whatever reason, the 5v source is unable to supply the required current. Whereas the 3.3v source can, however is probably tasked harder that designed.

And again, a final requirement when using multiple DC power sources, is that they must all share a common ground… or your circuit will not work, even if proper voltage and current ratings are met.

1 Like

@Gunner sorry for being such a noob but could you tell me in which mode i need to place the meter to get the readings you are telling me?

This is the meter i have

That will be well beyond this forum.

Try this

Also be aware that in most simple meters, you have to move the RED probe to a different socket on the meter for current readings (as well as how you measure in series with the circuit for current instead of in parallel of the circuit for voltage). But NEVER leave your meter in that probe configuration (when storing it) as it could cause damage to both meter and circuit if accidently measuring voltage when in current mode.

Okay so I measured the voltage output of the power supply board and its putting out 4.91 on the 5v pins and 3.29 in the 3.3v pins.

if you would mind to read my comment several days ago, you could save all this time for you and for the others.

  1. most relays (the one at the photo for sure) will draw much more current than a mcu digital pin can safely handle (in the long run you either will have stability issues - you already have, or simply burn down the respective pin)

  2. the coil in the relay is capable to inject a high reverse voltage (kick back) to the digital pin when the relay is turned off. this, again, can cause problems in the long run. you should protect the pin with a flyback diode Flyback diode - Wikipedia

to eliminate all this muck, you should use a mosfet or ss relay. but if you ignore to learn basic electronic stuff before begining projects like this (which is way beyond this forum) you just wasteing your and other members time…

Hello everyone, I’m new here and with little experience, I have nodemcu, and I would like this same project but for 03 relay !!

I tested it with a relay and it worked, but I need it for three !!

Thank you very much!!

Sorry about my English.

I need 4 channel with physical button anybody help me

I need airplane with built in tank, please help


What have you tired so far


I try shoot football on tree but plane no fly with it :confused: also tank is disel with pink heart. :sweat:

Oh God kill me now :sweat_smile:

1 Like

OK, we really need clarification about your hardware if you expect help! Is the diesel itself pink (as in marked fuel) or is the whole tank pink? And what version library have you installed? :stuck_out_tongue_winking_eye:

omg, i never believed that something like this REALLY exists :scream:
but how do you know it’s diesel???