Connecting a Relay Module with NodeMCU

In this note, let’s see how to connect a relay module to a NodeMCU board. We use a relay module when we have to drive devices that use more current than NodeMCU board can supply.


Here’s a list of things needed:


  • NodeMCU v1.0 or v0.9
  • Relay module – I used a 5V DC 4 channel relay module
  • Some jumper wires


  • Arduino IDE (
  • Arduino ESP8266-core (Steps for installation can be found in my other blog. Link Here)

Step 1: How does the relay module work?

The relay module is an electrical switch. It isolates the input side from the output side. When it reads input, it uses an electro-magnet to turn the switch on or off.

It’s output side has three pins – COM(Power Supply), NC(Normally Closed), NO(Normally Open).

NO and NC has the load (in this case an LED is being used).

If I give a high input through the input side, NO pin will go LOW and NC pin will go HIGH. Due to it’s isolation, the output side and input side must be two different circuits. I used two different power supplies for the input (Including NodeMCU) and output sides(COM, NC, NO).

Here, I connected the NO (Normally Open) pin of the relay to the LED.

Step 2: Wiring

Here’s the fritzing image (Click to enlarge)

Wiring diagram for connecting a relay module with NodeMCU



  • D1  –> IN1 (channel 1 on the relay module)
  • VIN   –> +5V (Input side power supply)
  • GND –> Ground (Input side power supply)


  • COM –> +3.3V (Output side power)
  • VCC –> +5V (Input side power)
  • GND – Ground (Input side power)


  • +(Anode) – Relay NO pin
  • -(Cathode) – OUTPUT side power supply ground


NOTE: NodeMCU has a built in voltage regulator. So, connect the NodeMCU’s Vin pin to 5V and one of the GNDs to ground of the input side power supply.

EDIT 14/Jan/2018:

Since I have used a bread board power supply on the output side of the relay module, it may be confusing to understand the connections. The below image shows the basic connections required for connecting a +5V power supply to drive an LED using one channel of the relay module:

Basic connections 5V power supply through a relay module

Step 3: Code

The code given below is the same I used in my video.

To use the I/O of the NodeMCU board, I use digitalWrite() function. If I give a HIGH current, the LED will turn on and if I give it a LOW current, it will turn off. Similarly, other three channels in the relay can be connected to other devices.

int pinw = D1;
int pinr = D8;

void setup()
 pinMode(pinw, OUTPUT);
 pinMode(pinr, INPUT);


void loop()
 if (digitalRead(pinr) == HIGH)
 digitalWrite(pinw, LOW);

else if (digitalRead(pinr) == LOW)
 digitalWrite(pinw, HIGH);

//.............END of code.............
Mosquitto MQTT Publish - Subscribe from PHP
Home Automation using IOT - Concept and Working Model

Written by Joseph Justin

I am a gadget freak, electronics enthusiast and passionate programmer. My current interests are electronics platforms like Arduino and various IOT boards. Attending school (St. Peter's Senior Secondary School, Kadayiruppu) by day and coding by night is what I do.