Friday, November 7, 2014

Controlling your Arduino from your phone via bluetooth

Controlling your Arduino from your phone via bluetooth is simple business. I would  have liked having been told that a few years ago. It's just that to get it done, you need resources that are spread throughout the web. In this post, we'll build a simple controller. Our aim is to switch an LED on/off . All that, wirelessly.
So, where do we start? We could ramble on and on about the various theories on Bluetooth but chances are you need a quick fix solution. That is what we'll be doing here.
Ok, so parts we'll be needing:
1. An arduino
Well, any arduino would do but I'm using an arduino uno
2. An LED
Classic 5mm LED
3. A bluetooth module
The bluetooth module is essentially the component which takes care of the transmission and reception of the data. I'm using a bluetooth mate silver which is shown in Figure 1.



Figure 1: The BT mate silver



4. A phone with some serial communication software
I'm using a Lumia 625 and despite the limited limited LIMITED(did I say LIMITED? It's that bad) number of apps available on their store, I grabbed a small serial communication software called BT Terminal, shown in Figure 2. Many many thanks to the creator of this app.


Figure 2: The App
5. An RGB
An RGB is essentially 3 LEDs in one package. They are insanely cool.


Figure 3: The RGB

6. Some wires
First things first. You need to power your project. Be it the 5V regulator, usb power, anything that works will do. So in my case, I'm just plugging the UNO to my computer's usb port.
Then, you need to connect the components together. So connections are

A) Bluetooth mate silver module
Now this is advanced stuff. So, the module will communicate at some speed and we need to know that speed. I think the BT mate silver's default speed is 115000 bauds. You can change that to values that suit you by configuring the module. If you don't want to do that, you'll be using 115000 in the program. I've set mine at 9600. The module comes without header pins, so if yours is out of the box, you have some soldering job on the line(be careful to not leave your iron on these pins for too long)
There are some marking on the board, so:
1. VCC to 5V power
2. GND to Ground
3. TX-0 to Arduino Pin 2
4. RX-1 to Arduino Pin 3

B) LED
Simple job
1. Ground it
2. Connect the +ve lead to pin 5 via a resistor(anything between 1k and 2k is OK)

C) RGB
The RGB is cool once you know it. So longest lead is grounded and the remaining three pins are connected to Arduino Pins 6, 7 and 8 via resistors. You should have something like this.



Figure 4: Setup


Now, for the code:
/* 
Simple Bluetooth controller project
Sordagen Bryan Arnasala
7 November 2014
*/
#include <SoftwareSerial.h>
//Setting up trans and recept pins
int BTtx = 2; //Arduino Pin 2 is connected to TX-0
int BTrx = 3; //Arduino Pin 3 is connected to RX-1
SoftwareSerial BT(BTtx, BTrx); // Serial comm config, I'm using BT,anything'll do
//Setting up led and RGB pins
int LED = 5;
int RGB1 = 6;
int RGB2 = 7;
int RGB3 = 8;
char TOKEN; 

void setup()
{
  BT.begin(9600); // Careful here. Make sure you have the proper value
  pinMode(LED, OUTPUT);
  pinMode(RGB1, OUTPUT);
  pinMode(RGB2, OUTPUT);
  pinMode(RGB3, OUTPUT);
}

void loop()
{
  while (!BT.available()); 
  TOKEN = BT.read();
  if( TOKEN == '1' )
  {
    digitalWrite(LED, HIGH);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN == '2' )
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, HIGH);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN == '3' )
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, HIGH);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN =='4')
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, HIGH);
  }
  else if( TOKEN =='5') //Turn off all LEDs
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
}

Ok, now just upload this. Load your serial comm app. You might have to do some straight forward bluetooth pairing and you are off. Commands "1" to "4" switch one colour on. Command "5" turns off all of them. Here's the project at work.




That's it! If you can switch LEDs on and off, you could do the same for motors, sirens, fans...anything! You might want to read data from sensors. This is easy to do. The BT.println() command does that. You might also be cognizant in App development - you could then build an app that is custom for your project. 
Thanks for reading!


No comments:

Post a Comment