6

I have hooked up 2 channel 5-volt relays with NodeMCU v1.0 as shown in the fritzing below.

Relay are powered with a different power source than NodeMCU Relay are powered with a different power source than NodeMCU.

As of now, I am powering the NodeMCU from my laptop and the relay board from a 5-volt battery.

Below is the code I have written:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "said.";
const char* password = "secret";
ESP8266WebServer server ( 80 );
const int led = 13;
const int outputLed = 12;

void turnOnRelayOne(){
  digitalWrite (outputLed, 1);
  digitalWrite (5, HIGH); //GPIO 5 // Relay 1
  server.send (200, "text/html", "Relay 1 turned on.");
  digitalWrite (outputLed, 0);
}

void turnOnRelayTwo(){
  digitalWrite (outputLed, 1);
  digitalWrite (4, HIGH); //GPIO 4 // Relay 2
  server.send (200, "text/html", "Relay 2 turned on.");
  digitalWrite (outputLed, 0);
}

void handleRoot() {
  digitalWrite ( led, 1 );

  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  snprintf ( temp, 400,

             "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>NodeMCU hooked to Relay Board</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
  <h1>Hello from NodeMCU!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
  </body>\
</html>",

          hr, min % 60, sec % 60
           );
  server.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void handleNotFound() {
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for ( uint8_t i = 0; i < server.args(); i++ ) {
    message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  }

  server.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
}

void setup ( void ) {
  pinMode ( led, OUTPUT );
  pinMode(10, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(outputLed, OUTPUT);
  digitalWrite ( led, 0 );
  Serial.begin ( 9600 );
  WiFi.begin ( ssid, password );
  Serial.println ( "" );

  // Wait for connection
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  Serial.println ( "" );
  Serial.print ( "Connected to " );
  Serial.println ( ssid );
  Serial.print ( "IP address: " );
  Serial.println ( WiFi.localIP() );

  if ( MDNS.begin ( "esp8266" ) ) {
    Serial.println ( "MDNS responder started" );
  }

  server.on ( "/", handleRoot );

  server.on ( "/inline", []() {
    server.send ( 200, "text/plain", "this works as well" );
  } );

  server.on("/relay1", turnOnRelayOne);
  server.on("/relay2", turnOnRelayTwo);
  server.onNotFound ( handleNotFound );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void loop (void) {
  server.handleClient();
}

When I execute the URL: 192.168.1.8/relay1 the outputLed blinks as per the logic in turnOnRelayOne.

But neither of the relays turns on. Please help.

3
  • Im not sure but it looks like it's not a closed circuit, the 5 volt. I wonder if the ground from the 5volt outlet could be split up, one for NodeMCU and one for the relay. Then you would have a closed cuircuit between the relay and NodeMCU.
    – Sigma
    Commented Mar 26, 2017 at 14:00
  • all what you need to do is connect the g on the relay to g on the esp8266 and the battary
    – ehab
    Commented Jan 8, 2018 at 12:38
  • Simply connect the Vcc of the relay to 3V of NodeMCU.It's working for me. Commented Oct 5, 2019 at 7:27

5 Answers 5

8

I have several of these modules. They have an optional opto-isolator built-in, but you have to remove the jumper to use it.

  • Connect your GPIO trigger as you have it
  • Run the relay's Vcc and Gnd pins to the MCU, not the power plug.
  • connect the power plug's +5v to the left-most pin, where the jumper once stood: JDVcc
  • connect the power plug ground to the MCU or the open common Gnd on the left-group of pins.

Now your relay coil(s) will be powered by the 5v plug, but the relay's logic and control are handled by the MCU.

4
  • Can you please elaborate the last connection in your answer, possibly share a fritzing diagram also?
    – Birla
    Commented Oct 4, 2017 at 17:20
  • a diagram will take me a while, i'm swamped at work, but it's a common ground on that last connection: as long as the OP's black wire connects to either GND on the relay or a gnd on the MCU it's ok.
    – dandavis
    Commented Oct 4, 2017 at 20:07
  • Just to confirm, the ground from the 5V supply could either be connected to the second ground (the one beside JD-VCC) on the relay or to a ground pin on the MCU. Correct?
    – Birla
    Commented Oct 5, 2017 at 16:48
  • 1
    right, the GNDs on the relay module (and MCU) have continuity, so those options behave identically.
    – dandavis
    Commented Oct 5, 2017 at 20:32
3

The relay has no reference for what the voltage from the NodeMCU is. Since the power supplies of the two devices are isolated (from each other; the laptop may not be isolated itself, but the battery certainly should be) you can (and in fact must) connect the grounds of the relay board and the NodeMCU together. If neither power supply is isolated or if you are unsure if they are isolated then you should use an optoisolator in order to prevent potential power issues.

2

I believe the problem could be that the relay is 5v but the signal coming from the NodeMCU is 3.3v. You may need do something like boost the signal to 5v or implement a logic level shifter, for example.

Here's a related post: https://electronics.stackexchange.com/questions/213051/how-do-i-use-a-5v-relay-with-a-3-3v-arduino-pro-mini

1
  • 1
    That is not the case, IMO. The output of the NodeMcu is not driving the relay but driving a transistor/fet/optocoupler. In all cases, 3.3V is perfectly valid to turn on. @Ingacio is correct, the grounds must be connected. Now only one wire is the connection between the NodeMcu and the relay. "Voltage" is defined as a difference between two potentials, hence one wire is not enough.
    – Gee Bee
    Commented Feb 26, 2018 at 17:29
0

How are you powering your ESP-12E? This is not included in your diagram. I have had success using the VU pin (5V output) when powering from the Micro-USB. You would then connect VCC to VU, GND to GND, and Signal to your GPIO4. If you are powering your ESP-12E from a 5v power supply directly into the VIN pin, then I would power the Relay from the same 5V power supply. you would connect the + from the power supply to VIN (ESP) and VCC (relay) all the grounds tied together then the GPIO4 to Signal.

-1

This relay module works differently. It is on when the signal is LOW and it turns off when the signal is HIGH (3v3V on the NodeMCU) So you only need to modify the code in the two functions to be LOW instead of HIGH Also make sure the NodeMCU and the relay module have a common ground.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "said.";
const char* password = "secret";
ESP8266WebServer server ( 80 );
const int led = 13;
const int outputLed = 12;

void turnOnRelayOne(){
  digitalWrite (outputLed, 1);
  digitalWrite (5, LOW); //GPIO 5 // Relay 1
  server.send (200, "text/html", "Relay 1 turned on.");
  digitalWrite (outputLed, 0);
}

void turnOnRelayTwo(){
  digitalWrite (outputLed, 1);
  digitalWrite (4, LOW); //GPIO 4 // Relay 2
  server.send (200, "text/html", "Relay 2 turned on.");
  digitalWrite (outputLed, 0);
}

void handleRoot() {
  digitalWrite ( led, 1 );

  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  snprintf ( temp, 400,

             "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>NodeMCU hooked to Relay Board</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
  <h1>Hello from NodeMCU!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
  </body>\
</html>",

          hr, min % 60, sec % 60
           );
  server.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void handleNotFound() {
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for ( uint8_t i = 0; i < server.args(); i++ ) {
    message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  }

  server.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
}

void setup ( void ) {
  pinMode ( led, OUTPUT );
  pinMode(10, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(outputLed, OUTPUT);
  digitalWrite ( led, 0 );
  Serial.begin ( 9600 );
  WiFi.begin ( ssid, password );
  Serial.println ( "" );

  // Wait for connection
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  Serial.println ( "" );
  Serial.print ( "Connected to " );
  Serial.println ( ssid );
  Serial.print ( "IP address: " );
  Serial.println ( WiFi.localIP() );

  if ( MDNS.begin ( "esp8266" ) ) {
    Serial.println ( "MDNS responder started" );
  }

  server.on ( "/", handleRoot );

  server.on ( "/inline", []() {
    server.send ( 200, "text/plain", "this works as well" );
  } );

  server.on("/relay1", turnOnRelayOne);
  server.on("/relay2", turnOnRelayTwo);
  server.onNotFound ( handleNotFound );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void loop (void) {
  server.handleClient();
}
1
  • Hm, can you infer from that drawing the actual type of the relay module? Many modules wok using positive logic, i.e. setting input high turns the relay on.
    – Gee Bee
    Commented Feb 26, 2018 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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