ACROBOTIC Industries ACROBOTIC Industries

WeMos D1 Mini (ESP8266): Relay Shield

Overview

Time to complete: 2–10min; Level of difficulty: Beginner

This guide will show you how to use the WeMos D1 Mini and a Relay Shield to switch a small fan on and off wirelessly over Wi-Fi.

List of Materials


Obtaining the Code

For this activity, we'll be using two sketches inside our Ai Demos WeMos repository:

Different than some of the other shields for the WeMos D1 Mini, there are no additional Arduino libraries that need to be installed for working with the Relay Shield.


How Relays Work

Relays are electronic switches, that is, switches controlled by the presence (or not) of an electric current.  These devices are different from the more traditional mechanical switches, where a button or lever needs to be manually set in order to change the state of the switch.

[Relay vs Button/Switch]

A relay consists of two parts, the control side (coil) and the switching side (terminals).  By applying a voltage and current on the control side, we are able to change the state of the relay's terminals.

[Decapped Relay]

The question in all our minds at this point is: if we need to apply a voltage and current anyway, then why do we even need a relay?

Relay Operation Diagram

The answer is that they offer a convenient way to separate the control signal from whatever is being switched on and off.  Thus, we can use a low-voltage/low-current signal from our favorite microcontroller to (de)activate the coil, and the relay will then switch the terminals so that a connection is made (broken) and electric current can (not) flow.

[mechanical vs. solid-state]

Relays either use an electromagnet or a light-activated transceiver (optoisolator) to make and break the connection of the terminals.  The most common relays have three terminals labeled Normally Open (NO), Common (COM), and Normally Closed (NC).

[animation]

The terminals' names describe their states when the relay is off, that is, when no voltage or current is supplied to the coil.  When the control circuit supplies a voltage and current to the coil or optoisolator, the COM terminal connects to the NO terminal and the connection from the NC terminal to the COM terminal is broken.  When the control circuit stops supplying voltage and current, the COM terminal returns to contact the NC terminal, and the connection to the NO terminal is broken.

When working with relays, we must keep in mind what their ratings are.  Starting with whether they can handle AC, DC, or both.  The relay on the Wemos D1 Mini Relay Shield is rated (datasheet):

NO: 5A (250VAC/30VDC), 10A (125VAC), MAX:1250VA/150W
NC: 3A (250VAC/30VDC), 6A (125VAC), MAX:750VA/90W

Notice that ratings change depending on whether you use the NO or NC terminals!s


Soldering The Pins: Wemos D1 Mini

Different than other shields for the WeMos D1 Mini Development Board, the Relay Shield only comes with one set of pin headers (male).  This is because it is meant to sit at the top of any stack due to it's high vertical extent.  Because of this, the Wemos D1 Mini must use a either a female or long female pin header (socket) if we want to stack the Relay Shield on top.

So that we can mount the Wemos D1 Mini on a breadboard we'll choose the long female pin header.  To facilitate the soldering process we first secure the pin headers onto the board.  One way is to use a breadboard hold the pin headers in place.

Next, we place the Wemos D1 Mini board on top of the header so that all of the short ends of the header are sticking through the board with the relay facing up.

Note that the pins will get hot and it might melt the plastic of the breadboard so as an alteranative you can use tape to secure the pins in.

After securing the pins, we solder each one to the board to make a secure connection.

Finally, we let the board cool down for a couple of minutes, and then remove it from the breadboard or tape.


Soldering The Pins: Relay Shield

Now, we can proceed in a similar fashion to solder the pins onto the Relay Shield .  For this board, we select the male pin headers that were included with the shield.

To facilitate the soldering process we first secure the pin headers onto the board using a breadboard as we did before.

We then place the Relay Shield on top of the header so that all of the short ends of the header are sticking through the board with the relay facing up.

After securing the pins, we solder each one to the board to make a secure connection.

Finally, we let the board cool down for a couple of minutes, and then remove it from the breadboard or tape.


Wiring: Fan Control

Wiring the WeMos D1 Mini to the Relay Shield is very straight forward, simply stack the Relay Shield on top.  Be careful though, check the pin labels first to ensure the proper orientation of the boards.  Choose a pin (e.g., RST) and make sure it lines up on both boards.

