ESP8266: Controlling a relay

Introduction

The objective of this post is to explain how to use the ESP8266 to control a relay. Since we are going to control the relay with a digital pin of the ESP8266, we need to use a device that can operate with 3.3V.

Fortunately, eBay offers a wide variety of relays suitable to be controlled by a microcontroller, as can be seen here. These devices are very cheap and can be bought by less that 1 euro. Also, they can be bought in configurations of 2, 4 and 8 relays per PCB, as can be seen here.

Although most of the relays that appear in the list are specified as 5V relays, most of them can operate with a 3.3V power supply and control. Nevertheless, I cannot guarantee that all will operate well in these conditions, since I only tested a few.

You can check an example of one of these relays in figure 1.

Arduino Relay board
Figure 1 – Relay board. Taken from [1].

Electronics

Important: Working with the main power supply is dangerous if we don’t know what we are doing. I’m not going to explain how to connect the relay to the main power supply since I think the best way to learn these kind of things is with someone supervising, due to the dangers involved. I’m only going to explain the part related with the control of the relay. Please stay safe and don’t try to play with high voltages if you don’t have experience with it.

The connection diagram for this tutorial is very simple since these boards already have all the electronics needed for us to directly control the relay with a GPIO of the ESP8266. For most of these relays, we have two pins for powering the device and a third one to control the state of the relay’s coil.

So basically, as shown in figure 2, we just need to power the device and connect the GPIO pin of the ESP8266 to the control input pin of the board. Please note that the names of the pins may vary depending on the version of the relay.

ESP8266 Connection to relay
Figure 2 – Schematic for connecting the ESP8266 to a relay board.

Naturally, if we choose a device with multiple relays in the same board, we will have some more input control pins.

Although the versions I’ve used operate with an active-low input, there may be versions operating in active-high configuration. You can read more about these concepts here.

If you are going to test the relay for controlling a main power supply device, please don’t forget to check the maximum supported voltage and current for the relay you have.

If you are using a NodeMCU board, the GPIO14 of the ESP8266 will correspond to the pin D5 of the board. You can check here the correct pin mappings.

The code

The code for this example is really simple since we are only going to periodically change the state of the relay.

We start by declaring a global variable that will hold the number of the pin we choose. In our case, following the specified in the diagram of figure 2, we will use pin 14. Then, in the setup function, we declare that pin as output with the pinMode function.

Finally, in the main loop, we change the state of the relay (on/off) by changing the state of the pin (High/Low). To do so, we use the digitalWrite function.

To introduce a delay between changes in the state of the relay, we simply call the delay function.

The full code can be seen bellow.

int relayPin = 14;
 
void setup() {
 
    pinMode(relayPin, OUTPUT);
 
}
 
void loop() {
 
   digitalWrite(relayPin, HIGH); //Set the pin to HIGH (3.3V)
   delay(5000);                  //Delay 5 seconds
   digitalWrite(relayPin, LOW);  //Set the pin to LOW (0V)
   delay(5000);                  //Delay 5 seconds
 
}

Bellow you can check a video where this code is used in a hardware configuration where the ESP8266 is controlling a relay connected to a lamp.

References

[1] http://thumbs1.ebaystatic.com/d/l225/m/my6CmxLH2IHqX_YbjoQFoTQ.jpg

Technical details

  • ESP8266 libraries: v2.3.0

Related Posts

4 thoughts on “ESP8266: Controlling a relay”

  1. Amigo Emmanouil gostaria de fazer este exemplo de ligar relês com NodeMCU usando requisições através de JSON, tem como você explicar como posso fazer estas requisições, fico no aguardo.

    Obrigado pelo conteúdo.

    1. Hi! The easiest way is by setting a simple HTTP webserver on the ESP8266 and receive requests to control the relay. You can check how to set a webserver and control some hardware with the ESP8266 bellow:
      https://techtutorialsx.wordpress.com/2016/11/19/esp8266-webserver-controlling-a-led-through-wifi/

      In this example, I’m controlling a LED instead of a relay, but it’s the same since we are actually controlling a digital pin of the ESP8266/NodeMCU that will be controlling the hardware.

      I don’t have an example using JSON and the ESP8266 as webserver, but it should be pretty straightforward. You can check bellow how to parse JSON with the ESP8266:
      https://techtutorialsx.wordpress.com/2016/07/30/esp8266-parsing-json/

      Hope this helps, let me know if you need more help

  2. Amigo Emmanouil gostaria de fazer este exemplo de ligar relês com NodeMCU usando requisições através de JSON, tem como você explicar como posso fazer estas requisições, fico no aguardo.
    Obrigado pelo conteúdo.

    1. Hi! The easiest way is by setting a simple HTTP webserver on the ESP8266 and receive requests to control the relay. You can check how to set a webserver and control some hardware with the ESP8266 bellow:
      https://techtutorialsx.wordpress.com/2016/11/19/esp8266-webserver-controlling-a-led-through-wifi/
      In this example, I’m controlling a LED instead of a relay, but it’s the same since we are actually controlling a digital pin of the ESP8266/NodeMCU that will be controlling the hardware.
      I don’t have an example using JSON and the ESP8266 as webserver, but it should be pretty straightforward. You can check bellow how to parse JSON with the ESP8266:
      https://techtutorialsx.wordpress.com/2016/07/30/esp8266-parsing-json/
      Hope this helps, let me know if you need more help

  3. Hi,
    Please help me.
    I am controlling relay from windows based dot net application. Now I want to add timer means time to on and off relay specified on UI.
    How can I do this?

    Thanks,
    Amol

    1. Hi!

      That is a very generic question because that can be implemented in many ways 🙂

      You can use multiple protocols for implementing the communication with the ESP, the UI can be HTML based or implemented in the .net application, there are probably a lot of UI libs for .net, etc..

      Where are you having difficulties?

      Best regards,
      Nuno Santos

  4. Hi,
    Please help me.
    I am controlling relay from windows based dot net application. Now I want to add timer means time to on and off relay specified on UI.
    How can I do this?
    Thanks,
    Amol

    1. Hi!
      That is a very generic question because that can be implemented in many ways 🙂
      You can use multiple protocols for implementing the communication with the ESP, the UI can be HTML based or implemented in the .net application, there are probably a lot of UI libs for .net, etc..
      Where are you having difficulties?
      Best regards,
      Nuno Santos

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading