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:

### Hardware

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

### Software

• 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)

### Connections

#### NodeMCU

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

#### Relay

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

#### LED

• +(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:

## 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);
Serial.begin(115200);

}

void loop()
{
{
digitalWrite(pinw, LOW);
Serial.println("On");
}

//.............END of code.............