Nodemcu ESP12

The ESP8266 is a Wi-Fi module great for IoT and Home Automation projects. This article is a getting started guide for the ESP8266 development board.

Instructions

  • Start Arduino and open Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and find esp8266 platform.
  • Select the version you need from a drop-down box.
  • Click install button.
  • Don’t forget to select your ESP8266 board from Tools > Board menu after installation.

You may optionally use staging boards manager package link: http://arduino.esp8266.com/staging/package_esp8266com_index.json. This may contain some new features, but at the same time, some things might be broken

About the ESP8266

The ESP8266 is a $4 (up to $10) Wi-Fi module. It allows you to control inputs and outputs as you would do with an Arduino, but it comes with Wi-Fi.
So, it is great for home automation/internet of things applications.

So what can you do with this low cost module?

  • create a web server
  • send HTTP requests
  • control outputs
  • read inputs and interrupts
  • send emails
  • post tweets

ESP8266 specifications

  • 11 b/g/n protocol
  • Wi-Fi Direct (P2P), soft-AP
  • Integrated TCP/IP protocol stack
  • Built-in low-power 32-bit CPU
  • SDIO 2.0, SPI, UART

ESP-12E NodeMCU Kit Pinout

Here’s a quick overview of the ESP-12E NodeMCU Kit pinout:

WeMos D1 Mini Pinout

Here’s the Wemos D1 Mini pinout:

ESP8266-01 Pinout

Here’s the ESP-01 pinout.

Introducing Relays

A relay is an electrically operated switch and like any other switch, it that can be turned on or off, letting the current go through or not. It can be controlled with low voltages, like the 3.3V provided by the ESP8266 GPIOs and allows us to control high voltages like 12V, 24V or mains voltage (230V in Europe and 120V in the US).

1, 2, 4, 8, 16 Channels Relay Modules

There are different relay modules with a different number of channels. You can find relay modules with one, two, four, eight and even sixteen channels. The number of channels determines the number of outputs we’ll be able to control.

Relay Pinout

For demonstration purposes, let’s take a look at the pinout of a 2-channel relay module. Using a relay module with a different number of channels is similar.

2-channel Relay Module Pinout

The two connectors (with three sockets each) on the left side of the relay module connect high voltage, and the pins on the right side (low-voltage) connect to the ESP8266 GPIOs.

Mains Voltage Connections

Relay module mains voltage side

The relay module shown in the previous photo has two connectors, each with three sockets: common (COM), normally closed (NC), and normally open (NO).

  • COM: connect the current you want to control (mains voltage).
  • NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default. The NC are COM pins are connected, meaning the current is flowing unless you send a signal from the ESP8266 to the relay module to open the circuit and stop the current flow.
  • NO (Normally Open): the normally open configuration works the other way around: there is no connection between the NO and COM pins, so the circuit is broken unless you send a signal from the ESP8266 to close the circuit.

Control Pins

2-channel Relay Module

The low-voltage side has a set of four pins and a set of three pins. The first set consists of VCC and GND to power up the module, and input 1 (IN1) and input 2 (IN2) to control the bottom and top relays, respectively.

If your relay module only has one channel, you’ll have just one IN pin. If you have four channels, you’ll have four IN pins, and so on.

The signal you send to the IN pins, determines whether the relay is active or not. The relay is triggered when the input goes below about 2V. This means that you’ll have the following scenarios:

  • Normally Closed configuration (NC):
    • HIGH signal – current is flowing
    • LOW signal – current is not flowing
  • Normally Open configuration (NO):
    • HIGH signal – current is not flowing
    • LOW signal – current is flowing

You should use a normally closed configuration when the current should be flowing most of the times, and you only want to stop it occasionally.

Use a normally open configuration when you want the current to flow occasionally (for example, turn on a lamp occasionally).

Power Supply Selection

2-channel relay module control pins

The second set of pins consists of GND, VCC, and JD-VCC pins. The JD-VCC pin powers the electromagnet of the relay. Notice that the module has a jumper cap connecting the VCC and JD-VCC pins; the one shown here is yellow, but yours may be a different color.

With the jumper cap on, the VCC and JD-VCC pins are connected. That means the relay electromagnet is directly powered from the ESP8266 power pin, so the relay module and the ESP8266 circuits are not physically isolated from each other.

Without the jumper cap, you need to provide an independent power source to power up the relay’s electromagnet through the JD-VCC pin. That configuration physically isolates the relays from the ESP8266 with the module’s built-in optocoupler, which prevents damage to the ESP8266 in case of electrical spikes.

ESP8266 Safest Pins to Use with Relays

Some ESP8266 pins output a 3.3V signal when the ESP8266 boots. This may be problematic if you have relays or other peripherals connected to those GPIOs.

Additionally, some pins must be pulled HIGH or LOW in order to boot the ESP8266.https://b1cd864a44c365e374f190e61e246575.safeframe.googlesyndication.com/safeframe/1-0-37/html/container.html

Taking this into account, the safest ESP8266 pins to use with relays are: GPIO 5, GPIO 4, GPIO 14, GPIO 12 and GPIO 13.

Wiring a Relay Module to the ESP8266 NodeMCU Board

Connect the relay module to the ESP8266 as shown in the following diagram. The diagram shows wiring for a 2-channel relay module, wiring a different number of channels is similar.

Warning: in this example, we’re dealing with mains voltage. Misuse can result in serious injuries. If you’re not familiar with mains voltage ask someone who is to help you out. While programming the ESP or wiring your circuit make sure everything is disconnected from mains voltage.

Alternatively, you can use a 12V power source to control 12V appliances.

Wiring a Relay Module to the ESP8266 NodeMCU Board

In this example, we’re controlling a lamp. We just want to light up the lamp occasionally, so it is better to use a normally open configuration.

We’re connecting the IN1 pin to GPIO 5, you can use any other suitable GPIO.

Controlling a Relay Module with the ESP8266 NodeMCU – Arduino Sketch

The code to control a relay with the ESP8266 is as simple as controlling an LED or any other output. In this example, as we’re using a normally open configuration, we need to send a LOW signal to let the current flow, and a HIGH signal to stop the current flow.

Control a Lamp with the ESP8266 using a Relay

The following code will light up your lamp for 10 seconds and turn it off for another 10 seconds.

 
const int relay = 5;

void setup() {
  Serial.begin(115200);
  pinMode(relay, OUTPUT);
}

void loop() {
  // Normally Open configuration, send LOW signal to let current flow
  // (if you're usong Normally Closed configuration send HIGH signal)
  digitalWrite(relay, LOW);
  Serial.println(" Flowing");
  delay(5000); 
  
  // Normally Open configuration, send HIGH signal stop current flow
  // (if you're usong Normally Closed configuration send LOW signal)
  digitalWrite(relay, HIGH);
  Serial.println(" not Flowing");
  delay(5000);
}

(0)