Cheap way to connect an Arduino to a Android Smartphone via bluetooth

trendski:
Hi teckel,

do you have any code examples of how to get this working with arduino and android?

cheers

Most of the bluetooth modules that you can buy have 4-6 pins (Rx, Tx, Power, Ground, and optionally Key, State). You hook Rx up to a port that does serial input, Tx up to a port that does serial output, Power and Ground to their respective rails, and State up to a LED if you want an indication of the connection. On your pro mini, you might connect Rx to pin 0, Tx to pin 1, and use the hardware serial port using the Serial function. You could use other pins with the software serial port emulation. On my Teensy 3.0, I'm using pins 7 & 8, which on that chip is the 3rd hardware serial port.

Here is some quick and dirty code that I was using to test my HC-05 bluetooth serial device on my Teensy 3.0. I have 4 LEDs hooked up to pins 13, 12, 11, and 10. I use a single character command:

  • 'a' .. 'd' turns off the respective LED;
  • 'A' .. 'D' turns on the respective LED;
  • '*' turns on all of the LEDs;
  • '0' turns off all of the LEDs;
  • '.' and ',' introduce delays.
#include <stddef.h>

const int leds[] = { 13, 12, 11, 10 };

#define NUM_LEDS (sizeof (leds) / sizeof (leds[0]))

void
setup (void)
{
  size_t i;

  Serial3.begin (9600);
  Serial3.print ("Start\r\n");
  Serial3.flush ();

  for (i = 0; i < NUM_LEDS; i++)
    {
      pinMode (leds[i], OUTPUT);
      digitalWrite (leds[i], LOW);
    }
}

static int number = 0;

void
loop (void)
{
  if (Serial3.available ())
    {
      char ch = Serial3.read ();
      if (ch >= 'a' && ch < 'a' + NUM_LEDS)
	digitalWrite (leds[ ch-'a' ], LOW);

      else if (ch >= 'A' && ch < 'A' + NUM_LEDS)
	digitalWrite (leds[ ch-'A' ], HIGH);

      else if (ch == '.')
	delay (1000);

      else if (ch == ',')
	delay (250);

      else if (ch == '0')
	{
	  for (size_t i = 0; i < NUM_LEDS; i++)
	    digitalWrite (leds[i], LOW);
	}

      else if (ch == '*')
	{
	  for (size_t i = 0; i < NUM_LEDS; i++)
	    digitalWrite (leds[i], HIGH);
	}

      number++;
      Serial3.print ("Bluetooth number: ");
      Serial3.print (number);
      Serial3.print (", read: ");
      Serial3.write (ch);
      Serial3.print ("\r\n");
    }
}

I happen to use the "Connection Terminal" app on the phone that provides a simple serial terminal interface: https://play.google.com/store/apps/details?id=app.bluetooth&hl=en.

I also tested the BTInterface trial app as mentioned in this thread: http://arduino.cc/forum/index.php/topic,145241.0.html.

One thing to watch out for is whether the device you buy can operate at the voltage your pro mini runs at (5v or 3.3v).

I bought my unit from a US seller on ebay (nyplatform) for $11US, and the app on the phone was free. There are various other bluetooth apps on the phone.