Introduction: Remote Controlled LED Using HC-05 Bluetooth, Arduino and Mobile Phone App

About: I am a digital nomad and I often find myself navigating myself the fields of IoT, IIoT, VR, Voice Activation, Web and Mobile Apps and you know.. you got the idea :)

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:

  1. 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
  2. An Arduino, for this tutorial I will be using Arduino Uno.
  3. A solder less breadboard, 220 ohm to 1K ohm resistor and a LED
  4. 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.

  1. Connect VCC with 3.3V of Arduino, please do not connect it with 5V as that can cook the module
  2. Connect GND with any GND of Arduino
  3. Connect Rx pin with Tx of Arduino
  4. 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:

  1. Connect the long end of LED with 220 ohm to 1K ohm resistor
  2. Connect the other end of resistor to the pin 13 of Arduino
  3. 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.

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:

  1. Connect the Rx and Tx pins of HC-05 back to Arduino (if you haven't did this already).
  2. Pair the device with your mobile phone (in my case I am using Android OS and my phone is Samsung SII).
  3. Download "Bluetooth Terminal" app from Google Play Store.
  4. Open Bluetooth Terminal app.
  5. From the menu, tap on "Connect a device - Insecure".
  6. 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".
  7. 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:

  1. Install Evothings Workbench.
  2. Install Evothings Client app ( Android, iOS)
  3. Connect Evothings Client with Evothings Workbench using your local WiFi IP address.
  4. Download and save the complete example app code from my github repository.
  5. 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.
  6. Click the "Run" button alongside the new project entry, that will load the example app inside the Evothings Client.
  7. 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!