Arduino Smart Home | DIY IoT Home Automation

| |

A few days back we published an article “Home Automation using Raspberry Pi” in our blog which was well-received among hobbyists. Then we did a Home Automation System using NodeMCU. In this post, I will show you how you can set up a Arduino Smart Home – IoT Home Automation using Arduino!

In this Arduino IoT project, I will show you how to make a simple DIY Arduino Smart Home 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 an Arduino board with network connectivity, some relays, and android phone. Let’s get started.

About Our Sponsor – PCBGoGo

This project is sponsored by PCBGoGo. PCBGoGo is a PCB manufacturer specializing in PCB prototyping, low-volume production, and neat and tidy PCB Assembly. They deliver high-quality PCB faster and cheaper.

Arduino Smart Home

As one of the most experienced PCB manufacturers in China, they pride themselves to be our best business partners as well as good friends in every aspect of your PCB needs.

Time needed: 1 hour and 30 minutes.

How to setup an Arduino Smart Home?

  1. Gather the components

  2. Setup the Circuit

  3. Download and Install Arduino IDE

  4. Setup the Library and Board

  5. Modify the below code

  6. Upload the code

  7. Test

Now lets take a look at everything in detail

Arduino Smart Home – Components Required

Lets get familiarised with components

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.

solid-state relay (SSR) is similar a type of Relay. SSR is basically an electronic switch that switches on & off when a small external voltage is applied across its control terminals. We will be using these relays in our Arduino Smart Home.

SSR in Home Automation. Image from - https://www.allaboutcircuits.com/

Most of the SSRs consist of a sensor which responds to an input signal, generally known as a control signal, a solid-state electronic switching device that closes and opens the load circuit, and a coupling mechanism to enable the control signal to activate this switch without mechanical parts.

There are AC SSR, that works with AC voltages, DC, SSR that works with DC voltages and SSR that works with both AC and DC.

Arduino Nano 33 IOT for Arduino IOT Projects

Newest to the Arduino Nano cousins – The Arduino Nano 33 IoT is the best Arduino board available if you want to add network connectivity to your project. Due to its small form factor, it is very easy to incorporate with existing projects.

This is an IoT board, light, and compact and it’s the same size as that of nano. The board has two 15 pins connectors – one on each side,  pin to pin compatible with the original Arduino Nano.

Get more Details from here.

Arduino Nano 33 BLE Sense Pin Functions

arduino nano 33 ble sense pin functions
Arduino Nano 33 BLE Sense Pin Functions
Note: There are somethings that are to be noted before working with this board. Arduino Nano 33 BLE only supports 3.3V I/Os and is NOT 5V tolerant so please make sure you are not directly connecting 5V signals to this board or it will be damaged. Also, as opposed to Arduino Nano boards that support 5V operation, the 5V pin does NOT supply voltage but is rather connected, through a jumper, to the USB power input.

Arduino Smart Home Step by Step Instructions

Step 1 – Designing the Circuit

Guys, I have designed a PCB layout where you can easily mount your Arduino Nano 33 IOT and your SSR, set this up without using messy wires and cables hanging around.  The board is lightweight and can be powered using a 9V battery or a 9-12 V power adapter.

Arduino home automation circuit

Here, there are 2 voltage inputs – One to power the Arduino and other components on the board and another which will drive the electronic devices connected to the relay, which can depend on the devices.

Arduino home automation PCB Layout

Here I will be connecting LED strips that work on 12 V So, I will be connecting a 12 V DC adapter. The input power is connected to a 7805 regulator. 7805 is a 5V regulator which will convert an input voltage of 7- 32V to a steady 5V DC supply. There are indicator LEDs across various points for easy troubleshooting.

Soldering

Once you get all the components, it’s time for you to solder them together. Solder all the components onto the board and make sure to check the polarity of the components.

I personally find soldering on this kind of PCBs a fun task, because of this pads soldering becomes very easy. The solder takes up the conical shape and get soldered from all the sides evenly. After soldering the PCB looks like this.

Step 2 – Download, Install and Setup Arduino IDE

This little board can be easily programmed using the world’s most user-friendly Open Source Platform – Arduino. This will be explained in detail below. So, to get started with our Home Automation system using Arduino, the first thing to do is download and install Arduino IDE from Here.

Step 3 – Coding

