Relay switch issue on ESP32

I have an issue when I use a relay with my ESP32. I have a project to measure current, voltage of 2 LEDs using 3 ina219 sensor and I also used a relay to turn on/off the LEDs. I used blynk iot platform to turn on/off the relay. The relay lights follow the command of the blynk but there is no clicking sound (i.e doesnt turn on/off). What seems to be the problem? Thanks in advance
Here is my code.

#define BLYNK_TEMPLATE_ID "TMPLhMKQW-Vm"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "EvOP11uD9aKMTxeqcMwtcVs3u3gwSMl2"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <Wire.h>
#include <Adafruit_INA219.h>
#include "Adafruit_GFX.h"
#include <Arduino.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "New Beginnings";
char pass[] = "#tat@2022#";

Adafruit_INA219 ina2190(0x40);
Adafruit_INA219 ina2191(0x41);
Adafruit_INA219 ina2192(0x45);

BlynkTimer timer;

float tolerance = 0;
float difference = 0;

#define relay1_pin 2
#define relay2_pin 4
int relay1_state = 0;
int relay2_state = 0;


#define button1_vpin    V1
#define button2_vpin    V2
#define button3_vpin    V3 
#define button4_vpin    V4
#define button5_vpin    V5

BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
  Blynk.syncVirtual(button5_vpin);
}

BLYNK_WRITE(button1_vpin) {
  relay1_state = param.asInt();
  digitalWrite(relay1_pin, relay1_state);
}

BLYNK_WRITE(button5_vpin) {
  relay2_state = param.asInt();
  digitalWrite(relay2_pin, relay2_state);
}

void setup(void) 
{
  Serial.begin(115200);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }

  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);

  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);
  
  Serial.println("Hello!");
  
  ina2190.begin();
  ina2191.begin();
  ina2192.begin();
  
  Blynk.begin(auth, ssid, pass);
  Serial.println("Measuring voltage and current with INA219 ...");
}
  static float shuntvoltage1 = 0;
  static float busvoltage1 = 0;
  static float current_mA1 = 0;
  static float loadvoltage1 = 0;
  static float power_mW1 = 0;

  static float shuntvoltage2 = 0;
  static float busvoltage2 = 0;
  static float current_mA2 = 0;
  static float loadvoltage2 = 0;
  static float power_mW2 = 0;

  static float shuntvoltage3 = 0;
  static float busvoltage3 = 0;
  static float current_mA3 = 0;
  static float loadvoltage3 = 0;
  static float power_mW3 = 0;

  
void loop(void) 
{
  Blynk.run();
  
  shuntvoltage1 = ina2190.getShuntVoltage_mV();
  busvoltage1 = ina2190.getBusVoltage_V();
  current_mA1 = ina2190.getCurrent_mA();
  power_mW1 = ina2190.getPower_mW();
  loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000);
  
  shuntvoltage2 = ina2191.getShuntVoltage_mV();
  busvoltage2 = ina2191.getBusVoltage_V();
  current_mA2 = ina2191.getCurrent_mA();
  power_mW2 = ina2191.getPower_mW();
  loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000);
    
  shuntvoltage3 = ina2192.getShuntVoltage_mV();
  busvoltage3 = ina2192.getBusVoltage_V();
  current_mA3 = ina2192.getCurrent_mA();
  power_mW3 = ina2192.getPower_mW();
  loadvoltage3 = busvoltage2 + (shuntvoltage2 / 1000);
  
  timer.run();
  
  Serial.print("Bus Voltage1:   "); Serial.print(busvoltage1); Serial.println(" V");
  Serial.print("Shunt Voltage1: "); Serial.print(shuntvoltage1); Serial.println(" mV");
  Serial.print("Load Voltage1:  "); Serial.print(loadvoltage1); Serial.println(" V");
  Serial.print("Current1:       "); Serial.print(current_mA1); Serial.println(" mA");
  Serial.print("Power1:         "); Serial.print(power_mW1); Serial.println(" mW");
  Serial.println("");
  
  Serial.print("Bus Voltage2:   "); Serial.print(busvoltage2); Serial.println(" V");
  Serial.print("Shunt Voltage2: "); Serial.print(shuntvoltage2); Serial.println(" mV");
  Serial.print("Load Voltage2:  "); Serial.print(loadvoltage2); Serial.println(" V");
  Serial.print("Current2:       "); Serial.print(current_mA2); Serial.println(" mA");
  Serial.print("Power2:         "); Serial.print(power_mW2); Serial.println(" mW");
  Serial.println("");
      
  Serial.print("Bus Voltage3:   "); Serial.print(busvoltage3); Serial.println(" V");
  Serial.print("Shunt Voltage3: "); Serial.print(shuntvoltage3); Serial.println(" mV");
  Serial.print("Load Voltage3:  "); Serial.print(loadvoltage3); Serial.println(" V");
  Serial.print("Current3:       "); Serial.print(current_mA3); Serial.println(" mA");
  Serial.print("Power3:         "); Serial.print(power_mW3); Serial.println(" mW");
  Serial.println("");

  difference  = current_mA1 - current_mA2 - current_mA3;
  tolerance = current_mA1*0.029;
  if (difference > tolerance)
   {
    Serial.print("THEFT!!!");
    Blynk.virtualWrite(V2, "THEFT");
    }
   else{
    Serial.print("ALL OK");
    Blynk.virtualWrite(V2, "ALL OK");
    } 
  
  Blynk.virtualWrite(V3, power_mW1);
  Blynk.virtualWrite(V4, power_mW2);
 delay(1000);
}
1 Like

Welcome to the forum

What is the operating voltage of the relays and how are they powered, bearing in mind that the ESP32 is a 3,3V device ?

1 Like

Does the one board LED switch on and off?

Post an image of your setup.

Does the relay have opto-isolation?

1 Like

I have used the 3v3 port to power the Vcc of the relay. I also used the 3v3 port to power the ina219 sensors

The in1 switch of the relay doesnt work but the in2 port works fine. Gpio pin 4 is connected to in2 and Gpio pin 2 in connected to in1. I think the Gpio pin works fine since the lights of the relay turn on/off according to command but the switch (in1) doesn't work.



1 Like

Did you try to swap the cables of IN1 and IN2? If in2 works and in1 does not then see if the problem moves when the cables are swapped.

Also your breadboard needs a jumper for the power and ground rails.


see how the center rails are split? to get power and ground across the split a jumper needs to be inserted.

Oh, thanks for posting your properly formatted code in code tags and images of your project.

1 Like

Thanks for replying. I have used only one side of the bread board so the bridging isnt necessary. After swapping the wires then problem still persists the in1 doesnt work and in2 works fine. Additionally the light of the in1 doesnt respond now, the light is turned off and so in the switch.

got another relay module to try?

If swapping the wires did not move the problem, then the problem may not be the code.

1 Like

Do you think there is a voltage shortage in the supply of the relay module? Since esp32 only supplies 3.3V. Do I need a 5V supply? Or is everything ok?

I do not know. I do not know. Obviously, something is wrong if the relay does not work. If swapping the wires did not move the problem, then something may be wrong with the relay.

Where does the 3.3V come from ? And where does it go?
Where are the LEDs series resistors?
A schematic diagram would not be a luxury...

Hi,
As the coils of the relays are 5V, then you will need a 5V supply to power them.
3V3 may be turning the relevant LED ON, on the relay board, but that is not enough for the relay.

There is a yellow jumper on your relay board, look up what it is for.

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.