Arduino Bluetooth Basic Tutorial

Arduino Bluetooth Basic Tutorial

Ever thought of controlling any electronic devices with your smartphone? Controlling your robot or any other devices with your smartphone will be really cool right.S Here is a simple and basic tutorial for interfacing Bluetooth with Arduino

Things you need

  1. Bluetooth Module HC 05/06
  2. Arduino
  3. LED
  4. 220Ω Resistor
  5. Android device

How Does it Work??

HC 05/06 works on serial communication. Here the android app is designed to send serial data to the Bluetooth module when a button is pressed. The Bluetooth module at the other end receives the data and sends it to Arduino through the TX pin of the Bluetooth module(RX pin of Arduino). The Code fed to Arduino checks the received data and compares it.
If received data is 1 the LED turns on turns OFF when received data is 0

Code

We will upload the program first because you will receive an error for sure if you try to upload the sketch after making the connection. The reason is that the TX and RX pins are used while uploading the program to the microcontroller. You can see the TX RX LEDs near the onboard LED blinks continuously at the time of uploading the program.

/*
* Arduino bluetooth basic interfacing program
* Coder - Mayoogh Girish
* Website - www.mgprojecthub.com 
* Download the App : https://github.com/Mayoogh/Arduino-Bluetooth-Basic
* This program lets you to control a LED on pin 13 of arduino using a bluetooth module
*/
char data = 0; //Variable for storing received data
void setup()
{
    Serial.begin(9600); //Sets the baud for serial data transmission                               
    pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
   if(Serial.available() > 0)  // Send data only when you receive data:
   {
      data = Serial.read();        //Read the  incoming  data and store it into variable data
      Serial.print(data);          //Print Value inside data in Serial monitor
      if(data == '1')              // Checks whether value of data is equal to 1
         digitalWrite(13, HIGH);   //If value is 1 then LED turns ON
      else if(data == '0')         //  Checks  whether value of data is equal to 0
         digitalWrite(13, LOW);    //If value is 0 then LED turns OFF
   }
}

Code Explanation

char data = 0;

Initially, we created a variable called data to store data received from the Bluetooth module.

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

Inside setup() Serial communication is initialized with a baud rate of 9600. Since the HC-05/06 Bluetooth module communicates to Arduino through serial communication.
pinMode(13, OUTPUT); sets digital pin 13 as OUTPUT pin. Since we are using a LED, which is an output device.

   if(Serial.available() > 0)  
   {
      data = Serial.read(); 
      Serial.print(data); 

The Serial.available() function returns the number of characters available for reading from the serial port.
If the value is greater than 0, which means it’s receiving Serial data.
Then the received characters are stored in the variable data.

 if(data == '1')              
         digitalWrite(13, HIGH);  
      else if(data == '0')        
         digitalWrite(13, LOW); 
   }
}

If the received data equal to 1 then digitalpin pin 13 is made HIGH and made LOW when data equal to 0

Connection

The connections are quite simple, all you have to do is just 4 connection

Arduino Pin Bluetooth Pin
RX (Pin 0) TX
TX (Pin 1) RX
5V VCC
GND GND

Connect the positive leg of the LED to pin 13 through a resistor (220Ω – 1KΩ) and negative to GND pin. The bluetooth module connection will be done after uploading the program to the board.

Note: The connection is not RX to RX and TX to TX.
TX of bluetooth module goes to RX of arduino and vice-versa.

Output

Open the serial monitor and watch the received data

Android Application

In this tutorial, I will not be covering tutorials on android app development. You can download the android application from here and the source code of the entire project

Watch in the video how to pair to the Bluetooth module

  • Download the Application from here or here
  • Pair your device with HC 05/06 Bluetooth module
    1) Turn ON HC 05/06 Bluetooth module
    2)Scan for available device
    3)Pair to HC 05/06 by entering default password 1234 OR 0000
  • Install  LED application on your android device
  • Open the Application
  • Press paired devices
  • Select your Bluetooth module from the List (HC 05/06)
  • After connecting successfully
  • Press ON button to turn ON LED and OFF button to turn OFF LED
  • Disconnect button to disconnect from the Bluetooth module

This is just a basic tutorial on interfacing Bluetooth  module with Arduino
This project can be improved to a higher level like Home automation using a smartphone, Smartphone controlled robot, and much more.

45 thoughts on “Arduino Bluetooth Basic Tutorial

  1. hi I have 1 problem . I cant upload the code it a message like this :

    Arduino: 1.6.13 (Windows 10), Board: “Arduino/Genuino Uno”

    Sketch uses 1,756 bytes (5%) of program storage space. Maximum is 32,256 bytes.
    Global variables use 187 bytes (9%) of dynamic memory, leaving 1,861 bytes for local variables. Maximum is 2,048 bytes.
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x99
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x99
    Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

  2. Hi,

    Could you also publish your Android code? I want to do a similar application and your app would be a good starting point.

    Thanks!

  3. When I push the “ON” and “OFF” buttons using your app, I see the character “ÿ” in the serial monitor screen. I’ve tried a couple other apps and still get that same character when using the buttons. Any ideas? I’m using an Arduino Uno and an iteadstudios bluetooth module v2.2 alongside a Galaxy S7 for app.

  4. First of all, thanks so much Mayoogh, this was super helpful. For expanding this, do you know how to connect multiple HC-05s to a single Android?

    1. It’s a very nice project……also it run successfully……but it will be great if you tell us how to control more than one led….. waiting for your reply…..

  5. I have a problem. When i try to switch the led on by my phone i have short blink on TX led and RX led and thats all, but its okay when i try to switch the led on by monitor port.
    What’s wrong with it?

  6. I have a question how do you use the resistor in the video I can’t see where did you use the resistor. I have 220-ohm resistor where do i have to plug the resistor can you please help me.
    thankyou

  7. When I push the “ON” and “OFF” buttons using your app, I see the character “ÿ” in the serial monitor screen.
    I have tried many times..Even baud rate is also same 9600 in serial monitor and code. Could u please tell me what to do for?

  8. Hi, Thanks for the tutorial but I having problem.. I can get connected with hc-05 but arduino doesnt receiving and data

  9. Hi Mayoogh I can’t paired the bluetooth, I’m using a SH-BT-BOARD V1.3 bluetooth, Can you help me please? It’s a great project, and I’m learning with. Thank you

  10. Thanks Alot….
    Very Helpful For My Science Exhibition Project…..
    I used ur code to control my relays….
    Urs was d only website that had a HC-05 config that was absolutely right….

  11. Programming HC-06 is very easy. There are already a lot of Sample programs and projects in the Internet. Why didn’t you include the tutorial of the program? Isn’t it your program? In you Hackster.io post of this, you said the tutorial for the Android App can be found here. That is why I’m here to see the App program code so I can learn from it. Don’t you think its wise you Post it here? I expect you will. Regards.

  12. What I mean of the program is the Android App program that I expect you posted the code in here. It’s just disappointing I came from Hackster.io and found link there of this page just to find out the Android App tutorial is not Posted, only the user’s tutorial guide.

  13. Sorry i’m a french student and when i run your script this appear “Connection Failed. Is it a SPP Bluetooth? Try again”. What should i do?

Leave a Reply

Your email address will not be published. Required fields are marked *