Introduction: Arduino Home Automation (Bluetooth)

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

Comments

author
VishalV63 made it!(author)2017-06-15

i getting trouble in connection between relay 12V and light lamp and i need button type app not voice command

author
AnubhabG made it!(author)2017-05-09

may i use HC05, will there any changes occur in the programming....
pls any1 rply fast...its urjnt...
and I'm using 4 chnnl 12V relay board

author
NamanC12 made it!(author)2017-03-29

can i get code for a 8 relay module plz?

author
Havoc_HD made it!(author)2017-03-25

Is the power outlet connected to the relay with the lamp? i don't think i'm fully understanding that part.

author
ValeriyM made it!(author)2017-03-12

im telling to amr voice "Включи свет", in programm i wrote "Включи свет", but relay module does nothing, im stock at step#3 can u help me plz

author
Asadullah+Mughal made it!(author)2016-06-04

i made it

temp_1532821409.jpg
author
biloni made it!(author)2017-03-03

welldone.

send me the code.

author
Doug+Brion made it!(author)2016-09-19

Hope it is working well for you.

author
Asadullah+Mughal made it!(author)2016-09-19

yup thanks

author
PetarK16 made it!(author)2017-02-09

Small problem the device it will switch on and off every now again without any reason ???? please help

author
PrathameshM13 made it!(author)2017-02-04

sir pls give me codes for just copy padt for 2 channel

It not success

author
robotic+mayank made it!(author)2016-12-14

i tried my best but was not able to controll my new bt module hc 05 .

i had programmed it but when i say command it is not working can u please sent me some video help

author
AkshitA3 made it!(author)2017-02-01

Please detail your problem

author
abjurian made it!(author)2017-01-01

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.

author
babby123456 made it!(author)2017-01-31

Hello .
I want to connect with coffee maker and also music player.
So do this code work for you ?
I really wanna try this................

author
SAADK8 made it!(author)2016-09-23

is there any way to change the bluetooth device password ?

author
VINOTHKUMARS made it!(author)2016-12-23

AT+PSWD=password

author
RashminRathod made it!(author)2016-11-16

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?

1479287376762824377863.jpg
author
shahg9948 made it!(author)2016-12-13

I think you need power for relay board. Check the datasheet for your relay board.

author
RashminRathod made it!(author)2016-11-16

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?

author
AusafR made it!(author)2016-06-25

nice work!!!!!

can app inventor app will be compatible with this project, instead of using simplelab app

author
Doug+Brion made it!(author)2016-09-19

I haven't tried it but you may be able to get it to work - keep me updated.

author
Volthaus+Electronics+Laboratory made it!(author)2016-08-13

I'd be interested in knowing you made any progress developing your own app to control this project.

author
Volthaus+Electronics+Laboratory made it!(author)2016-08-13

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.

https://youtu.be/nTSdYoxaucg

DSC00019.JPG
author
hareesh+tm made it!(author)2016-07-26

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

author
informanteshop made it!(author)2016-06-14

excelent your proyect!!! congratulations!!!!! woooww!!!

author
Asadullah+Mughal made it!(author)2016-05-12

It's work good job.

author
DeepakD26 made it!(author)2016-04-27

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.

author
Follow+to+help+u made it!(author)2016-03-30

hello

can i make this same project...but to work with buttons on the smartphone not with voice?

PLEASE RESPOND******

Thanks

author
tperju made it!(author)2016-03-12

Thank you so much.
It's my first project with arduino uno r3.
Can i use you project with 9V battery?
Also i would like to have an application for my Android as on / off buttons not with voice.
Thank you again.

temp_1753064972.jpg
author
Doug+Brion made it!(author)2016-03-14

Awesome that you made it!

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

If just search the Google Play store for Bluetooth Switch applications many will come up which are suitable for this project.

author
tperju made it!(author)2016-03-15

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.

I would like 5V but battery cause i would like to take it with me and use it.
That's why i ask you. cause 9V battery it's easier to connect to arduino and don't take so much space.

author
Doug+Brion made it!(author)2016-03-14

Awesome that you made it!

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

If just search the Google Play store for Bluetooth Switch applications many will come up which are suitable for this project.

author
tperju made it!(author)2016-03-14

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.
I would like 5V but battery cause i would like to take it with me and use it.
That's why i ask you. cause 9V battery it's easier to connect to arduino and don't take so much space.

author
ArifSae made it!(author)2016-03-13

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

author
%C3%89dilusP made it!(author)2015-11-11

Dear Friends,

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.

author
danilo886 made it!(author)2015-09-22

Really nice project, I love it.

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

author
Doug+Brion made it!(author)2015-10-13

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 "https://processing.org/". 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

author
LokkeshR made it!(author)2015-09-18

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.

author
Doug+Brion made it!(author)2015-09-19

Hi there LokkeshR!

First off connecting the lamp to the relay. A relay is basically 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.

The lamp is just powered from the mains, using an ordinary plug and ordinary socket.

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!

Hope that was helpful :) Good Luck!

author
LokkeshR made it!(author)2015-09-19

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?

author
Doug+Brion made it!(author)2015-09-20

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.

author
deba168 made it!(author)2015-09-03

Thank for making this useful project.You can see my work from the link below.

https://www.instructables.com/id/SMART-POWER-OUTLET/

IMG_20150903_083900.jpgIMG_20150903_085706.jpgIMG_20150903_111915.jpg
author
Doug+Brion made it!(author)2015-09-06

Fab! Glad you found it useful. Loving your work there - sweet job :)

author
deba168 made it!(author)2015-09-06

Thanks :)

author
deba168 made it!(author)2015-08-28

Hi,

Very nice project.

Which software you are using to draw the schematics ?

author
Doug+Brion made it!(author)2015-08-31

Hey! Thanks :)

The schematics for the project are drawn using Fritzing

http://fritzing.org/home/

author
Shraddhaa made it!(author)2015-07-07

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.

Thank you!

author
shraddhaagadi made it!(author)2015-06-28

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?
please explain this as soon as possible.

thank you in advance!

author
Doug+Brion made it!(author)2015-06-29

Hey shraddhaagadi, glad you found it interesting!

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.

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.

I hope that helped! :) Got anymore q's feel free to ask

About This Instructable

101,058views

273favorites

License:

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