Home Automation Using Arduino with Microsoft Visual Studio

Requirements
  1. Visual Studio.
  2. Arduino IDE.
  3. Connecting wires (10 numbers or more).
  4. Breadboard.
  5. DC motors can run by just 5 volts power supply (2 numbers).
  6. LED light (1 number).

Introduction

 
We people expect advancements in our day to day life. That is why I have thought of implementing a voice control technology using Arduino to take control of the electrical appliances at my home. Hence, in this article, I will explain to you how to create an application that helps you control the devices in your home, with your voice commands. So, to make it simple, I will explain how to connect a door that can be opened and closed and a fan along with a light that can be turned on and off, using the voice commands. Let me explain the process step by step. 
 
Note 1 - If you feel that you are unable to understand this program, just click on the following link (Turning LED on and off using voice commands) to learn basic programming for Arduino. Then, it will become very easy for you to implement this technique. 
 
Step 1- Programming the Arduino
  • Open the Arduino IDE, if already installed (or download and install it from this link).
  • Now, open the IDE and enter the following code in it.
  1. char incomingdata;  
  2. void setup() {  
  3.     pinMode(2, OUTPUT);  
  4.     pinMode(4, OUTPUT);  
  5.     pinMode(12, OUTPUT);  
  6.     pinMode(13, OUTPUT);  
  7.     Serial.begin(9600);  
  8. }  
  9. void loop() {  
  10.     incomingdata = Serial.read(); {  
  11.         if (incomingdata == 'a') {  
  12.             digitalWrite(2, HIGH);  
  13.         } else if (incomingdata == 'b') {  
  14.             digitalWrite(2, LOW);  
  15.         } else if (incomingdata == 'c') {  
  16.             digitalWrite(4, HIGH);  
  17.         } else if (incomingdata == 'd') {  
  18.             digitalWrite(4, LOW);  
  19.         } else if (incomingdata == 'e') {  
  20.             digitalWrite(12, HIGH);  
  21.             digitalWrite(13, LOW);  
  22.             delay(3500);  
  23.             digitalWrite(12, LOW);  
  24.         } else if (incomingdata == 'f') {  
  25.             digitalWrite(12, LOW);  
  26.             digitalWrite(13, HIGH);  
  27.             delay(3500);  
  28.             digitalWrite(13, LOW);  
  29.         }  
  30.     }  
  31. }  
  • In this program, I have used a delay of 3.5 seconds in the last two conditions. This is because of the length of the door which I have used in my prototype. You can change the delay length according to your prototype.
  • Save this sketch at any location and verify the code.
  • Now, connect your Arduino board and upload this sketch into the board.
Step 2 - Programming in Visual Studio
  • Open Visual Studio and create new Windows Form application with any name you want.
  • Once you have created a new form application, open the designer window of Form1.
  • Now, from the tools box, drag and drop the SerialPort tool into the form1.
  • This will help you communicate your program with the Arduino board.
  • Now, you have to make your PC recognize your voice so that it can act as a command for the system.
  • For this, we need to include a reference file called "System.Speech".
  • This will now help you to give speech as an input to the computer.
Step 3 - Writing code for home automation
  • We need to write a piece of code which will help the computer to recognize our voice input.
  • So, first we need to set a few predefined commands like LIGHT ON, LIGHT OFF, FAN ON, FAN OFF, DOOR OPEN and DOOR CLOSE.
  • These commands will work as they are if you build your prototype by connecting a DC motor with a small fan wing to work like a fan and another DC motor with a door which can be moved up and down like a shutter door and also an LED which can work like a light.
  • Remember again that you are building a prototype for an automated house and not a real house.
  • Double click on the From1 and paste the following code in there.
  1. using System;  
  2. using System.Speech.Recognition;  
  3. using System.Speech.Synthesis;  
  4. using System.Windows.Forms;  
  5. namespace VoiceAutomation {  
  6.     public partial class Form1: Form {  
  7.         public Form1() {  
  8.             InitializeComponent();  
  9.         }  
  10.         SpeechSynthesizer speechSynthesizerObj;  
  11.         private void Form1_Load(object sender, EventArgs e) {  
  12.             speechSynthesizerObj = new SpeechSynthesizer();  
  13.             speechSynthesizerObj.SpeakAsync("Welcome to the world of voice. Hope you are doing well. Give the commands.");  
  14.             SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();  
  15.             Choices sList = new Choices();  
  16.             sList.Add(new string[] {  
  17.                 "light on",  
  18.                 "light off",  
  19.                 "fan on",  
  20.                 "fan off",  
  21.                 "door open",  
  22.                 "door close"  
  23.             });  
  24.             Grammar gr = new Grammar(new GrammarBuilder(sList));  
  25.             try {  
  26.                 sRecognize.RequestRecognizerUpdate();  
  27.                 sRecognize.LoadGrammar(gr);  
  28.                 sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;  
  29.                 sRecognize.SetInputToDefaultAudioDevice();  
  30.                 sRecognize.RecognizeAsync(RecognizeMode.Multiple);  
  31.                 sRecognize.Recognize();  
  32.             } catch {  
  33.                 return;  
  34.             }  
  35.         }  
  36.         private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {  
  37.             if (e.Result.Text == "lights on") {  
  38.                 serialPort1.Open();  
  39.                 serialPort1.Write("a");  
  40.                 serialPort1.Close();  
  41.             } else if (e.Result.Text == "lights off") {  
  42.                 serialPort1.Open();  
  43.                 serialPort1.Write("b");  
  44.                 serialPort1.Close();  
  45.             } else if (e.Result.Text == "fan on") {  
  46.                 serialPort1.Open();  
  47.                 serialPort1.Write("c");  
  48.                 serialPort1.Close();  
  49.             } else if (e.Result.Text == "fan off") {  
  50.                 serialPort1.Open();  
  51.                 serialPort1.Write("d");  
  52.                 serialPort1.Close();  
  53.             } else if (e.Result.Text == "door open") {  
  54.                 serialPort1.Open();  
  55.                 serialPort1.Write("e");  
  56.                 serialPort1.Close();  
  57.             } else if (e.Result.Text == "door close") {  
  58.                 serialPort1.Open();  
  59.                 serialPort1.Write("f");  
  60.                 serialPort1.Close();  
  61.             }  
  62.         }  
  63.     }  
  64. }  
  • If any error occurs when you copy and paste your code, then it would be just because of the serialPort tool which you could have forgoten to include in the form. 
  • For this issue, have a look over step 2 again.
     
Step 4 - Giving the connections in the breadboard 
  • According to the program, I have used pin 2 of the Arduino for light, pin 4 for the fan and pin 12, 13 for the door.
  • Since the door needs to move in both directions, we need to make the dc motor of the door to run in both directions.
  • Hence, the door is given with two-pin connections.
Connecting the setup
  1. Take a ground connection from the Arduino and connect it to the breadboard.
  2. Take pin 2 and connect it to the LED along with the ground connection.
  3. Take pin 4 and connect it to the DC motor along with the ground connection which will act as a fan.
  4. Take pins 12, 13 and connect each of them to one end of the motor so that it can run in both directions.
  5. Take a look over the image for clarification.
     
Working of the setup
  1. When you say the command light on, lights will turn on and when you say the command light off, the light will turn off.
  2. When you say fan on, a fan will turn on and when you say fan off, a fan will turn off.
  3. When you say door open, the motor of the door will rotate in a direction and then turns off after a few seconds.
  4. When you say door close, the motor will rotate in another direction and then turns off after a few seconds.
Final step
  • If you are unable to build a simulated model, you can just give connections for the LEDs and the DC motors and check the output.
  • Here, for the door, the motor will just rotate in bi-directions. Hence, you need to create the door such that it can move up and down.
  • Once again, check up all your code and the connections.
  • Now, run the program and say the commands like,
    • Light on
    • Light off
    • Fan on
    • Fan off
    • Door open
    • Door close
  • Thus, you have finally created your program and created an automated home. 
Note - I also have few other concepts for this automation technique. They include the password, security alarm, scheduled power on and off of the gadgets, and so on. I hope you will understand and like this concept. Thank you.