Arduino Home Automation Cheap DIY Using NodeMCU

A few days back we published an article “Home Automation using Raspberry Pi” in our blog which was well received among hobbyists. Now one of our members – Krishna S has brought up with a Home Automation System using Arduino and Node MCU.

In this post, we will explained how to build a home automation system that can control electrical appliances such as lights, fans, gates etc suing our mobile phone from anywhere around the world. All you need is a Node MCU board, some relays and an android phone. Lets get started.

Components Required

Click on them to purchase from amazon

The Relay

Relays are switching circuits that can close and break circuits mechanically. That means it can control an electrical circuit by closing and breaking connections in that circuit.

As you can see, there are mainly 5 terminals in a relay. Two for energizing the coil, one Common terminal, a Normally Closed terminal which will be connected to the Common terminal when the coil is not energized and a Normally Open terminal which will be in contact with the Common terminal when the coil is energized.

This is how the relay works. In our case, we will be connecting

  • GPIO pins to One End of Coil
  • Ground to other End of Coil
  • Phase of Main Power Supply to The Common terminal or Pole
  • One Terminal of bulb (Or other electrical Appliance) to Normally Open Terminal
  • And the Other Terminal of Bulb to Neutral Point of Main Power Supply


Depending upon the output of arduino board, you can select your relay. Since the Output of Node MCU GPIO pins are 3.3V, you will have to buy a 3.3V Relay. You can purchase the relay board of 4 relays that is compatible with Raspberry Pi from here or click on the image.


Node MCU is an open source platform that is specifically designed for IOT projects. This tiny little board is based on ESP8266 WiFi module which is specifically designed for working with or woithout micro controllers which uses Lua scripting language.

This module have 10 GPIO, all of which can be used as PWM, IIC, 1-Wire and Analog to Digital Converters all in one board. It also has a PCB antenna etched in the board, which significantly improves the connectivity.


Step 1 – Download, Install and Setup Arduino IDE

This little board can be easily programmed using the worlds most user friendly Open Source Platform – Arduino. This will be explained in detail below. So, first thing to do is download and install Arduino IDE from Here.
Follow the below tutorial and setup Node MCU in your Arduino IDE.

Step 2 – Connections

We powered Node MCU through USB with 5V.

For this project we will be using 4 IO pins to control the devices. D0, D1, D2 and D3 all goes directly to the 4 relays input.

Step 3 – Setting Up the UDP Listener on Node MCU

Now all you have to do is setup a listener on the Pi. Download the sketch from the link below. Download

Code Explained
const char* ssid = "rootsaid";
const char* password = "testpassword";

This is where you enter the ESSID and Passphrase of your network. Before uploading, make sure that you change values to SSID and Passphrase of your WiFi network.

unsigned int port = 5005;

This is the port, NodeMCU will be listening for incoming UDP Packets

pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);

The 4 pins we will connecting to the relay input

 WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
Serial.println("Connection Successful");
Serial.printf("Listener started at IP %s, at port %d \n", WiFi.localIP().toString().c_str(), port);

Connect to the WiFi network using predefined ESSID and Passphrase and set up a UDP listener in the predefined port

int packetSize = Udp.parsePacket();
if (packetSize)
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len =, 255);
if (len > 0)
packet[len] = 0;
Serial.printf("UDP packet contents: %s\n", packet);

Save the UDP packet contents to the variable ‘packet’ and print its value

digitalWrite(D0, HIGH); 
else if(packet=="device1off")
digitalWrite(D0, LOW); 
else if(packet=="device2on")
digitalWrite(D1, HIGH); 
else if(packet=="device2off")
digitalWrite(D1, LOW); 
else if(packet=="device3on")
digitalWrite(D2, HIGH); 
else if(packet=="device3off")
digitalWrite(D2, LOW); 
else if(packet=="device3on")
digitalWrite(D3, HIGH); 
else if(packet=="device3off")
digitalWrite(D3, LOW); 

Turns the output of the pins to HIGH or LOW depending upon the packets received.

Step 4 – Install RootSaid WiFi Command Center from Google PlayStore

RootSaid WiFi Command Center is a simple light weight android application that can be used to control robots and home appliances over WiFi. All you have to do is connect your mobile phone to the network, enter the IP address and Port of the server (the home automation system) and control it using the On Off buttons. Click here to know more about this App.

Click Here to Download this app from Playstore.

Step 5

Now all you have to do is start the App, enter the IP address of the Pi and port it is listening to (5005).

Load the IP and Port using the link button and navigate to the Home Automation Tab.

Thats it, you can now control devices connected to your Node MCU using this simple app and turn it on and off.



Add a Comment

Your email address will not be published. Required fields are marked *