Introduction: Remote Controlled LED Using HC-05 Bluetooth, Arduino and Mobile Phone App
If you are looking to connect a BLE (Bluetooth 4.0) module instead of Bluetooth 2.0 module, please see my new tutorial: How to control Arduino with HM-10 (CC2541) BLE module and a mobile app.
Technology is progressing at break-neck speed, everyone of us has smart phones now-a-days, there are cool apps which let us stay in contact with our friends and family, get latest weather information or stay-updated with latest news but what's next? All of these are old-school techs by now, what is the next use of this amazing processing speed and cutting-edge communication method we carry around with us all times? If I say you can control the lights of your home, know that if heating is on? or if your mobile phone automatically shuts off your air conditioner? Ring bells?
Of course, here on Instructables, we all know these uses and using this tutorial we will build a circuit and control it through the mobile app and we will do it very rapidly, let's say you will be able to control lights of your home in under 20-30 minutes? Uh-oh, not really the lights but for brevity's sake we will be controlling an LED for now and you can add all kinds of circuitry later!
Let's start, we will need:
- A cheap and commonly available Bluetooth module known as HC-05, you can buy it from ebay with "buy it now price" starting from $4.17
- An Arduino, for this tutorial I will be using Arduino Uno.
- A solder less breadboard, 220 ohm to 1K ohm resistor and a LED
- Evothings Studio to rapidly develop our app in JavaScript
Let's go forward to the next step and start building our circuit!
Step 1: Connecting HC-05 Bluetooth Module With Arduino
HC-05 is a serial port module which makes it very easy to use. If you see the pin configuration of HC-05, there are total 6 but we only need 4 middle ones for our set-up.
- Connect VCC with 3.3V of Arduino, please do not connect it with 5V as that can cook the module
- Connect GND with any GND of Arduino
- Connect Rx pin with Tx of Arduino
- Connect Tx pin with Rx of Arduino
Now power-up the Uno using USB cable, a red light LED on HC-05 will start blinking, means we are ready to go forward to the next step!
Step 2: Connect the LED and Control It Using Arduino Serial Monitor
I often refer to this step as Arduino 101, connecting the LED with Arduino on pin 13 is the most basic thing to do and you must be familiar with it if you came so far, just to help you again doing it:
- Connect the long end of LED with 220 ohm to 1K ohm resistor
- Connect the other end of resistor to the pin 13 of Arduino
- Connect the short leg of LED to GND of Arduino
If you prefer, you can skip this step and just use the built-in yellow LED of Arduino. Our circuit is now complete and we will now move forward to building our software blocks!
Step 3: Send Serial Commands to Arduino Using Serial Monitor (or Any Terminal)
Upload the following sketch to the Arduino using USB cable.
Caution: Disconnect the HC-05 bluetooth module Rx and Tx pins from Arduino Uno as this particular board has only one hardware serial and connecting something to it while uploading a sketch will create conflict or your can using Arduino SoftwareSerial to avoid conflicts. Reconnect these pins once you are done uploading the sketch.
/*
Arduino Turn LED On/Off using Serial Commands Created April 22, 2015 Hammad Tariq, Incubator (Pakistan)It's a simple sketch which waits for a character on serial and in case of a desirable character, it turns an LED on/off.
Possible string values: a (to turn the LED on) b (tor turn the LED off) */
char junk; String inputString="";
void setup() // run once, when the sketch starts { Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor pinMode(13, OUTPUT); }
void loop() { if(Serial.available()){ while(Serial.available()) { char inChar = (char)Serial.read(); //read the input inputString += inChar; //make a string of the characters coming on serial } Serial.println(inputString); while (Serial.available() > 0) { junk = Serial.read() ; } // clear the serial buffer if(inputString == "a"){ //in case of 'a' turn the LED on digitalWrite(13, HIGH); }else if(inputString == "b"){ //incase of 'b' turn the LED off digitalWrite(13, LOW); } inputString = ""; } }
After uploading, open Arduino Serial Monitor, set the baud rate to 9600 and command line ending drop down (the one next to baud rate) to "No line ending", that means we will not be sending and /r or /n characters with our serial command.
Now type character "a" in the serial monitor and press send, the your LED should turn on, then send "b", the LED should turn off.
I have also attached the sketch file with this step, this is all we need at Arduino sketch level for turning LED on/off even through the Bluetooth and mobile app. In next step, we will communicate via bluetooth in order to play with our LED.
Attachments
Step 4: Getting HC-5 to Work With Arduino and Testing the Commuincation
In this step, we are focusing on getting HC-05 to work with Arduino and testing all the communication before we move towards building our mobile app.
Important: There is no extra step or coding required for HC-05 to work, it's a simple serial port module, which means if you pair it and then connect to it using any serial port terminal, it will work just like Arduino Serial Monitor.
Follow these mini-steps:
- Connect the Rx and Tx pins of HC-05 back to Arduino (if you haven't did this already).
- Pair the device with your mobile phone (in my case I am using Android OS and my phone is Samsung SII).
- Download "Bluetooth Terminal" app from Google Play Store.
- Open Bluetooth Terminal app.
- From the menu, tap on "Connect a device - Insecure".
- You will see a pop-up of "Paired Devices", tap on "HC-05", after a second you will get a toast notifying "Connected to HC-05".
- Now type "a" and send, the LED will turn on, similarly, send "b" to turn the LED off.
In the next step will start building our cross-platform HTML5 app by installing and using Cordova and Evothings Studio.
Step 5: Installing Evothings Studio
In this step, we will install Evothings Studio.
Evothings Studio uses Cordova which let's you develop applications in HTML5 and provides easy to use functions to interface with underlying OS architecture to use different functionalities of smartphone itself! Apps built with Cordova can be deployed on all kinds of modern mobile operating systems such as iOS, Android and Windows Phone etc. It's open-source and a large community is behind it, means you can easily find online help and plugins written for different use-cases and technologies.
Similarly, Evothings Studio is also open-source and it's main advantage is it's "reload-on-save" functionality, which means you don't need to rebuild your app all the time. If Evothings client and studio are installed and working, you can just edit your source HTML file and can see the latest changes in your mobile phone instantly.
Now quickly, follow these steps:
- Install Evothings Workbench.
- Install Evothings Client app ( Android, iOS)
- Connect Evothings Client with Evothings Workbench using your local WiFi IP address.
- Download and save the complete example app code from my github repository.
- Drag & drop the app's "index.html" file from the example app code folder to the Evothings Workbench, that will creat a new project entry in the Evothings Workbench.
- Click the "Run" button alongside the new project entry, that will load the example app inside the Evothings Client.
- Now click the "Code" button to see the location of the code, open the "index.html" file in your favourite IDE and change something, like the colour of a button, the Evothings Studio will immediately pick the change and will reload the app inside the Evothings Client, this functionality allows you to rapidly prototype your web app for IoT devices.
In the next step, I will explain the code.
Step 6: Introducing BluetoothSerial
Evothings comes pre-installed with BluetoothSerial plugin of Cordova, that means we can straight away start implementing bluetooth to serial communication in our project.
The most important function of BluetoothSerial plugin is:
bluetoothSerial.write();
Whatever we will send in write function, the plugin will write that on serial output of bluetooth module.
So, in this case, following code bit is doing all the work. When a user presses the "LED ON" button, the script will write "a" onto the Arduino serial using BluetoothSerial plugin and in the other case it will write the character "b", which our Arduino sketch understands as the argument to switch the LED to off status.
app.initialize(); app.ledOn = function(){ bluetoothSerial.write("a"); } app.ledOff = function() bluetoothSerial.write("b"); }
Step 7: Developing Front-end Using Evothings
So, we have everything ready now, our circuit, Arduino sketch, example app code and Evothings Studio also knows about the location of the project and can "Run" it.
The plugin's functionality code is hidden under /www/js/index.js and front-end is in /www/index.html, we have copied default CSS of Evothings and project is also running using Evothings Studio, whatever we will change, it will automatically reload the app in the phone, giving us the instant preview.
The only bit required is a bit of JavaScript and HTML know-how.
In next step, we will review our final app which is now ready!
Step 8: Reviewing the Completed Bluetooth Remote Control App
So, now we have our app and hardware working. Your app is already running in Evothings client, that is great, you have your 3 buttons and they switch the lights on and off and also let your manage your connection with the bluetooth module. Play with these, your work is over. The next step will be to Google a tutorial about adding a relay instead of the LED and control your home's lights through your new remote control.
Have fun!
70 Comments
Question 2 years ago
I have a problem where i cant find hc05 on my device...but i can enter the at command to change the name password and so on...what should i do...can anyone help
7 years ago
The EGBT-045MS Bluetooth modules (the smaller daughter board) is a 3.3v device. The HC-05 break out board has a 3.3v regulator that allows an input voltage of 3.6v to 6v but the TX and RX pins are still 3.3v. This means you can use the 5V out from the Arduino to power the boards but you cannot connect the Arduino directly to the HC-05 RX pin.
For the HC-05 RX pin (data in) you need to convert the Arduino's 5V signal level to 3.3v. A simple way to do this is by using a voltage divider made from a couple of resistors. A 1K ohm resistor and a 2K ohm resistor will do the job nicely. In your diagram, you are showing the Arduino connected *directly* to the module. Bad idea. Connecting a 5V signal to a module expecting a 3.3V signal would be like connecting a 7.6V signal to your Arduino. Would you do that? No. You need to step-down the signal voltage going into the HC-05's RX pin.
As a quick guide to the voltage divider; 1K + 2K = 3K. 1K is a third of 3K so it reduces the voltage by a third. One third of 5V is 1.66 and 5-1.66 = 3.33 which is what we want. Putting the resistors the other way would reduce the voltage by 2 thirds.
For more information on voltage dividers have a look at the Sparkfun tutorial
Since the Arduino will accept 3.3 volts as HIGH you can connect the HC-05 TX pin (data out) directly to the Arduino RX pin (The 5V Arduino takes a voltage of 3V or more as HIGH).
Reply 2 years ago
If the LED doesn't turn on it means the Bluetooth is damaged
Because I used 5V instead 3.3V now I can't control anything with my Bluetooth
Question 2 years ago
Why can't I control led using HC-05 Bluetooth Module means my Bluetooth is broken.
Question 3 years ago
error uploading code:
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x45
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x45
Problem beim Hochladen auf das Board. Hilfestellung dazu unter http://www.arduino.cc/en/Guide/Troubleshooting#upload.
Answer 3 years ago
Remove Tx Rx pin connection before uploading
4 years ago
the above code is used to control only one led light using bluetooth module .can u give the code to control three leds as traffic signal lights
Question 5 years ago
I follow every step but it doesn't work on step 4 when I have to send "a" or "b" from my moblie phone... What can I do for solving this problem? I have already tried with other apps...
Answer 5 years ago
I had this problem too but I managed to figure it out.
What I did was:
On your Arduino board, swap TX and RX Connections.
So Arduino's TX will now be connected to (HC-05) RX
and Arudio RX should be connected to (HC-05) TX
SO pretty much just swap the two connections on the Aruino board.
That worked for me.
Answer 5 years ago
Have you found a solution? I have the same problem.
Question 5 years ago
How to make an unmanned flood rescue boat system using arduino
Could help please it's urgent
Question 5 years ago on Step 4
My connection is working perfectly with the serial monitor but when conected by phone it is not working
5 years ago
Please download the iPhone serial APP on your phone _
https://itunes.apple.com/us/app/hm10-bluetooth-ser...
plug your UNO on Pc and copy and past the code....
// Connect : uno pin 7 to HM-10 pin TX,
uno pin 8 to HM-10 pin RX,
uno pin 5/3 volt to HM-10 pin VVC,
uno pin Ground to HM-10 pin Ground and led short leg,
uno pin 2 to led long leg with 220 Ohm reg.
// LED control with iPhone 5s & HM-10 Bluetooth Moduel by_Syed Shakhawat Hossain, Dhaka, Bangladesh
#include <SoftwareSerial.h>
#define LED_PIN 2
SoftwareSerial mySerial (7, 8); // TX, RX
// Connect HM10 Arduino Uno
// Pin 1/TXD Pin 7
// Pin 2/RXD Pin 8
char junk;
String inputString="";
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(2, OUTPUT);
mySerial.begin(9600);
}
void loop()
{
if(mySerial.available()){
while(mySerial.available())
{
char inChar = (char)mySerial.read(); //read the input
Serial.println("Got input:");
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (mySerial.available() > 0)
{ junk = mySerial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
Serial.println(" on");
digitalWrite(2, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
Serial.println(" off");
digitalWrite(2, LOW);
}
inputString = "";
}
}
// Happy Controling.......................
6 years ago
Hey, can you please help me. I wanna code an arduino for Ios app and the app only allows to send commands digitally from 1-9, for example 0 turn an led on and 1 to turn it off. But i need to control a lot of led's and it doesns't read past 9, When i press command 11 it receivs it as 1. I know its in the code. but im a bad coder. Can you please help me?
6 years ago
Could you do this?
I have completed the awesome project by Hammad Tariq (Control an Arduino via the HM-10 BLE module, from a mobile app on your smartphone)
I was wondering if I could send a 4 digit string via a text box from the app instead of one of the buttons. I could have the arduino loaded up with a 4 digit number to turn on led instead of the predetermined "0" . I would like to be able to change that 4 digit number ever so often by reloading the arduino sketch, but not the app html. So when i put 1234 or any 4 digit number into the arduino sketch, i could just input the 4 digit number in the app and submit.
Arduino Sketch
void loop() {
int c;
if (mySerial.available()) {
c = mySerial.read();
Serial.println("Got input:");
if (c != 0) // I would be able to change this part to any 4 digit number
{
// Non-zero input means "turn on LED".
Serial.println(" on");
digitalWrite(LED_PIN, HIGH);
}
else
{
// Input value zero means "turn off LED".
Serial.println(" off");
digitalWrite(LED_PIN, LOW);
}
}
}
And the text box instead of the predetermined "0"
// Turn on LED.
app.ledOff = function()
{
app.device && app.device.writeDataArray(new Uint8Array([0]), '0000ffe1-0000-1000-8000-00805f9b34fb'); //the 0 here would be the text box to enter any 4 digit number corresponding to the 4 digit number in the sketch
}
PLEASE!!!!!!!!!!!
6 years ago
from my Bluetooth terminal app only the commands a and b are sent but there is no change in the LED at PIN 13. It remains in the OFF state. but the TX LED in the arduino is glowing when i send the command from the app.( HC-05 Vcc = 5V RX pin = 3.3 V.) I am not able the fix the problem. Kindly suggest me some soulution.
6 years ago
You can control RGB led via bluetooth.
https://play.google.com/store/apps/details?id=com.appybuilder.sefikkaraburun.HC05_Arduino
7 years ago
Stucked at last step, not showing any bluetooth device as shown in ur screen shots though it is easily getting Connected using bluetooth terminal app.. Plz help
Reply 6 years ago
Yes , Me too. my smartphone is searching nearby devices but it is saying.
No nearby bluetooth device found . While everything is connected correctly. Please Help!
6 years ago
Hi,
I have a problem. The red letters are not the same of blue letters. Do you know how to fix this?
Thanks a lot