Completely stumped and desperate for help. Relay no longer actuating on digitalWrite(pin#, HIGH)

Desperately need help with this one…

Attached is what seems like a simple program to turn a relay on and off and to post the relay status to a Blynk template.

When I run this simple program without the Blynk functionality, the ESP32 behaves as expected and the relay toggles on/off at specified intervals.

When I add the Blynk functionality, the template shows the virtual flag going on/off, but the physical relay does not actuate. Any ideas why this is happening?

I am experiencing the same behavior with all my other projects that need to actuate a relay - in one case, this is now happening on code that worked a year ago. I am completely stumped.

Here is my code:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

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

#define relayPin  25

 char ssid[] = "";
 char pass[] = "";
 char auth[] = BLYNK_AUTH_TOKEN;

int fanOnFlag;

BlynkTimer relayOnTimer;
BlynkTimer relayOffTimer;

void relayOn() {
  digitalWrite(relayPin, HIGH);
  fanOnFlag = 1;
  Serial.print("Fan Flag:  ");
  Serial.println(fanOnFlag); 
  Blynk.virtualWrite(V0, fanOnFlag);
}

void relayOff() {
  digitalWrite(relayPin, LOW);
  fanOnFlag = 0;
  Serial.print("Fan Flag:  ");
  Serial.println(fanOnFlag); 
  Blynk.virtualWrite(V0, fanOnFlag);
}

void setup() {

pinMode(relayPin, OUTPUT);
WiFi.begin();
Blynk.begin(auth, ssid, pass);
Serial.begin(9600);
delay(500);

relayOnTimer.setInterval(1000L, relayOn);
relayOffTimer.setInterval(2000, relayOff);

}

void loop() {

Blynk.run();
relayOnTimer.run();
relayOffTimer.run();

}

please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly. Triple backticks look like this: ```

Thanks for responding. I am a newbie to this community, so thx for your pointers. I believe I edited the post as you suggested.

No, you’ve used the wrong characters.
Please copy/paste the triple backticks provided by @Madhukesh

Pete.

1 Like

Hi Pete, thanks - I think I got it right now. Could really use some help with this. I had an application that uses a relay working earlier in the year - suddenly nothing I am doing works. All the logic seems to be fine, but the relay itself does not get energized. I am wondering if this has something to do with current draw - I am measuring between 75 - 150 mA on my meter, but that seems to be within the limits of the ESP32.

First of all, you should remove this…

Then, you don’t need two timer objects…

Because each timer object can support up to 16 timer instances. So, one timer object (which you’d normally call timer) should be used.

Then, what you’re doing with your timers is a bit weird. You’re turning your relay on once every second, all of the time. Then, you’re turning it off once every other second. Depending on how the timing of this works, your code could be turning your relay on, then immediately (within a fraction of a second) turning it off again. The end result could appear that the relay is always on, even though it is actually being turned off for an instant.

Are your relays active HIGH or active LOW ?
Are they the same relays that you’ve used previously?
What version of the ESP32 core, and Blynk library do you have installed?

Pete.