Bluetooth Basics: How to Control an LED Using a SmartPhone and Arduino

By Mayoogh Girish

arduino bluetooth

 

Ever thought of controlling any of your electronic devices with your smart phone? How about a robot or any other device? Wouldn’t it be cool to control them with your smartphone? Here is a simple tutorial for interfacing an Android Smartphone with Arduino via Bluetooth!

Required Materials

arduino bluetooth

Hardware

  1. Bluetooth Module HC 05/06
  2. Arduino & Battery (with cable)
  3. LED
  4. 220Ω Resistor
  5. Android device

Software

  1. Arduino IDE
  2. Android Studio.

 

Android studio isn’t really required here since I will provide you with the android application that I made. You can install the given .apk file to use the application. :)

How Does it Work?

There are three main parts to this project. An Android smartphone, a Bluetooth transceiver, and an Arduino.

 

arduino bluetooth

 

HC 05/06 works on serial communication. The Android app is designed to send serial data to the Arduino Bluetooth module when a button is pressed on the app. The Arduino Bluetooth module at other end receives the data and sends it to the Arduino through the TX pin of the Bluetooth module(connected to RX pin of Arduino). The code uploaded to the Arduino checks the received data and compares it. If the received data is 1, the LED turns ON. The LED turns OFF when the received data is 0. You can open the serial monitor and watch the received data while connecting.

Connecting the Arduino Bluetooth hardware

 

arduino bluetooth

This circuit is simple and small. There are only four connections to be made between the Arduino and Bluetooth module!!

 

Arduino Pins     Bluetooth Pins

RX (Pin 0)     ———>     TX

TX (Pin 1)     ———>      RX

5V                 ———>      VCC

GND             ———>      GND

 

Connect a LED positive to pin 13 of the Arduino through a resistance (valued between 220Ω – 1KΩ). Connect its negative to GND, and you’re done with the circuit!

 

arduino bluetooth

You can connect the Bluetooth module to the Arduino using a set of jumper wires and a connector.

 

Note: Don’t  Connect RX to RX and TX to TX on the Bluetooth and Arduino. You will receive no data. Here TX means Transmit and RX means Receive.

Uploading the Sketch to Arduino

 

char data = 0;                //Variable for storing received data
void setup() 
{
  Serial.begin(9600);         //Sets the data rate in bits per second (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
    Serial.print("\n");        //New line 
    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
  }                            
 
}

Upload the given sketch to Arduino using the Arduino IDE software, you can also get it from Github.

Installing the Android Application

 

arduino bluetooth

 

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

  • Download the Application from Amazon App Store or Github
  • Pair your device with the HC 05/06 Bluetooth module:
  • 1. Turn ON the HC 05/06 Bluetooth module by powering the Arduino.
  • 2. Scan your smartphone for available devices.
  • 3. Pair your smartphone to the HC 05/06 by entering default password 1234 OR 0000.
  • Install the LED application on your Android device.
  • Open the Application

 

arduino bluetooth

 

  • Press paired devices
  • Select your Bluetooth module from the List (HC-05/06)

 

arduino bluetooth

 

  • After connecting successfully, press the ON button to turn the LED on and the OFF button to turn the LED off.
  • Disconnect the button to disconnect frBluetoothoth module.

 

arduino bluetooth

Here is how it works!!

 

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 robots, and much more!

You can watch the video tutorial below:

 

 

 

 

 

Recommended Posts
Showing 21 comments
  • Krupesh Anadkat
    Reply

    thanks, it worked. app had super simple interface.. thanks a lot.

  • vrijkocdh
    Reply

    Serial.available always return 0
    Please, Need Help

  • rCr_BD
    Reply

    Thanks for this nice Tutorial. every things works great for me. just have to supply 3.3V at RX pin of the module.

  • Mostafa
    Reply

    PLease can u upload the code belong to android app if it available !!

  • Sanjay
    Reply

    there are many arduino uno board can you please suggest exact link to buy the same. Also for HC05/06 module so that I can buy directly from there and create this demo to demonstrate to my colleagues.

    • Ayan Mukherjee
      Reply

      Sanjay, It doesn’t depend on the arduino board you are using, You need to check the microcontroller whether it is an Atmega328-P and just just generalise the pin no. As shown here.Thats it

  • Dongksi
    Reply

    hi can I ask? is that possible that I will change the code “if(data == ‘1’) ” to if(data == ‘on’) ? do I have to change the source code of the app?

  • mohd azhan hj hashim
    Reply

    how to program coding for control 5 led using bluetooth…?

  • Mike
    Reply

    dont you need logic level shifter on RX pin on bluetooth from 5v to 3v?

    • mike
      Reply

      never mind, saw answer already.

  • Amit
    Reply

    Is it possible to connect the led to HC 05 PIO pin and control the same ping via mobile application ?

  • anas
    Reply

    Please guide me to send a data from arduino to phone

    • Mayoogh Girish
      Reply

      Bluetooth uses serial communication

      Serial.print(5);
      The above statement will send 5 from arduino

  • Alex Vasilas
    Reply

    Hello.
    I have a problem.
    I upload the script my arduino and I take out error.

    Arduino: 1.6.11 (Windows 7), Board: “Arduino/Genuino Uno”

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

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

    • Mayoogh Girish
      Reply

      Did you connected the TX RX pins of module to arduino board while uploading ? If so there is a chance of error , try after removing the connection to TX RX pins

  • Zernan Hernandez
    Reply

    please provide code of the android application.
    I really want to know how to send data and communicate from android to arduino.

  • sheshan kumar
    Reply

    How to create code and upload code in arduino for bluetooth interface please provide a video.

  • Amal Shajan
    Reply

    Sholud have used an voltage divider biasing to conver 5v arduino logic to 3.3v. It is more auitable for HC 05.

    • Spartan Velanjeri
      Reply

      AmalShajan

      Voltage divider biasing need not be required for small projects like this. However it may be needed when you are transmitting and receiving large amount of data

Leave a Comment

− 6 = 4

Start typing and press Enter to search