ACROBOTIC Industries ACROBOTIC Industries

WeMos D1 Mini (ESP8266): Relay Shield


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 and Switch/Button ComparisonA 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. 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 Relay

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

Relay Operation Animated

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.

Wemos D1 Mini Relay Shield Pin Headers Selection

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.  One way to do this is to use a breadboard to hold the pin headers in place.

Soldering Long Female Headers To WeMos D1 MiniWe'll make use of the male headers for holding up the long female sockets.  We can then slide the WeMos D1 Mini board through the pins of the female socket.

Soldering Long Female Headers To WeMos D1 Mini

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.

Soldering Long Female Headers To WeMos D1 Mini

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.

Soldering Long Female Headers To WeMos Relay Shield

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

Soldering Long Female Headers To WeMos Relay Shield

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

Soldering Long Female Headers To WeMos Relay Shield

After securing the pins, we solder each one to the board to make a robust 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.

Now, we'll connect the fan itself. As is typical when using a relay, we'll be using an external power supply to power the fan but not the WeMos D1 Mini or Relay Shield themselves, which will be powered from the USB port of the computer we'll use to load our code.

Wemos D1 Mini Fan Control Wiring Diagram

To connect the external power supply we first ensure that it's disconnected from the outlet and that the WeMos D1 Mini is unplugged from the USB cable. We then connect the positive lead of the power supply to the COMMON pin of the relay using the middle screw terminal.

Next, we connect the positive lead of the fan to the NO (normally open) pin of the realy using the screw terminal that's labeled accordingly.

Lastly, we tie together the power supply negative lead (ground) to the negative lead of the fan. We secure this connection by soldering the leads and using heat shrink to prevent accidental shorting.

As shown in the Relay Shield's schematic below, the electrical design includes components in addition to the relay itself. 

Wemos D1 Mini V2 Relay Schematic

The circuitry of the Relay Shield 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.  We can specify the HIGH/LOW state of pin D1 in the code running on the ESP8266.

If we didn't want to use pin D1, we could use a breadboard for the connections.  But the main advantage of using the WeMos is the ability to stack the boards on top of one another.

Once the boards are stacked, 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));

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);
  Serial.print("IP Address: ");

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

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");
  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!.