Solenoid causing ESP32 to freeze/become unresponsive

Hi, I have an ESP32 (Adafruit HUZZAH32) and am connecting a FeatherWing power relay that I'm trying to use to switch on/off a solenoid through the web page. But for some reason, every switch of the relay or two, the ESP32 freezes and becomes unresponsive. I have to reset the ESP32 to get it working again and I'm not sure what's going on.

The solenoid I'm wanting to control is 24VAC and below is a picture of how I wired it up:

I also read on this forum that adding a flyback diode might help. So I went ahead and did that but that didn't seem to resolve the issue. (I'm unsure if I actually wired up the "flyback diode" correctly) Here's how I wired up the "flyback diode":

I probably have all of these wire up incorrectly, which might be my problem.

Here are some pictures I took that may help:



Your guys' help is much appreciated, thanks!

Here's my code as well. Thanks!

#include <WiFi.h>
#include <WebServer.h>
#include "SPIFFS.h"
#include "ESPAsyncWebServer.h"

/* SSID and PW */
const char* ssid = "";
const char* pw = "";

/* Network Details */
IPAddress local_ip(192,168,1,120);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

//AsyncWebServer server(80);
WebServer server(80);

/* PIN usage */
uint8_t LED = 21;
uint8_t relayCtrl = 15;
bool LEDstatus = LOW;
bool relayCtrl_Sig = LOW;


String processor(const String& var) {
  Serial.println(var);
  return String();
}

void setup() {
  Serial.begin(115200);

  // Setting PINs
  pinMode(LED,OUTPUT);
  pinMode(relayCtrl,OUTPUT);
  digitalWrite(LED, LEDstatus);
  digitalWrite(relayCtrl, relayCtrl_Sig);
  
  // Setting up File System
  if(!SPIFFS.begin(true)) {
    Serial.println("An error has occurred while mounting SPIFFS\n");
    return;
  }

  File file = SPIFFS.open("/index.html", FILE_WRITE);

  if (!file) {
    Serial.println("Error openin file");
    return;
  }

  file.close();

  Serial.println(SPIFFS.exists("/index.html"));
  
  // Setting up ESP32 Station Mode
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssid, pw);
  delay(100);

  Serial.print("Connecting to WiFi...");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println(WiFi.localIP());
  
  server.on("/", handle_OnConnect);
  server.on("/ledon", handle_ledon);
  server.on("/ledoff", handle_ledoff);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");
}
uint16_t count = 0;
void loop() {
  server.handleClient();

  //Serial.println("Count: ");
  //count = count + 1;
  //Serial.println(count);
  digitalWrite(LED, LEDstatus);
  delay(500);
  digitalWrite(relayCtrl, relayCtrl_Sig);
  delay(500);
}

void handle_OnConnect() {
  LEDstatus = LOW;
  relayCtrl_Sig = LOW;
  Serial.println("LED OFF");
  server.send(200, "text/html", SendHTML(false, LEDstatus));
}

void handle_ledon() {
  LEDstatus = HIGH;
  relayCtrl_Sig = HIGH;
  Serial.println("LED ON");
  server.send(200, "text/html", SendHTML(true, LEDstatus));
}

void handle_ledoff() {
  LEDstatus = LOW;
  relayCtrl_Sig = LOW;
  count = 0;
  Serial.println("LED OFF");
  server.send(200, "text/html", SendHTML(false, LEDstatus));
}

void handle_NotFound() {
  server.send(404, "text/plain", "Not Found");
}

String SendHTML(uint8_t led1stat,uint8_t led2stat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>LED Control</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button-on {background-color: #3498db;}\n";
  ptr +=".button-on:active {background-color: #2980b9;}\n";
  ptr +=".button-off {background-color: #34495e;}\n";
  ptr +=".button-off:active {background-color: #2c3e50;}\n";
  ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<h1>ESP32 Web Server</h1>\n";
  ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
  
   if(led1stat)
  {ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/ledoff\">OFF</a>\n";}
  else
  {ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/ledon\">ON</a>\n";}

  ptr +="</body>\n";
  ptr +="</html>\n";
  

  return ptr;
}

Hi,
Thanks for the info.
A flyback diode will not work with AC, you need some form of "snubber" circuit across the contacts of the relay.

I assume if you disconnect the solenoid the fault does not occur?

If you Google

24vac solenoid snubber circuit

It will show you some solutions that may help.

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

That diode is most likely blown. Throw it away.

You are driving a 5V relay with an ESP32 GPIO pin. Yea, no wonder you are having issues.

It is not a relay, it is a relay module.

And it is not a 5 V relay in any case, but a 3.3 V one.


I use the optically isolated relay modules for the ESP32, they work better, for the ESP32.

Hi Tom,

You're correct, if I disconnect the solenoid, the fault does not occur.

I will look into the snubber circuit.

Thanks for your help! I'll let you guys know how it goes.

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