IAN LANG ELECTRONICS

So, here's the pipeline:

 

The title to this article is simultaneously extraordinarily descriptive and yet somewhat vague because what we are going to discuss in this article is indeed pressing buttons on an Android app which relays instructions to your Arduino via a Bluetooth module, but what it does not say is that we are going to write the app as well as the Arduino sketch in which we are going to put some command code to configure the module. Oh and we are going to use serial software as well.

 

Come back......it's miles easier than it sounds. Here's a video to show the intended outcome.

Go Back

Using a Bluetooth Module to Control your Arduino with an Android App.

Android..

Bluetooth

Arduino

Anything attached to Arduino Pins

Again it's not as hard as it looks. The Android device sends a code to its embedded bluetooth module, that module chucks it out over the ether where it's picked up by the Sparkfun BlueSmirf module, which detects the signal, passes it to the Arduino, where a sketch translates the received signal (a set of two characters) into an action for the Arduino to do. In this case it's switching on and off pins 4 and 5 as required.

 

So, before we delve into coding, let's look at Bluetooth itself and then the Sparkfun module.

bluetooth

Bluetooth is a standardised low power wireless transmission protocol for sending information between electronic devices. It used to be standardised as IEEE 802.15.1 but the standard is no longer applicable and so although all bluetooth devices are broadly the same, some are different. For example the latest thing is BLE (Bluetooth low energy) which is designed for very short ranges as from a phone to an earpiece and is much more battery-friendly. Leave Bluetooth and wi-fi running on your mobile phone and see what happens to the battery. You'll need a travel charger.

Bluetooth operates in the 2.4 GHz  unlicensed ISM frequency band, which to say the least is quite crowded. The reason it's crowded is because as well as all those Bluetooth devices which are now on absolutely everything up to and including toasters, there's also things like Zigbee and ubiquitous wi-fi networks. It's probably the hardest-working band on the planet. Still, it's remarkably stable and completely accurate. It does it by using frequency hopping spread spectrum techniques over 79 channels dedicated to Bluetooth communications. These channels start at 2402 MHz and go up to 2480 MHz in steps of 1 MHz. At the basic rate it hops 1600 times per second.

 

Bluetooth has been around since 1994 but it's only in the last ten years or so that's it's become a consumer must-have. In the last five years it's caused a revolution in mobile communications since it's been stuck on phones and now you can control anything by Bluetooth using a smartphone or tablet, and that's what we are looking at here.

 

Now, you can have Bluetooth piconets, where you have a master and up to seven different slave devices. Bluetooth was originally conceived as a wireless replacement for RS232 cables and so works best if it's attached to one microcontroller which interprets the signals coming via Bluetooth and acts accordingly by running through an algorithm.  But none of this is any good if you dont pair up the module and the device you want to transmit from in the first place. We'll get to that later. For now, let's look at the module.

 

I'm using a Sparkfun BlueSmirf that I got from Proto-Pic in Scotland. Click on their logo below to see their page for it.

protopic bluetoothmate

The module itself has six pin outs but you only use four. The two we do not require here are CTS and RTS, which are clear to send and request to send and hark back to RS232 techniques. Below is the diagram as a Fritzing drawing of the setup of the Arduino board we'll need for this experiment:

arduinobluetooth

Ensure the TX from your module goes to pin 2 of your Arduino and the RX on your module goes to pin 3 of your Arduino or this is not going to work, because what we are going to do here is to use software serial.

 

Are we?

Yep.

Jolly good.

 

Software serial means you can assign any pin on the Arduino to be a serial Rx and Tx and for those of you who don't know Rx is receive and Tx is transmit. What is the advantage of using software serial? It means you can have more than one transmission method operating on your Arduino and also that you don't interfere with the running up sketch uploads by having stuff plugged in to the Rx and TX on pins 0 and 1.

 

Here comes a sketch which you can download in text format to copy and paste by clcking on the link on the left:

 

#include <SoftwareSerial.h>  

char incoming;

String parsein="";

boolean redflash=false;

boolean greenflash =false;

unsigned long timethenred =0;

unsigned long timethengreen =0;

 

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2

int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

 

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

 

void setup()

{

pinMode(4,OUTPUT);

pinMode(5,OUTPUT);

 

 bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps

// Printng $ three times individually puts bluetooth module in command mode

 bluetooth.print("$");

 bluetooth.print("$");

 bluetooth.print("$");  

 delay(100);  // Short delay, wait for the Mate to send back CMD

 bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity

 bluetooth.begin(9600);  // Start bluetooth serial at 9600

 

Serial.begin(9600);

}

 

void loop()

{

 

 if(bluetooth.available())  // If the bluetooth gets any characters

 {

   incoming = bluetooth.read();//read them

   parsein=parsein+incoming;}// bundle them up together

 

if (parsein.length()==2){// if a bundle of two is detected

if (parsein=="rf"){redflash=false;digitalWrite(4,0);}

if (parsein=="rn"){redflash=false;digitalWrite(4,1);}

if (parsein=="gf"){greenflash=false; digitalWrite(5,0);}

if (parsein=="gn"){greenflash=false;digitalWrite(5,1);}

if (parsein=="gh"){greenflash=true;digitalWrite(5,1);timethengreen =millis();}

if (parsein=="rh"){redflash=true;digitalWrite(4,1);timethenred =0;}

///Serial.println(parsein);

parsein="";//clear the bundle

   }

if (redflash==true){flashred();}

if (greenflash==true){flashgreen();}

 }

 

 void flashred(){

  if (millis()-timethenred >= 500){

    if (digitalRead(4)==1){digitalWrite(4,0);}

    else {digitalWrite(4,1);}

    timethenred=millis();

 }

 }

 void flashgreen(){

         if (millis()-timethengreen >= 500){

    if (digitalRead(5)==1){digitalWrite(5,0);}

    else {digitalWrite(5,1);}

    timethengreen=millis();

 }

 }

BTAD

Let's boil on over the page and see what's happening here.

More >