Using a relay to activate other devices

One of the most useful things you can do with the IoT is to control higher voltage (120-240V) devices like fans, lights, heaters, and other household appliances. Since the NodeMCU operates at 3.3V it can’t control these higher voltage devices directly, but you can use a relay to switch the 120-240V current and use the NodeMCU to control the relay.

A Relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate the switch and provide electrical isolation between two circuits. In this project we will use a NodeMCU to control the relay. We will develop a simple circuit to demonstrate and distinguish between the NO (Normally open) and NC (Normally closed) terminals of the relay.

The NodeMCU can be programmed to turn on the relay when a certain event occurs, for example when the temperature of a thermistor gets higher than 30° C. Or when the resistance of a photoresistor drops below 400 Ohms. Almost any sensor can be used to trigger the relay to turn on or off. The trigger doesn’t even need to be from a sensor. It can occur at set time intervals, it can be triggered from the press of a button, or even when you get an email.

In our case we 'll build a circuit that will activate the relay whenever we press a button on our mobile into the Blynk application.

First of all we have to create the circuit below:

It is a simple circuit with a relay that is taking commands from D5 pin (GPIO 14) and controls the device that is connected on the other side, which is a single LED. In place of a LED we can have any other device of high or low voltage that will take commands from the relay.

Then, create a new Project in Blynk app named "Relay". The auth key will be sent to your email address. You will need it later.

Place a button widget on the desktop of the project and adjust it based on the following screenshots:

Finally, we have to create the code into Arduino IDE. It is similar to the previous section. We just need the default Blynk program that we used on a previous section. So, go to File->Examples->Blynk-Boards_Wifi->Esp8266Standalone

Change the credentials to yours. If you don't know them, ask the admins. The auth[] is the key that you received previously on your email. The ssid[] is the name of your WiFi network and the pass[] is the password.

char auth[] = "????????????";

char ssid[] = "XXXXXXXXXX";
char pass[] = "YYYYYYYYYYY";

Finally Save the file and Press Upload.

If all went according to the instructions, then you can switch ON and OFF the LED.