Introduction: Control Leds With Arduino and Bluetooth

About: Android app developer

Most people carry their phones with them almost everywhere they go,making it convenient to control the things around them.That's why in this tutorial you will learn how to use your Android phone to control your Arduino projects.You can be creative and use your phone to control anything you want! but we are just going to start off with controlling a couple of Led's.

Lest get started!

Step 1: Parts List

Here is what you need to control Led's with Bluetooth:

  • Arduino
  • HC-05 Bluetooth module
  • Solder less breadboard
  • 3 Led's
  • 3 220Ω resistors
  • Wires
  • Most importantly your phone and a downloaded Bluetooth app(I recommend using the Arduino Bluetooth Controller which offers many different features)

Step 2: Circuit

Bluetooth module connection

  • connect the BT module's Rx pin to pin 11 on the Arduino
  • connect the BT module's Tx pin to pin 10 on the Arduino
  • connect up the Gnd and Vcc (5v) to the Arduino

Led's connection

  • connect all the cathodes(short pin) of the led to Gnd
  • connect each anode to a 220Ω resistor
  • connect a resistor to Arduino pin 2,3 and 4

If the led on the Bluetooth Module is blinking quickly then it is ready to pair to your phone,if not then check your connections

Circuit Complete! now lets move on to the code

Step 3: Arduino Code

#include<SoftwareSerial.h>
SoftwareSerial bluetoothSerial(10, 11); // Tx pin of the bluetooth module must be connected to Rx pin on arduino
                                         // Rx pin of the bluetooth module must be connected to Tx pin on arduino
const int led1=2;
const int led2=3;
const int led3=4;
String inputString;
String buttonId;
int buttonState=0;
int pinNum=0;
void setup(){
      //set the digital pins
      pinMode(led1,OUTPUT);
      pinMode(led2,OUTPUT);
      pinMode(led3,OUTPUT);
      //start communication with bluetooth module
      bluetoothSerial.begin(9600);
      //set all pins to off as default to start with
      digitalWrite(led1,LOW);
      digitalWrite(led2,LOW);
      digitalWrite(led3,LOW);
      
        
  
}
<p>void loop(){<br>  if( bluetoothSerial.available()){
      while( bluetoothSerial.available()){
         char inChar = (char) bluetoothSerial.read(); //read the input
         inputString += inChar;        //make a string of the characters coming in serial
        
      }</p><p style="margin-left: 20px;">	//if it ends with "/#" it is the end of the command</p><p> if(inputString.endsWith("/#")){  
         inputString = inputString.substring(0,inputString.length()-2); //get rid of the "/#" at the end
    
         for(int i=0;i<inputString.length();i++){       //split to get id and state</p><p>           if(inputString.charAt(i)==','){
               buttonId = inputString.substring(0,i);  //get the id of the button</p><p style="margin-left: 20px;">	//get the state of the button ,if its 0 switch it off and if its 1 turn it on</p><p style="margin-left: 20px;">	 buttonState = inputString.substring(i+1).toInt();
           }
           
    }
        
         inputString = "";  //reset the input data for the next time
          
}</p><p style="margin-left: 40px;">	//get pin number and then convert the string to an int</p><p style="margin-left: 60px;">   pinNum =  buttonId.substring(1).toInt(); 
            digitalWrite( pinNum,buttonState);
        
      }</p><p>  }</p>

Step 4: Download Bluetooth App for Phone

Download it here

Step 5: Bluetooth Communication With Arduino

Make sure that the led on the Bluetooth module is blinking quickly which means its ready to connect.

Turn on your Bluetooth on your phone and make sure your Bluetooth module is paired with your phone. then go into the Arduino Bluetooth Controller and click the "Connect" button, if it is connected with your module then the led on the Bluetooth module will blink every 2 seconds and the button in the app will now say "Disconnect" Once it is connected you can click on the "Digital Toggle" option to display the toggle buttons. Well done you have now managed to control a led via Bluetooth with your phone!