0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: Controlling a relay

DFRobot Mar 15 2018 2665

The objective of this ESP32 Arduino Tutorial is to explain how to control a relay using the Arduino core, running on a ESP32. The relay board used was this one from Elecrow.
 

Introduction

The objective of this esp32 tutorial is to explain how to control a relay using the Arduino core, running on a ESP32.

We will use a relay board which contains not only the relay but also some additional electronics that allow us to directly control the relay from a digital pin of a microcontroller (in our case, the ESP32).

This tutorial will focus only on the control of the relay and thus we will only work with low voltages and currents. Nonetheless, a relay can be used to control circuits operating with the mains, which can be dangerous if you don’t know what you are doing. Please stay safe and don’t work with the mains if you don’t have experience with it.

The ESP32 board used for this tutorial was a NodeMCU. The relay board used was this one from Elecrow, which can also be obtained as part of this kit. You can check the specs of the relay here.

 

The electric diagram

The electric diagram for this tutorial is really simple since we are only going to need a GPIO of the ESP32 to control the relay board. Figure 1 illustrates the schematic needed.


 

Figure 1 – Electric diagram to wire the ESP32 and the relay board.

As can be seen, we need to connect the GPIO of the ESP32 to the SIG pin of the relay board. Also, both devices need to have a common GND.

Depending on the ESP32 board you are using, it may have an output pin that can be used to power the relay with 5v. Nonetheless, if you are not sure of the maximum current draw it can provide from this pin, it is safer to use an external power supply.

Note that the relay will not be powered by the digital pin, which will be in the  3.3v level (the output of the ESP32). So, the current drawn from the digital pin should be low.

Nonetheless, my recommendation is that you first test the previous schematic taking the control signal from a power supply and measuring the current drawn with a multimeter. It should be lower than the maximum current drawn a ESP32 pin supports, which seems to be 12 mA [1][2][3] (there’s still some confusion in the ESP32 forums about this topic).

The current needed to turn on/off the relay will be drawn from the 5v power supply and thus it should be higher.

The code

The code for this tutorial will also be very simple, since we basically only need to set the digital pin to a high voltage when we want to turn on the relay and to a low voltage when we want to turn it off.

To make things slightly more interesting, we will do a periodic change on the relay state between on and off. Note however that relays are mechanical devices so they are not supposed to be used for very fast commutations. This is not only because they are slow and may not be able to keep up with the changes, but also because a high frequency of commutation may lead to damage of the contacts.

The first thing we will do is declaring a global variable to hold the number of the GPIO that we are going to use to control the relay, so we can easily change it. I will be using pin 23.

int relayPin = 23;
 

Then, we need to set the pin we are using as a digital output. We will do this on the Arduino setup by using the pinMode function.

This function receives as first input the number of the pin we want to configure and as second the mode.

In our case we want to set the pin to output, so we use the OUTPUT constant, which is defined here in the Arduino core for the ESP32.

void setup() {  pinMode(relayPin, OUTPUT); }
 

Next we will alternate the state of the relay periodically in the Arduino main loop. To do so, we simply need to call the digitalWrite function, which receives as first input the number of the pin and as second the state.

The state of the pin can be either high (voltage = Vcc) or low (voltage = Gnd). These states correspond to passing the HIGH or LOW constants to the digitalWrite function, respectively.

Since we want a delay between toggling the states, we will use the Arduino delay function, which receives as input the number of milliseconds to delay.

void loop() {  digitalWrite(relayPin, HIGH);  delay(4000);  digitalWrite(relayPin, LOW);  delay(4000); }
 

The final source code can be seen below.

int relayPin = 23; void setup() {  pinMode(relayPin, OUTPUT); } void loop() {  digitalWrite(relayPin, HIGH);  delay(4000);  digitalWrite(relayPin, LOW);  delay(4000); }
 

 

Testing the code

To test the code, compile it and upload it to your ESP32 using the Arduino IDE. Your relay should start switching its state with the periodicity we have defined in the code.

This switching should be easily noticed by the change of the LED in the board and also the click sound that is heard from the contacts. You check the result in the video below, at my Youtube channel.

 

If you are experienced in working with the mains, you can connect, for example, a lamp to the relay and see the effect more clearly.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.