BLYNK
HOME       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

Relay : green led and red led

Hello,
I will soon be at the end of my project.
I still need your help.
I would like to put a red led and green led depending on the position of the relay.
I have no problem with the green led. Can you help me with the red led.

#define BLYNK_PRINT Serial


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

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BlynkTimer timer;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxx";

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

// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;
const int ledV1 = 17;
const int ledR1 = 13;


const int relais2 = 32;
const int btn2 = 26;
const int ledV2 = 5;
const int ledR2 = 2;


const int relais3 = 15;
const int btn3 = 25;
const int ledV3 = 18;
const int ledR3 = 4;

const int relais4 = 12;
const int btn4 = 33;
const int ledV4 = 19;
const int ledR4 = 16;

// one wire temperature
#define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 3;
float tempC;






void checkPhysicalButton1();

void temperature();


int relais1Sate = LOW;
int btn1State = HIGH;
int ledV1State = LOW;
int ledR1State = LOW;

int relais2Sate = LOW;
int btn2State = HIGH;
int ledV2State = LOW;
int ledR2State = LOW;

int relais3Sate = LOW;
int btn3State = HIGH;
int ledV3State = LOW;
int ledR3State = LOW;

int relais4Sate = LOW;
int btn4State = HIGH;
int ledV4State = LOW;
int ledR4State = LOW;

int lcd_position[] = {16, 30, 44}; // position temperature sur l'Ă©cran


BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledV1State = param.asInt();
  digitalWrite(ledV1, ledV1State);

}

BLYNK_WRITE(V2) {
  relais2Sate = param.asInt();
  digitalWrite(relais2, relais2Sate);
  ledV2State = param.asInt();
  digitalWrite(ledV2, ledV2State);

}

BLYNK_WRITE(V3) {
  relais3Sate = param.asInt();
  digitalWrite(relais3, relais3Sate);
  ledV3State = param.asInt();
  digitalWrite(ledV3, ledV3State);

}

BLYNK_WRITE(V4) {
  relais4Sate = param.asInt();
  digitalWrite(relais4, relais4Sate);
  ledV4State = param.asInt();
  digitalWrite(ledV4, ledV4State);

}

void temperature()
{
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);


  display.setCursor(0, 0);
  display.println("---------------------");
  display.setCursor(0, 6);
  display.print("|    Temperature    |");
  display.setCursor(0, 12);
  display.println("---------------------");

  display.setCursor(0, 16); // position 0
  display.println(" Interieur  |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 30); // position 1
  display.println(" Exterrieur |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 44); // position 2
  display.println(" Eau        |");
  display.print("---------------------");
  display.display();




  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures();

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {

    tempC = sensors.getTempCByIndex(i);
    Blynk.virtualWrite(i + 21, tempC);
    display.setCursor(80, lcd_position[i]);
    display.print(tempC);
    display.display();

  }
}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledV1State = !ledV1State;
      digitalWrite(ledV1, ledV1State);

      // Update Button Widget
      Blynk.virtualWrite(V1, relais1Sate);
      Blynk.virtualWrite(V1, ledV1State);

    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
    
  }

  if (digitalRead(btn2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      relais2Sate = !relais2Sate;
      digitalWrite(relais2, relais2Sate);
      ledV2State = !ledV2State;
      digitalWrite(ledV2, ledV2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais2Sate);
      Blynk.virtualWrite(V2, ledV2State);

    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btn3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      relais3Sate = !relais3Sate;
      digitalWrite(relais3, relais3Sate);
      ledV3State = !ledV3State;
      digitalWrite(ledV3, ledV3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, relais3Sate);
      Blynk.virtualWrite(V3, ledV3State);

    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      relais4Sate = !relais4Sate;
      digitalWrite(relais4, relais4Sate);
      ledV4State = !ledV4State;
      digitalWrite(ledV4, ledV4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, relais4Sate);
      Blynk.virtualWrite(V4, ledV4State);

    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
}





void setup()
{
  sensors.begin();  // debut de la librairie capteur de température
  // Debug console
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  //timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);

  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledV1, OUTPUT);

  pinMode(relais2, OUTPUT);
  pinMode(btn2, INPUT_PULLUP);
  digitalWrite(relais2, relais2Sate);
  pinMode(ledV2, OUTPUT);

  pinMode(relais3, OUTPUT);
  pinMode(btn3, INPUT_PULLUP);
  digitalWrite(relais3, relais3Sate);
  pinMode(ledV3, OUTPUT);

  pinMode(relais4, OUTPUT);
  pinMode(btn4, INPUT_PULLUP);
  digitalWrite(relais4, relais4Sate);
  pinMode(ledV4, OUTPUT);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  display.display();


  timer.setInterval(100L,checkPhysicalButton1);

  timer.setInterval(10000L, temperature);

}