[Orientation Fritzing]

In case your WeMos D1 Mini Development Board doesn't have the female sockets needed to connect to the Relay Shield.  You can use a breadboard to wire the connection manually:

Wemos D1 Mini Fan Control Wiring Diagram

As shown in the Relay Shield's schematic, there are a few components onboard in addition to the relay itself.  The circuit is set up so that the coil (control side) of the relay is connected between 5V and the collector terminal of a transistor.  The base of the transistor is connected to a digital pin of the WeMos D1 Mini board (pin D1 when stacked) so that the state of the transistor–and thus, the state of the coil–can be turned ON and OFF based on whether the pin is at a HIGH (3.3VDC) or LOW (0VDC, ground) voltage.

Wemos D1 Mini V2 Relay Schematic

When connecting the shield on a breadboard, we could choose any other pin than D1 on the WeMos D1 Mini (though it'd have to be connected to D1 on the shield itself), but it's best to keep it consistent to the intended use when stacked.

With the wiring done, we're ready to start testing out our code.


Code Walkthrough: Fan Control

For turning the relay on and off we can use a program that's similar (if not identical) to one for blinking an LED! In our setup() function, we simply use the global variable to configure the D1 pin as an OUTPUT, and initialize its state so that the relay is initially turned off.  Given that we've wired the NO relay pin to our fan, a LOW output on the pin corresponds to a non-energized relay coil, which causes no contact between the NO and Common pins.

The loop() function simply switches states between HIGH and LOW by inverting the output of a digitalRead() on the control pin (D1).  The 4-second delay in the function results in the fan being on and off for that length of time.

uint8_t relay_pin = D1;

void setup()
{
  pinMode(relay_pin, OUTPUT); 
  digitalWrite(relay_pin, LOW);
}

void loop()
{
  digitalWrite(relay_pin, !digitalRead(relay_pin));
  delay(4000);
}

Having successfully tested the operation of the relay, we can move on to use the ESP8266's Wi-Fi capabilities for controlling the fan over the internet!


Code Walkthrough: IoT Fan

For controlling the fan over the internet we add the code that allows us to connect to our local Wi-Fi network, and run a simple webserver.  In the setup() function, we configure the pin as an OUTPUT, use the WiFi object to connect to our local Wi-Fi network, and print out the assigned IP address.  Then, we use the server object to create 2 routes to our ESP8266 application.  We want to print out a "usage message" when a user navigates to the top level path "/", and we want to toggle the relay on and off when a user navigates to the "/toggle-relay" path.

void setup()
{
  pinMode(pin_relay, OUTPUT);
  WiFi.begin(ssid,password);
  Serial.begin(115200);
  while(WiFi.status()!=WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.print(WiFi.localIP());

  server.on("/", [](){server.send(200,"text/plain","Hello Relay! Navigate to /toggle-relay to turn the Relay On/Off.");});
  server.on("/toggle-relay", toggleRelay);
  server.begin();
}

Using the on() method of the server object, we associate each of the two routes mentioned above with two corresponding functions.  These functions run when the paths are accessed by any client application.

The first function is defined in place, that is, inside the second argument passed of the on() method:

[](){server.send(200,"text/plain","Hello Relay! Navigate to /toggle-relay to turn the Relay On/Off.");}

This function simply calls the send() method of the server object to create an HTTP response with code 200, content-type plain text, and a string of text as its body.  The string contains a usage message to be sent to the client application accessing the top level (/) path.

For the second path, we actually use the function name as the parameter passed to the on() method.  A few lines below, we define what the function actually does:

void toggleRelay()
{
  uint8_t new_state = !digitalRead(pin_relay);
  String new_state_str = (new_state?"ON":"OFF");
  digitalWrite(pin_relay,new_state);
  server.send(200,"text/plain", "Relay is now "+new_state_str+"!");
}

As we intended, the toggleRelay() function toggles the state of the pin connected to the relay.  It also calls the send() method to send an HTTP response to client applications with code 200, content-type plain text, and a body containing a string reporting the new state of the pin.

You can easily use this code to start connecting different devices to the web, and controlling them wirelessly at will!


Comments, questions, or concerns? Drop us a line!.