1

I'm trying to drive a 5V relay with 3.3V ESP-01 with schematic below. I programmed the ESP to start a portal through which I can derive the relay by triggering GPIO-0 & GPIO-2. However when I power on the circuit I encounter the following scenarios:

  1. IF wire is plugged in to GPIO-2
  • ESP blue led turned on forever
  • Relay started buzzing sound forever
  • No WIFI network appears thus not working
  1. IF wire is plugged in to GPIO-0
  • No WIFI network appears thus not working
  1. IF I only plug the wire after powering on the circuit then it works just fine as intended.

Would you please explain what is happening and how to fix it enter image description here

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

const byte DNS_PORT = 53;
IPAddress apIP(172, 217, 28, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);

//int ledPin = 2; 
#define ledPin0 0
#define ledPin2 2

int ledStatus0;
int ledStatus2;

void setup() {
//  Serial.begin(9600);         // Start the Serial communication to send messages to the computer
//  delay(10);
//  Serial.println('\n');

  pinMode(ledPin0, OUTPUT);  ledStatus0 = digitalRead(ledPin0);  digitalWrite(ledPin0, HIGH); ledStatus0 = 1;
  pinMode(ledPin2, OUTPUT);  ledStatus2 = digitalRead(ledPin2);  digitalWrite(ledPin2, HIGH); ledStatus2 = 1;

  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("Toggle Portal");

  // if DNSServer is started with "*" for domain name, it will reply with
  // provided IP to all DNS request
  dnsServer.start(DNS_PORT, "*", apIP);

  // replay to all requests with same HTML
  webServer.onNotFound([]() {
 
    if (webServer.hasArg("led0")) {
      String status = webServer.arg("led0");
      if (status == "off") { digitalWrite(ledPin0, LOW); ledStatus0 = 1;  switchRelayOff(); }
      else if (status == "on") { digitalWrite(ledPin0, HIGH); ledStatus0 = 0; switchRelayOn(); }
    }
    if (webServer.hasArg("led2")) {
      String status = webServer.arg("led2");
      if (status == "off") { digitalWrite(ledPin2, LOW); ledStatus2 = 1;  switchRelayOff(); }
      else if (status == "on") { digitalWrite(ledPin2, HIGH); ledStatus2 = 0; switchRelayOn(); }
    }
  
    String content = "<!DOCTYPE HTML>";
    content += "<html>";
    content += "<head>";
    content += "  <title>ESP Web Server</title>";
    content += "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
    content += "  <link rel=\"icon\" href=\"data:,\">";
    content += "  <style>";
    content += "    html {font-family: Arial; display: inline-block; text-align: center;}";
    content += "    h2 {font-size: 3.0rem;}";
    content += "    p {font-size: 3.0rem;}";
    content += "    body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}";
    content += "    .switch {position: relative; display: inline-block; width: 120px; height: 68px} ";
    content += "    .switch input {display: none}";
    content += "    .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}";
    content += "    .slider:before {position: absolute; content: \"\"; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}";
    content += "    input:checked+.slider {background-color: #1f9f1b}";
    content += "    input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}";
    content += "  </style>";
    content += "</head>";
    content += "<body>";
    content += "  <h2>ESP Web Server</h2>";
    
    content += "  <h4>Output - GPIO 0</h4>";
    if ( ledStatus0 == 0 ) {      content += "  <label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"0\" ><span class=\"slider\"></span></label>";
    } else {      content += "  <label class=\"switch\"><input type=\"checkbox\" checked onchange=\"toggleCheckbox(this)\" id=\"0\" ><span class=\"slider\"></span></label>";    }

    content += "  <h4>Output - GPIO 2</h4>";
    if ( ledStatus2 == 0 ) {      content += "  <label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox2(this)\" id=\"2\" ><span class=\"slider\"></span></label>";
    } else {      content += "  <label class=\"switch\"><input type=\"checkbox\" checked onchange=\"toggleCheckbox2(this)\" id=\"2\" ><span class=\"slider\"></span></label>";    }
    
    content += "<script>function toggleCheckbox(element) {";
    content += "  var xhr = new XMLHttpRequest();";
    content += "  if(element.checked){ xhr.open(\"GET\", \"?led0=on\", true); }";
    content += "  else { xhr.open(\"GET\", \"?led0=off\", true); }";
    content += "  xhr.send();";
    content += "}";
    
    content += "function toggleCheckbox2(element) {";
    content += "  var xhr = new XMLHttpRequest();";
    content += "  if(element.checked){ xhr.open(\"GET\", \"?led2=on\", true); }";
    content += "  else { xhr.open(\"GET\", \"?led2=off\", true); }";
    content += "  xhr.send();";
    content += "}";
    
    content += "</script>";
    content += "</body>";
    content += "</html>";
    webServer.send(200, "text/html", content);
  });
  webServer.begin();
}

void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient();
}
2
  • GPIOs 0 and 2 have a special purpose (randomnerdtutorials.com/esp8266-pinout-reference-gpios), I believe it's possible to use at least one of them as outputs. The relay module already has a transistor included, so the 2N2222 might not be required (do you have a datasheet for the module?)... – Sim Son Feb 19 at 21:40
  • @SimSon this is the question, how to use them? it is not practical to attach them only after power up. – ASH Feb 20 at 2:29
0

"Boot fails if pulled LOW" - this applies to both GPIO 0 and 2. The 2N2222 pulls the GPIO down to one diode drop (0.7V) above GND, which actually is LOW. Instead of using a BJT, you need to control the relay module with a mosfet. Its high gate resistance will have negligible effect compared to the internal pullup resistor.

Chances are that the transistor on the relay module itself is a mosfet. If that's the case and if this mosfet works with 3.3V you can get rid of the 2N2222 at all. In general, using two transistors for this task does not make much sense, but in your case a BJT at the GPIO does not work.

You may notice that the onboard led is connected to GPIO 2, which is high at boot. So if you pull down GPIO 0 somehow, booting fails and GPIO 2 gets pulled HIGH (ref) - the onboard led will light up.

"the question, how to use them?" - once the ESP has booted into your sketch you should be fine to use the GPIOs like you would normaly do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.