void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Physical LEDs, or LED widgets?

What are these LEDs connected to?

Pete.

physical leds.
these are indicator LEDs, green on, red off.
she fools plugging into an esp 32

Pins?

Pete.,

Green led : 17, 5, 18, 19
Red led : 13, 2, 4, 16
Relay : 14, 32, 15, 12
Push boutton, 27, 26, 25,33

Your LEDs are connected to multiple pins???

Pete.

Green led 1 : pin 17 || Red led 1 : pin 13
Green led 2 : pin 5 || Red led 2 : pin 2
Green led 3 : pin 18 || Red led 3 : pin 4
Green led 4 : pin 19 || Red led 4 : pin 16

You are missing pinMode statements for your red LEDs.

Pete.

  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledV1, OUTPUT);
  pinMode(ledR1, OUTPUT);

  pinMode(relais2, OUTPUT);
  pinMode(btn2, INPUT_PULLUP);
  digitalWrite(relais2, relais2Sate);
  pinMode(ledV2, OUTPUT);
  pinMode(ledR2, OUTPUT);

  pinMode(relais3, OUTPUT);
  pinMode(btn3, INPUT_PULLUP);
  digitalWrite(relais3, relais3Sate);
  pinMode(ledV3, OUTPUT);
  pinMode(ledR3, OUTPUT);

  pinMode(relais4, OUTPUT);
  pinMode(btn4, INPUT_PULLUP);
  digitalWrite(relais4, relais4Sate);
  pinMode(ledV4, OUTPUT);
  pinMode(ledR4, OUTPUT);

Well seen, it’s good I just added

I have to add which line of code to make it work ?

Pete.

When I switch on, I activate the push button, the green led lights up, but the red led does not work. Once I use blynk application everything works fine

You need to add a BLYNK_CONNECTED() callback, and use Blynk.syncVirtual for the pins you want to synchronise.

Read the documentation and you’ll see an example.

Pete.

I will try to find examples because I do not understand why it does not work.

I made a video, it can help to understand my problem : https://youtu.be/OvSr7YILZ_E

I fully understand the problem.
You don’t need to find examples, just read the documentation for BLYNK_CONNECTED()

Alternatively, you could read this:

and read the “Synchronising the output state with the app at startup” section, but it’s just a more wordy version of what is in the official documentation:

Pete.

in my void setup () I add these lines ??

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);  
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);

}

No, it doesn’t belong in void setup, it’s a stand-alone function and you can’t place one function inside another.

Pete.

Here is a new video for you to see the changes https://youtu.be/b0EOmAw1xpM

My problem is still present

#define BLYNK_PRINT Serial


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

#include <SPI.h>
#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BlynkTimer timer;

char auth[] = "xxxxxxxxxxxxxxxxxx";

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

// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;
const int ledV1 = 17;
const int ledR1 = 13;


const int relais2 = 32;
const int btn2 = 26;
const int ledV2 = 5;
const int ledR2 = 2;


const int relais3 = 15;
const int btn3 = 25;
const int ledV3 = 18;
const int ledR3 = 4;

const int relais4 = 12;
const int btn4 = 33;
const int ledV4 = 19;
const int ledR4 = 16;

// one wire temperature
#define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 3;
float tempC;






void checkPhysicalButton1();

void temperature();


int relais1Sate = LOW;
int btn1State = HIGH;
int ledV1State = LOW;
int ledR1State = LOW;

int relais2Sate = LOW;
int btn2State = HIGH;
int ledV2State = LOW;
int ledR2State = LOW;

int relais3Sate = LOW;
int btn3State = HIGH;
int ledV3State = LOW;
int ledR3State = LOW;

int relais4Sate = LOW;
int btn4State = HIGH;
int ledV4State = LOW;
int ledR4State = LOW;

int lcd_position[] = {16, 30, 44}; // position temperature sur l'Ă©cran


BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  digitalWrite(relais1, relais1Sate);
  ledV1State = param.asInt();
  digitalWrite(ledV1, ledV1State);
  digitalWrite(ledR1, !ledV1State);

}

BLYNK_WRITE(V2) {
  relais2Sate = param.asInt();
  digitalWrite(relais2, relais2Sate);
  ledV2State = param.asInt();
  digitalWrite(ledV2, ledV2State);
  digitalWrite(ledR2, !ledV2State);

}

