Arduino Home Automation (Bluetooth)

135,002

320

78

Introduction: Arduino Home Automation (Bluetooth)

About: Student studying Electronic and Information Engineering at Imperial College London

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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

Home Technology Contest

Second Prize in the
Home Technology Contest

Remote Control Contest

Participated in the
Remote Control Contest

Epilog Challenge VI

Participated in the
Epilog Challenge VI

3 People Made This Project!

Recommendations

  • Backyard Contest

    Backyard Contest
  • Silly Hats Speed Challenge

    Silly Hats Speed Challenge
  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge

78 Discussions

0
shubhamg140
shubhamg140

2 years ago

What is the meaning of # in the line of code

if (c == #){

break;

}

0
bjrnhnnngssn
bjrnhnnngssn

Reply 1 year ago

It means that 'if c is equal to #' then...

0
tannenba
tannenba

1 year ago

1st, the voice control app was hard to find, and this doesn't work??? The relay test goes fine and I can use another app to a sketch that uses a 1 or 0 and it works, so the hardware is fine, but voice control, doesn't work. I had to get the voice app from aptoide, is it the correct app? tried the Arduino voice app also, but it also doesn't work. I'm using a HC-05 and can see pulses on the receive line to the Arduino. I'm using a Blue pill which is a STM32 not a ATMEGA as the Uno so is that it?

0
XymparacheP
XymparacheP

Question 1 year ago on Step 5

I'm having a problem with connecting a Bluetooth to my tablet are ther any configs need to be done before

0
shubhamg140
shubhamg140

Question 2 years ago

what is the meaning of '#' in the coding in Bluetooth coding in line

if(c == '#'){

break;

}

0
AnubhabG
AnubhabG

3 years ago

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

0
FarazA30
FarazA30

Reply 2 years ago

for 12 volt relay you have to connect the relay with power source not a battery

0
FredrickM11
FredrickM11

2 years ago

Relay wont turn on when I use the app, I followed the instructions step by step & my relay has two led lights on as showed in your picture, any help?

0
FarazA30
FarazA30

Reply 2 years ago

check the Rx and Tx slots: the Rx of Bluetooth module should be connected with the 10th slot of Arduino and Tx of Bluetooth with the 11th slot of arduino.

0
SarmaN2
SarmaN2

Question 2 years ago

I got the problem with the relay that the led is blinking for 2 seconds in which you have mentioned above at the relay test.Also u have said that this problem will occur because of the connection between jd-vcc and vcc. please help me to avoid this problem.

0
FredrickM11
FredrickM11

Question 2 years ago

Relay wont turn on when I use the app, is there something wrong with the code?

0
riteshdoijode7
riteshdoijode7

2 years ago

how to control the second relay like what should we have to say

0
christian jaym
christian jaym

Reply 2 years ago

I tried to review the code. the second relay voice command will be found in the codes.

0
kiani1
kiani1

2 years ago

Any one knows that this codes works? Thanks

0
christian jaym
christian jaym

Reply 2 years ago

Hi Kiani1,

this really works, this helps me a lot.. just follow his clear instruction.. thanks for Doug Brion..

0
gobigas
gobigas

2 years ago

how to connect relay with light ...i have some doubt about connection between lamp and relay ...i made it nearly 75% ...but because of having dilemma about that i can finish it ..plz help me to complete it....

0
VishalV63
VishalV63

2 years ago

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