[SOLVED] Arduino UNO + ESP8266 + 4 relay

Hi! I’m new to arduino and Blynk but I’m progressing everyday by reading the fórum. Thanks to all!

Right now I have an arduino UNO/esp8266 connected to a 4 relay board.
I’ve flashed ESP8266 Shield HardSer and can control the relays.

Problem is I want to progress further and integrate some variables:
1- Turn relay1 OFF, XX minutes after relay1 ON
2- When relay 1 ON, turn relay 2 OFF.

Can you help me?

@glorifiedg you’ll be glad to know you have already done the hard part, it is all downhill from here.

Take a look at this thread (it relates to the Photon) Blynk with Spark Photon - Dual output control with single botton and they use different timers to Arduinos but it will give you the basics.

Where you see reference to CapTimer you need to substitute the equivalent SimpleTimer code instead.
SimpleTimer example is available at https://github.com/blynkkk/blynk-library/blob/master/examples/GettingStarted/PushData/PushData.ino#L30 where 1000L = 1000 milliseconds / 1 second so 60000L is 1 minute.

This thread might also be useful for you [SOLVED] Turn on and off relay with blynk button and/or physical button

1 Like

Thank you!

I’ve started reading and experimenting and I am already getting some understanding and results with SimpleTimer.

I bet I’ll need more help but for now you gave me plenty to try… After all it’s my 3rd day with arduino and I have 0 electronic/programming background X)

What’s the diference between int relay1 = 12; and #define Relay1 12; ?
Don’t they both say to arduino that relay1 is on pin 12?

Yes they both do pretty much the same thing. Note define does not have a ; at the end of the line.
More details available at https://www.arduino.cc/en/Reference/Define.

1 Like

A define is not changeable in your code. It will be a “hard” value. If you declare something as int, you can change it along the way to a new value.

Usually hard values like pin numbers etc. I declare with define and values I have to do math on, for example, I declare as int, or float or whatever.

1 Like

I bet my code is BS but it works :smiley:

Thank you guys!

Can I invert the button meta? My switches appear ON when relays are off and vice-versa.

Also, how can I define, for example, virtual button 2 to turn on/off pin10?

1- V1 switches my “blinds up” and deactivates “blinds down”, after 20 secs (time they need to fully open) it deactivates “blinds up” (already done)

2- Now I want a push button in, lets say, v2, that allows me to “blinds up” while I press it.

  int setting = param.asInt();
  digitalWrite(2, setting);

This goes outside any loops. I prefer to put it even before the setup() loop. You can use the SimpleTimer to activate or deactivate the button if you wish.

1 Like


I’m still having problems with ON/OFF because the relays are OFF with HIGH and not LOW making buttons to be wrong. I don’t know how to fix it despite tons of reading :frowning:

Do you have pinMode as INPUT_PULLUP?

Paste the part of the sketch that is working in reverse and we will look through it.

Something like this if relay activates on LOW:

BLYNK_WRITE(V2) { // activate relay on digital pin 10 from Blynk virtual pin 2
  int RelayStatus = param.asInt();
  if (RelayStatus == 1) {
    digitalWrite(10, LOW);
    digitalWrite(10, HIGH);

Sent my extract before seeing yours, will check yours now.

Send yours again with </>.

.//#define BLYNK_DEBUG
//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

// Set ESP8266 Serial object

define EspSerial Serial
//Relay in Pin XX

define Relay10 10
define Relay11 11
define Relay12 12
define Relay13 13
ESP8266 wifi(EspSerial);
SimpleTimer timer;

// You should get Auth Token in the Blynk App.
char auth[] = “XXXXXXXXXXX”;

void setup()

digitalWrite(Relay10, HIGH);
digitalWrite(Relay11, HIGH);
// Set console baud rate
// Set ESP8266 baud rate

Blynk.begin(auth, wifi, “XXXXX”, “XXXXX”);


// Turn OFF A relay (left blind)
void turnRelayAOff() {
digitalWrite(Relay10, HIGH);
digitalWrite(Relay11, HIGH);
Blynk.virtualWrite(V1, LOW);
Blynk.virtualWrite(V2, LOW);
Serial.println(“Relays Desativados”);

Serial.println(“Relay 10 ativado”);
digitalWrite(Relay10, LOW);
digitalWrite(Relay11, HIGH);
timer.setTimeout(500, turnRelayAOff);

Serial.println(“Relay 11 ativado”);
digitalWrite(Relay11, LOW);
digitalWrite(Relay10, HIGH);
timer.setTimeout(500, turnRelayAOff);

// Loop
void loop()
timer.run(); // Initiates SimpleTimer

Highlight your sketch in the response box and then click the </> icon above the response box.

You can edit any previous posts you don’t need to delete them.

The extracts provided by @Lichtsignaal and I are not like yours.

You need to read the virtual pin in the Blynk_Write() functions with:

int SomeVariable = param.asInt();

If SomeVariable is 1 (ON) then normally turn the relay on and if it is 0 turn the relay off.

It’s extracted from one of the links you gave me first. Will dig into this new info. Thanks!

It’s working! Thank you very much!

Is it possible to virtually press virtual buttons at the time of the day I predefine? I wanted blinds to open at the same time as my alarm clock rings.
My first tought was Ifttt or automate but they don’t work with blynk.