Thursday, November 19, 2015

Arduino Bluetooth Controller

Hello All!

This is my another blog in which I am going to show you how to connect an Arduino to your android phone via Bluetooth and then use voice commands to control high voltage devices using relays.
Electricity is dangerous so you have to be very careful in this…

Step 1: Materials that are required 

The various materials that are required in this project are : 
•Relay Module
•Wires
•LED's
•A Lamp
•Android Phone with Bluetooth
•Electrical Tape
•Wire Strippers (or knife)

Materials Required
Step 2: Relay Test

First of all we have to check if the relay is switching or not.
You can 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 of all wire up the circuit as shown in the images.Here,I have used a breadboard and made one rail positive and one negative.One thing that I found in this 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.
Now,I am showing you the code that I have written for this 2 switch relay. 

/*
------------------------------------------------------------------------
                            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 will upload the code to your Arduino,you have to be make sure that you unplug the pins 0 and 1 otherwise you will probably get an error like this:

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

Another thing that you have to do is to download the app on your android phone.You can download it from Google Playstore

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

Connect it to the Bluetooth module it will probably be called something like ‘HC-06’.
The first time it will ask you for a password.Most of the times password is 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

The first thing you have to do is to find an old lamp or any other appliances as we have to cut the lead in this.
Now,remove the outer rubber sleeving but just make sure not to cut the sleeving of the wires inside,we don’t want any live wire visible.

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.

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



Step 5: Conclusion

I just hope my blog will be informative and you will be are able to make that.

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:










No comments:

Post a Comment