BLYNK_WRITE(V3) {
  relais3Sate = param.asInt();
  digitalWrite(relais3, relais3Sate);
  ledV3State = param.asInt();
  digitalWrite(ledV3, ledV3State);
  digitalWrite(ledR3, !ledV3State);

}

BLYNK_WRITE(V4) {
  relais4Sate = param.asInt();
  digitalWrite(relais4, relais4Sate);
  ledV4State = param.asInt();
  digitalWrite(ledV4, ledV4State);
  digitalWrite(ledR4, !ledV4State);

}

void temperature()
{
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);


  display.setCursor(0, 0);
  display.println("---------------------");
  display.setCursor(0, 6);
  display.print("|    Temperature    |");
  display.setCursor(0, 12);
  display.println("---------------------");

  display.setCursor(0, 16); // position 0
  display.println(" Interieur  |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 30); // position 1
  display.println(" Exterrieur |");
  display.print("---------------------");
  display.display();

  display.setCursor(0, 44); // position 2
  display.println(" Eau        |");
  display.print("---------------------");
  display.display();




  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures();

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {

    tempC = sensors.getTempCByIndex(i);
    Blynk.virtualWrite(i + 21, tempC);
    display.setCursor(80, lcd_position[i]);
    display.print(tempC);
    display.display();

  }
}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledV1State = !ledV1State;
      digitalWrite(ledV1, ledV1State);


      // Update Button Widget
      Blynk.virtualWrite(V1, relais1Sate);
      Blynk.virtualWrite(V1, ledV1State);

    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;


  }

  if (digitalRead(btn2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      relais2Sate = !relais2Sate;
      digitalWrite(relais2, relais2Sate);
      ledV2State = !ledV2State;
      digitalWrite(ledV2, ledV2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, relais2Sate);
      Blynk.virtualWrite(V2, ledV2State);

    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;

  }

  if (digitalRead(btn3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      relais3Sate = !relais3Sate;
      digitalWrite(relais3, relais3Sate);
      ledV3State = !ledV3State;
      digitalWrite(ledV3, ledV3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, relais3Sate);
      Blynk.virtualWrite(V3, ledV3State);

    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      relais4Sate = !relais4Sate;
      digitalWrite(relais4, relais4Sate);
      ledV4State = !ledV4State;
      digitalWrite(ledV4, ledV4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, relais4Sate);
      Blynk.virtualWrite(V4, ledV4State);

    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);  
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);

}



void setup()
{
  sensors.begin();  // debut de la librairie capteur de température
  // Debug console
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  //timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);

  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledV1, OUTPUT);
  pinMode(ledR1, OUTPUT);

  pinMode(relais2, OUTPUT);
  pinMode(btn2, INPUT_PULLUP);
  digitalWrite(relais2, relais2Sate);
  pinMode(ledV2, OUTPUT);
  pinMode(ledR2, OUTPUT);

  pinMode(relais3, OUTPUT);
  pinMode(btn3, INPUT_PULLUP);
  digitalWrite(relais3, relais3Sate);
  pinMode(ledV3, OUTPUT);
  pinMode(ledR3, OUTPUT);

  pinMode(relais4, OUTPUT);
  pinMode(btn4, INPUT_PULLUP);
  digitalWrite(relais4, relais4Sate);
  pinMode(ledV4, OUTPUT);
  pinMode(ledR4, OUTPUT);


  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  display.display();


  timer.setInterval(100L, checkPhysicalButton1);

  timer.setInterval(10000L, temperature);

}



void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Your checkPhysicalButtonx Functions are also missing the code to update your red LEDs.

Also, your code would be much easier to follow and more efficient if it were streamlined. You don’t need the ledV1State variable in this function at all…

the relais1Sate variable could be used instead.

Pete.

void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      relais1Sate = !relais1Sate;
      digitalWrite(relais1, relais1Sate);
      ledV1State = !ledV1State;
      digitalWrite(ledV1, ledV1State);


      // Update Button Widget
      Blynk.virtualWrite(V1, relais1Sate);
      Blynk.virtualWrite(V1, ledV1State);

    }
    btn1State = LOW;
  } else {

// ********for the red led i have to insert some code here ???? ***********************************

    btn1State = HIGH;
    


  }

This is your line of code that turns your green LED on. If you want to turn/off
If you want to have the red LED doing the exact opposite of the green LED then you need to follow that with a line of code to change the state of your red LED, just as I showed you before.

Pete.