Now we will start coding!


#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
#include "arduino_secrets.h" 
char ssid[] = SECRET_SSID;     
char pass[] = SECRET_PASS; 
int keyIndex = 0;    
unsigned int localPort = 5005; 

char packetBuffer[256]; 
char  ReplyBuffer[] = "acknowledged"; 

WiFiUDP Udp;

void setup() {
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);

    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("nStarting connection to server...");
  Udp.begin(localPort);
}

void loop() {

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    //Serial.print("Received packet of size ");
    //Serial.println(packetSize);
    //Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
   // Serial.print(remoteIp);
   // Serial.print(", port ");
   // Serial.println(Udp.remotePort());

    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    Serial.print("Command Received: ");
    Serial.println(packetBuffer);


 if(strcmp(packetBuffer, "device1on") == 0)
    {
       digitalWrite(3, HIGH); 
       Serial.println("Turning Device 1 ON");

    }
 else if(strcmp(packetBuffer, "device1off") == 0)
    {
       digitalWrite(3, LOW); 
       Serial.println("Turning Device 1 OFF");
    }
 else if(strcmp(packetBuffer, "device12on") == 0)
    {
       digitalWrite(4, HIGH); 
       Serial.println("Turning Device 2 ON");
    }
 else if(strcmp(packetBuffer, "device2off") == 0)
    {
       digitalWrite(4, LOW); 
       Serial.println("Turning Device 2 OFF");
    }
else if(strcmp(packetBuffer, "device3on") == 0)    {
       digitalWrite(5, HIGH); 
       Serial.println("Turning Device 3 ON");
    }
else if(strcmp(packetBuffer, "device3off") == 0)    {
       digitalWrite(5, LOW); 
       Serial.println("Turning Device 3 OFF");
    }
else if(strcmp(packetBuffer, "device4on") == 0)    {
       digitalWrite(6, HIGH); 
       Serial.println("Turning Device 4 ON");
    }
else if(strcmp(packetBuffer, "device4off") == 0)    {
       digitalWrite(7, LOW); 
       Serial.println("Turning Device 4 OFF");
    }

Serial.println("");
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Arduino Smart Home Code Explained
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

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 localPort = 5005;

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

pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

The 4 pins we will connecting to the relay input

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);

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.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
 Serial.print(remoteIp);
 Serial.print(", port "); Serial.println(Udp.remotePort());

int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.print("Command Received: ");
Serial.println(packetBuffer);

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

if(strcmp(packetBuffer, "device1on") == 0)
{
digitalWrite(3, HIGH);
Serial.println("Turning Device 1 ON");
}
else if(strcmp(packetBuffer, "device1off") == 0)
{
digitalWrite(3, LOW);
Serial.println("Turning Device 1 OFF");
}
else if(strcmp(packetBuffer, "device12on") == 0)
{
digitalWrite(4, HIGH);
Serial.println("Turning Device 2 ON");
}
else if(strcmp(packetBuffer, "device2off") == 0)
{
digitalWrite(4, LOW);
Serial.println("Turning Device 2 OFF");
}
else if(strcmp(packetBuffer, "device3on") == 0) {
digitalWrite(5, HIGH);
Serial.println("Turning Device 3 ON");
}
else if(strcmp(packetBuffer, "device3off") == 0) {
digitalWrite(5, LOW);
Serial.println("Turning Device 3 OFF");
}
else if(strcmp(packetBuffer, "device4on") == 0) {
digitalWrite(6, HIGH);
Serial.println("Turning Device 4 ON");
}
else if(strcmp(packetBuffer, "device4off") == 0) {
digitalWrite(7, LOW);
Serial.println("Turning Device 4 OFF");
}

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 lightweight android application that can be used to control robots and Raspberry Pi and Arduino Home Automation 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 Arduino of our Home Automation system using Arduino) and control it using the On-Off buttons. Click here to know more about this App.

Home Automation using Arduino

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).

Arduino Android App

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

Arduino Smart Home

That’s it, your Home Automation system using Arduino is now ready. You can now control devices connected to your Arduino using this simple app and turn it on and off.

Rate the Project

Did you find this page useful? Help us to improve by rating this page.

[RICH_REVIEWS_FORM]
[RICH_REVIEWS_SNIPPET stars_only=”true”]

Similar Posts

Leave a Reply

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