Saturday 31 December 2016

CONTROLLING AN LED LIGHT USING BLUETOOTH MODULE HC-06

*Wouldn't you love to switch on and off the lights in your house without having to raise a finger? (well, technically you would have to raise a finger or two, but you get the jist of the story). The good news is that you can. All you needs is an arduino, HC-06 bluetooth module, some relays and a little electrical know-how.
In this tutorial, however, i am going to show you how to setup the HC-06, send some data to the arduino and receive and echo and control an LED via bluetooth. At least this should get you started, before you get to control the whole grid with your HC-06...just kidding.
Add bluetooth to your Arduino project - Arduino+HC-06


THE ITEMS REQUIRED FOR MAKING THIS PROJECT

1.Bluetooth module 
2.Arduino Uno
3.Jumper wires
4. LEDLight

Step 1: Setup

Setup
Setting up the HC-06 is as easy as ABC. All you need to know is the pin configuration. The HC-06 has 6 pins: wakeup, VCC, GND, TXD, RXD and State. Right now I will only deal with 4 pins, which are VCC, GND, TXD and RXD.
Here is how you should connect the Bluetooth module to your Arduino.
HC-06>>>Arduino
VCC>>>>3.3v
GND>>>>GND
TXD>>>>RXD
RXD>>>>TXD
The HC-06 acts as a serial port through which you can send and receive data. So using a serial terminal or a Bluetooth customized application on your computer or phone, you can control and monitor your project. I used Teraterm as the serial terminal.
Before, uploading the code to the Arduino, disconnect the HC-06 module, since it shares the tx/rx pins and will interfere with the upload. Connect it back once the code has been uploaded successfully.
hc-06.png
char blueToothVal;           //value sent over via bluetooth
char lastValue;              //stores last state of device (on/off)

void setup()
{
 Serial.begin(9600); 
 pinMode(13,OUTPUT);
}


void loop()
{
  if(Serial.available())
  {//if there is data being recieved
    blueToothVal=Serial.read(); //read it
  }
  if (blueToothVal=='n')
  {//if value from bluetooth serial is n
    digitalWrite(13,HIGH);            //switch on LED
    if (lastValue!='n')
      Serial.println(F("LED is on")); //print LED is on
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='f')
  {//if value from bluetooth serial is n
    digitalWrite(13,LOW);             //turn off LED
    if (lastValue!='f')
      Serial.println(F("LED is off")); //print LED is on
    lastValue=blueToothVal;
  }
  delay(1000);
}


No comments:

Post a Comment