ESP8266: Controlling a relay

The objective of this post is to explain how to use the ESP8266 to control 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

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
Advertisements
This entry was posted in ESP8266 and tagged , , , , , , , . Bookmark the permalink.

2 Responses to ESP8266: Controlling a relay

  1. cyberlacs says:

    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.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s