martes, 30 de agosto de 2011

Connect Arduino to an Android Device using Bluetooth SPP

In this tutorial we’ll check out how to make a really quick and effective connection via bluetooth (using one Bluesmirf module) with one Arduino and one Android cellphone/tablet.
Bluesmirf is one Bluetooth device specially designed for use with microcontrollers via a UART connection, it’s a Serial device. It uses Bluetooth SPP(Serial Port Protocole) for communication, which is a Serial Port emulation via a Bluetooth Connection.
Programming in Arduino is one thing, but programming in Android is another... So, what do we do if we want to control our Arduino using one Android device, but without programming this device?, I found a solution: Blueterm, this Android app is a download free app, that emulates a serial port terminal via a Bluetooth connection, really simple to use, it only requires that our Android device is Bluetooth SPP capable. So... if we have one Bluetooth SPP device(Bluesmirf preferable), one Arduino and one Android Device we are ready to make a connection!
We’ll be using the next stuff:


Bluesmirf and Bluetooth Mate have different pinouts...

Blueterm is a terminal app, this means it will display characters sent and received via a bluetooth serial port connection. So... let’s download and install Blueterm on our android device, Blueterm can be downloaded freely via Android Market, no further settings required, should work on most Android devices, if encountering problems, please refer to manufacturer’s datasheet for bluetooth profiles installed.
Now, we have to prepare our hardware, we’ll make a really simple test program to see if Arduino is receiving data via a bluetooth connection. First, we have to connect our Rx/Tx pins to our bluetooth module, if using a Bluesmirf module make sure to power it with 5V. You can use a breadboard or male/female header cables for connections.
Here’s our Arduino test code:
void setup(){
  //make sure your bluesmirf module is
  //configured at 115200 baudrate setting
  Serial.begin(115200);
  //let's test arduino led
  pinMode(13, OUTPUT);
}
void loop(){
  if(Serial.available()){
    //read first characer received
    unsigned char charreceived = Serial.read();
    
    switch(charreceived){
      case 'a':
        digitalWrite(13, HIGH);
        Serial.println("Arduino Led On");
        break;
      case 'b':
        digitalWrite(13, LOW);
        Serial.println("Arduino Led Off");
        break;
      default:
        break;
    }
    
    //flush remaining characters
    //we only want first character
    Serial.flush();
  }
  delay(10);
}
After uploading our code to our Arduino, we are ready to proceed with the bluetooth connection. In our Android device, we must activate bluetooth connections and search for our bluesmirf and pair it, depending on our bluesmirf, it’s name will vary, usually it will be findable with the initials: RN-41 or RN-42, pair code is usually “1234”. I strongly recommend setting our bluetooth module to defaults, check here how to do so. Pairing should present no further problems after a reset.
Now it’s time to truly test it, after pairing we have to open our Blueterm app, in our Blueterm app we have to press an option button, and we’ll see one option called “Connect device”, press it, it should display all our bluetooth paired devices, click on the bluesmirf option (RN-41 or RN-42 usually) and after a couple of seconds we should see in our upper  right corner a message saying “Connected”, now we are able to send characters via our terminal, remember: if we press ‘a’ we’ll turn on Arduino’s led, and if we press ‘b’ we’ll turn it off. Note: remember that not all android devices are Bluetooth SPP compatible, please refer to manufacturer’s product specifications.
Once we are connected to our Bluetooth module, you'll see the green led turned on




Main View of Blueterm App (I used Asus EePad Transformer)

So, that’s it, without much effort, we have our Arduino+Bluesmirf paired and connected with our Android device, now it’s up to us to make more complex code for Arduino, for example: maybe adding more cases in our switch sentence for more leds control, maybe using characters for motor speed control, it’s up to our imagination. It’s pretty easy, we didn’t have to kill ourselves programming in android. Please refer to this page for further bluetooth programming stuff in android.

1 comentario: