Find fresh tutorials related to Arduino, Raspberry Pi, 555 Timer and many more microcontrollers. Learn something new everyday!



Showing posts with label Wireless. Show all posts
Showing posts with label Wireless. Show all posts

Saturday, 10 January 2015

Using HC-05 Bluetooth Module - Control an LED with an Android phone [ARDUINO]



So, you saw how we used a PIR motion sensor to create a simple motion detecting mechanism within a short span of time. We also saw how to use simple sensors like HC-SR04HC-SR501 and DHT11. We even controlled LCD and 7-segment display. But, the major inconvenience was - wiring. Just think of it. You make a motion detector, wire up all the components and then what, connect an LED in your room with around 5 to 6 meters to the setup on your staircase? Oh, hell no! In situations like these, the word: "wireless" comes to our mind, and why shouldn't it? Everything around us is more or less, wireless! Our phones, wifi, etc are major examples. You can even lock your car with a tiny wireless remote. We all know stuff like these.

But, to make your arduino project wireless, you have a lot of technologies at your disposal.
Major wireless techniques, that could be used with arduino projects and let you go wireless:
1. Bluetooth
2. FM transceiver
3. Infrared
4. WiFi

Now, let's briefly discuss these technologies:

1. Bluetooth: 

    Simple, elegant, cheap. Has a pretty good range. Less complicated. Easy to connect. A large spectrum of devices use Bluetooth.

2. FM transceiver:

    Excessively cheap. Pretty great range (90 meters). Requires two standalone arduinos(One for receiving, One for transferring data)
    But, huge chances for interference. Legal issues.

3. Infrared:

   Cheap, short range. Less effective, not secure. Can't send different signals.

4. WiFi:

   A bit expensive. Good range. A bit complex. But, convenient.

Don't worry, slowly but steadily, we'll use all of those, but first, lemme take a selfie! Just, kidding.
But first, let's use bluetooth in our projects.

Let's know the basics:

Pinouts of my HC-05 module

HC-05 module is an easy to use Bluetooth SPP (Serial Port Protocol) module, designed for
transparent wireless serial connection setup.
Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps
Modulation with complete 2.4GHz radio transceiver and baseband. It uses CSR Bluecore
04-External single chip Bluetooth system with CMOS technology and with AFH(Adaptive
Frequency Hopping Feature). It has the footprint as small as 12.7mmx27mm. Hope it will simplify
your overall design/development cycle.

The HC-06 module is similar in design to the HC-05, but, it can't act as a master device. The HC-05 can behave both as master and slave.

We'll also use this library: Software serial

What this project does?
Demonstates the simple use of HC-05 bluetooth module. With this, you can simply use your android phone to turn an LED ON or OFF, via an android app.

Materials Required:
Click for clarity

1. An HC-05 or HC-06 Bluetooth Module
2. Arduino
3. Jumpers
4. One LED
5. One 220OHM resistor for LED
6. Breadboard
7. and of course, a PC with Arduino IDE installed.
8. Oh yes, an Android phone too!


Procedure:

1. Upload the sketch given below.
2. Then remove the arduino and wire up the components as shown below.




NOTE: PLEASE REMEMBER TO CONNECT THE RX OF HC-05 TO SOFT TX[PIN 11] ON ARDUINO AND VICE VERSA.


3. Power on the Arduino.
4. Download and install the app on your phone: [DOWNLOAD THE APP]
5. Turn on the BLUETOOTH in your phone and tap on Scan.
6. You'll notice your HC-05 or HC-06 name on the list of found devices.
7. Tap on the module's name and pair with it. The default pairing code is 1234 or 0000
8. Now, open the HC05_ARDUINO_LED app.
9. Tap :"Connect to Module"
10. You'll get a screen, where the nearby bluetooth devices' names and addresses will be listed.
11. Tap on the device(HC-05 or HC-06)
12. You'll notice that the led on HC-05 will stop blinking randomly and will blink once in every 4 seconds
13. Now, tap on ON to turn on the LED or tap on OFF to turn it off.
14. Done. Simple. :)

Sketch:

/* 
 * ON PUBLIC DOMAIN
 * Author         : Nitish Dash
 * Name           : LED control with HC-05 and android
 * Created        : 1/10/2015
 * Webpage        : http://goo.gl/u2d0Cv
 * Author Email   : nitishdash95@gmail.com
 * Author Website : http://www.nitishdash.com/
 **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS ****
 */

#include 
SoftwareSerial electro(10, 11); // RX, TX
int l1=12; 
int flag=0;
String bdata="";
char c=' ';

void setup()
{
  electro.begin(9600);
  pinMode(l1, OUTPUT);
}
void loop()
{
  while (electro.available()){
    c=electro.read();
    bdata+=c;
if (bdata=="on")
    {
      flag=1;
    }
    if (bdata=="off")
    {
      flag=0;
    }  
}
  digitalWrite(l1, flag);
  delay(500);
  bdata="";
}
NOTE: Please remove the line 42 before uploading the sketch.

How this works (Algorithm):
We used Software Serial library, which converts any digital pins into serial pins RX and TX. That means, your 0 and 1 digital pins are free to be used. Now, I created the android app in such a way that, when you press the ON button, it will send a string value "on" to the HC-05 module. Same is the case of "off" button. Now, using if-else, I turn the LED on and off. At the end, the string is cleared for next iteration. 

Video:
Coming soon.....


Downloads:
1. sketch.ino
2. HC-05 datasheet
3. HC-06 datasheet
4. HC-05_ARDUINO_LED  app 

Something for you!
Try these ideas to enhance your knowledge and test yourself:
1. Replace the LED with a buzzer.
2. Try adding 3 different colored LEDs.

In the upcoming tutorials, we'll get more advanced and shift up our level! The HC-05 is a wonderfully good device, which will surprise you till the end. Just think of the possibilities! Controlling any device in your home, for example, you could easily automate your TV, A/C, XBOX, etc from the comfort of your couch!


Upcoming projects!

1. Programming ESP8266 using NodeMCU and LUA Scripts

2. IoT Applications

3. NRF42L01 2.4 GhZ module interfacing with arduino

4. ESP8266 - Everything cool about it!

Language ain't any barrier!

Get Updates!

Enter your email address:


Get the latest projects delivered into your inbox, hot!