loading

Hello All!

Now I don't know about you, but to me it seems an awful lot of effort to get up and flick a switch on an appliance. Let's say a lamp. Wouldn't it be marvellous, I ask myself, if I could just speak to my phone......YES IT WOULD.

So in this 'Instructable' I will attempt to show how to connect an Arduino to your android phone via Bluetooth and then use voice commands to control high voltage devices using relays.

Source code is available at:

https://github.com/dougbrion/instructables-home-automation

Electricity is dangerous so please be careful!!!!

So without further ado let's charge in!

Step 1: Things You Will Need!

Step 2: Relay Test

So first of all we want to check if the relay is switching.

Wire up the relay as shown in the diagram, make sure that the JD-VCC and VCC pins are bridged if you are powering the relay from your arduino. If they are not bridged you will see the LED turning on and off every 2 seconds but there will not be the clicking sound of the relay switching.

Code:

#define relay 2    //attaches the relay to pin 2

void setup()
{
  pinMode(relay, OUTPUT);    //sets the relay as an output
}
void loop()
{
  digitalWrite(relay, HIGH);    //relay open
  delay(2000);                  //wait 2 seconds
  digitalWrite(relay, LOW);     //relay closed
  delay(2000);                  //wait 2 seconds
}

Step 3: Bluetooth Test

First off wire up the circuit as shown above. I have used a breadboard and made one rail positive and one negative. Annoyingly I found that the TXD and RXD pins on the Bluetooth module don't work when connected to the same pins on the arduino itself. The TXD pin on the Bluetooth module I have connected to the RXD pin on the arduino (pin 0), and the RXD pin on the Bluetooth module is connected to the TXD pin on the arduino (pin 1). The Bluetooth Module will run off 3.3v but the relay needs 5v to work, hence I have used 5 volts on the arduino.

Here is the code I have written for this 2 switch relay. As an example I have it controlling a kettle and lamp.

/*
------------------------------------------------------------------------
                            InfidelFish
------------------------------------------------------------------------
*/
String voice;
#define relay1 2    //Connect relay1 to pin 2
#define relay2 3    //Connect relay2 to pin 3
void setup()
{
  Serial.begin(9600);            //Set rate for communicating with phone
  pinMode(relay1, OUTPUT);       //Set relay1 as an output
  pinMode(relay2, OUTPUT);       //Set relay2 as an output
  digitalWrite(relay1, LOW);     //Switch relay1 off
  digitalWrite(relay2, LOW);     //Swtich relay2 off
}
void loop()
{
  while(Serial.available())    //Check if there are available bytes to read
  {
    delay(10);                 //Delay to make it stable
    char c = Serial.read();    //Conduct a serial read
    if (c == '#'){
      break;                   //Stop the loop once # is detected after a word
    }
    voice += c;                //Means voice = voice + c
  }
    if (voice.length() >0)
    {
      Serial.println(voice);
      if(voice == "*switch on"){
        switchon();
      }               //Initiate function switchon if voice is switch on
      else if(voice == "*switch off"){
        switchoff();
      }               //Initiate function switchoff if voice is switch off
      else if(voice == "*lamp on"){   
//You can replace 'lamp on' with anything you want...same applies to others
        digitalWrite(relay1, HIGH);
      }
      else if(voice == "*lamp off"){
        digitalWrite(relay1, LOW);
      }
      else if(voice == "*kettle on"){
        digitalWrite(relay2, HIGH);
      }
      else if(voice == "*kettle off"){
        digitalWrite(relay2, LOW);
      }
      voice="";
    }
}
void switchon()               //Function for turning on relays
{
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
}
void switchoff()              //Function for turning on relays
{
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
}

/*

You can add any function you want depending on how many devices you have hooked up.
For example you could have a function called 'cinema' which would dim the lights and
turn the TV on. You can have as many as you have pins on your arduino.
For my relay 'LOW' turns off and 'HIGH' turns on
The outline to follow is this:
   
   void ......()
   {
     digitalWrite(...., LOW/HIGH);
     digitalWrite(...., LOW/HIGH);
   }
   
*/

When you upload the code to your Arduino, make sure you unplug pins 0 and 1 otherwise you will probably get this error:

avrdude: stk500_getsync(): not in sync: resp=0x00

Now on your android phone download this brilliant app by SimpleLabs!

https://play.google.com/store/apps/details?id=robotspace.simplelabs.amr_voice&hl=en

Connect to the Bluetooth module it will probably be called something like 'HC-06'

The first time it will ask you for a password which is usually 1234.

Once you have connected say the commands you have chosen in the code and hopefully the relay will switch on and off!

Step 4: Adding the Appliances

To quote Brick from Anchorman 'I love Lamp!'

So the first thing to do is find and old lamp or other appliance as we are going to be cutting the lead.

Now remove the outer rubber sleeving making sure not to cut the sleeving of the wires inside, we don't want any live wire showing! Next cut the positive wire (the red one) and remove a few millimetres of sleeving on each end. Insert the exposed wire in the correct ports of the relay as shown in the diagram and pictures above. MAKE SURE THERE IS NO WIRE VISIBLE! For safety wrap the wires in electrical tape, and cover any of the places that could be live on the relay, such as the screws.

I am not responsible for your safety, do this at your own risk!

Once this is done we are ready to power on the lamp and test the system. XD

Step 5: Conclusion

Hope that was informative and you were successful.

Electricity is dangerous so please be careful!!!!

The possibilities for this I feel are endless you can add loads of devices and then create functions in order to control several at once.

Here is a video of the lamp working:

Thank You :)

<p>can i get code for a 8 relay module plz?</p>
<p>Is the power outlet connected to the relay with the lamp? i don't think i'm fully understanding that part.</p>
<p>im telling to amr voice &quot;Включи свет&quot;, in programm i wrote &quot;Включи свет&quot;, but relay module does nothing, im stock at step#3 can u help me plz</p>
i made it<br>
<p>welldone.</p><p>send me the code.</p>
<p>Hope it is working well for you.</p>
yup thanks<br>
<p>Small problem the device it will switch on and off every now again without any reason ???? please help </p>
<p>sir pls give me codes for just copy padt for 2 channel</p><p>It not success</p>
<p>i tried my best but was not able to controll my new bt module hc 05 .</p><p>i had programmed it but when i say command it is not working can u please sent me some video help</p>
<p>Please detail your problem</p>
<p>Hello, thank you for the instructable! Very awesome. I modified your code to work for my 8 channel relay and it worked great. Is there any way instead of using voice to use buttons on the android app? Because the thing I found to be tough is some of the words my device didn't recognize right away. So of course I renamed them until it was easy for my device to recognize the words the first time. Also to let others know on my 8 Channel relay board with all the relay engaged my power supply was reading 565 ma of power. So I used a separate power supply for my 8 channel relay board and it worked great. Also I used an arduino nano so it would be smaller over all. Later I am going to put the whole thing in a power strip so I have bluetooth control over it. Thanks again.</p>
<p>Hello .<br>I want to connect with coffee maker and also music player.<br>So do this code work for you ?<br>I really wanna try this................</p>
is there any way to change the bluetooth device password ?
<p>AT+PSWD=password</p>
I have tried to do relay test but I couldnt suceess. Can you help me on this. I have done qiring as per the drawing. I have connect the USB to my laptop to power up the arduino. Should I use any other power source?
<p>I think you need power for relay board. Check the datasheet for your relay board.</p>
I have tried to do relay test but I couldnt suceess. Can you help me on this. I have done qiring as per the drawing. I have connect the USB to my laptop to power up the arduino. Should I use any other power source?
<p>nice work!!!!!</p><p>can app inventor app will be compatible with this project, instead of using simplelab app</p>
<p>I haven't tried it but you may be able to get it to work - keep me updated.</p>
<p>I'd be interested in knowing you made any progress developing your own app to control this project.</p>
<p>Thanks for the great project. Excellent work on explaining the setup. I've had no problems and plan to build it into a permanent case for around the house use. Also plan on expanding to four or more relays. Here is a link to the video showing it in operation. </p><p><a href="https://youtu.be/nTSdYoxaucg" rel="nofollow">https://youtu.be/nTSdYoxaucg</a></p>
<p>hey. i have a doubt. How can i connect the appliences to the elements.Iam not exactly aware about that.i need an exact information about this project.can you please help me .my mail id is hareesh.mahindran@gmail.com</p>
<p>excelent your proyect!!! congratulations!!!!! woooww!!!</p>
<p>It's work good job.</p>
<p>followed exact same steps as shown by you but one problem is my relay board passes the relay test but the switching isnt happenning(no sound is heard) only the led is glowing.</p>
<p>hello</p><p>can i make this same project...but to work with buttons on the smartphone not with voice?</p><p>PLEASE RESPOND******</p><p>Thanks</p>
Thank you so much. <br>It's my first project with arduino uno r3.<br>Can i use you project with 9V battery?<br>Also i would like to have an application for my Android as on / off buttons not with voice. <br>Thank you again.
<p>Awesome that you made it!</p><p>I agree with @ArifSae that you don't want to run off 9V. 5V power is what you really want just use USB or you can get 5V power supplies. :)</p><p>If just search the Google Play store for Bluetooth Switch applications many will come up which are suitable for this project.</p>
<p>Please can you find me an application because I try some but didn't work. send me a link or something like that if you ca. Thank you again.</p><p>I would like 5V but battery cause i would like to take it with me and use it. <br>That's why i ask you. cause 9V battery it's easier to connect to arduino and don't take so much space.</p>
<p>Awesome that you made it!</p><p>I agree with @ArifSae that you don't want to run off 9V. 5V power is what you really want just use USB or you can get 5V power supplies. :)</p><p>If just search the Google Play store for Bluetooth Switch applications many will come up which are suitable for this project.</p>
Please can you find me an application because I try some but didn't work. send me a link or something like that if you ca. Thank you again.<br>I would like 5V but battery cause i would like to take it with me and use it. <br>That's why i ask you. cause 9V battery it's easier to connect to arduino and don't take so much space.
<p>powering arduino with 9v battery will be very expense in long run. as battery would only last one day. either use wall wart / usb (because half of power is wasted in rgulation i.e. converting 9v to 5v required by the controller chip</p>
<p>Dear Friends,</p><p>In first place, nice project!!! In second place, what specific library of the Fritzing do you use to draw the schematic? I couldn't find the symbols that you have used.</p>
<p>Really nice project, I love it.</p><p>Is possible you send any example how you make the app? I would like to do for more lamp and not only with one. (I don't know programming, so maybe with one example I can work with). Thank you again :)</p>
<p>Ok well it might be challenging to write an app with no programming experience, but if you want to start somewhere I would recommend using Processing &quot;https://processing.org/&quot;. This is very simple to hook up to the arduino via serial and the ide is very similar. Here you can start writing some nifty little programs for controlling multiple appliances ;) hell you would even throw in something cheeky like a kinect hehe. Hope that helps :) WoOp</p>
<p>frnds,i am working on this prototype .but i got struck on how to connect the lamp and the relay circuit?also how to connect the lamp to power supply?pls help me frnds.</p>
<p>Hi there LokkeshR!</p><p>First off connecting the lamp to the relay. A relay is <strong>basically</strong> a switch which is turned on and off not by you clicking it, but by providing a voltage through a coil in order to magnetically flick the switch. So it just really needs to be wires like a normal switch. So cut the negative wire and attach each of the ends of the wire you have now cut to the ground and IN screw terminal blocks as shown in the wiring diagrams above. </p><p>The lamp is just powered from the mains, using an ordinary plug and ordinary socket. </p><p>Another option which I will endeavour to upload to this instructable in the future is to use 'radio controlled' plugs and then wire the remote up to the relay. This saves you cutting into the high voltage cable from the lamp and is a lot safer! I will upload an instructable on this hopefully in the near future!</p><p>Hope that was helpful :) Good Luck!</p>
Yes sir ,I had got it.I had gave all the connections properly ,but the prototype was not correctly working .I don't know why? But friends told they for the function serial.port() we had not included the library function.so is the serial.port() is library or user defined function?
<p>Don't worry with the Arduino you do not need to import a library for the serial communication. Your friends may be thinking of 'Processing' (which is what the Arudino IDE is partly based off) where you do have to import a library for serial communication.</p>
<p>Thank for making this useful project.You can see my work from the link below.</p><p><a href="https://www.instructables.com/id/SMART-POWER-OUTLET/" rel="nofollow">https://www.instructables.com/id/SMART-POWER-OUTLET/</a></p>
<p>Fab! Glad you found it useful. Loving your work there - sweet job :)</p>
<p>Thanks :)</p>
<p>Hi,</p><p>Very nice project.</p><p>Which software you are using to draw the schematics ?</p>
<p>Hey! Thanks :)</p><p>The schematics for the project are drawn using Fritzing</p><p><a href="http://fritzing.org/home/" rel="nofollow">http://fritzing.org/home/</a></p>
<p>hi... I have another problem. I have used a solid state relay instead of a normal one. So I wanted to know the connections using that one. Can you Please help me with that? I think my connections are incorrect, hence I wasn't getting the output. Also please explain me how to use the AMR_voice app. I am unable to configure it.</p><p>Thank you!</p>
<p>hey! I found this very interesting and keen in making this. but i didnt understand one thing. you said that you are going to be cutting the lead of the lamp, then how will you connect it to 220volts?<br>please explain this as soon as possible.<br><br>thank you in advance!</p>
<p>Hey shraddhaagadi, glad you found it interesting!</p><p>As a relay is basically a switch which is turned on and off by a electro-magnet not your finger ;) ... you want to wire it into the circuit in the same way.</p><p>Going to the lamp is the live wire, and from it the neutral, you just want to insert the relay into this loop...so just cut the live wire and put the relay in the gap (so it is now between the plug and the lamp itself). This means when the relay is switch it is both making and breaking the circuit - hence turning the lamp on and off. </p><p>I hope that helped! :) Got anymore q's feel free to ask</p>
<p>hey ! can i use hc-05 instead of hc -06 ?? </p>
<p>Hi, yes you should be able to, here is an instructable on using the HC-05 with an arduino:</p><p> <a href="https://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/?ALLSTEPS" rel="nofollow">https://www.instructables.com/id/Arduino-AND-Blueto...</a></p><p>Good luck, shout if you have any problems XD</p>

About This Instructable

95,969views

268favorites

License:

Bio: Student studying Electronic and Information Engineering at Imperial College London
More by Imperial Polygon:Kinect SDK Hello World Arduino Home Automation (Bluetooth) 
Add instructable to: