Sunday, April 29, 2012

Arduino, Android and Seeedstudio Bluetooth Bee

Introduction

In this article we examine the Seeedstudio ”Bluetooth Bee“ modules and how they can be used in a simple way in conjunction with Android devices to control the Arduino world.  Here is an example of a Bluetooth Bee:

For the curious, the hardware specifications are as follows:

  • Typical -80dBm sensitivity
  • Up to +4dBm RF transmit power
  • Fully Qualified Bluetooth V2.0+EDR 3Mbps Modulation
  • Low Power 1.8V Operation, 1.8 to 3.6V I/O
  • UART interface with programmable baud rate
  • Integrated PCB antenna.
  • XBee compatible headers

You may have noticed that the Bluetooth Bee looks similar to the Xbee-style data transceivers – and it is, in physical size and some pinouts, for example:

The neat thing with the BtB (Bluetooth Bee) is that it is compatible with Xbee sockets and Arduino shields. It is a 3.3V device and has the same pinouts for Vcc, GND, TX and RX – so an existing Xbee shield will work just fine.

In some situations you may want to use your BtB on one UART and have another for debugging or other data transport from an Arduino – which means the need for a software serial port. To do this you can get a “Bees Shield” which allows for two ‘Bee format transceivers on one board, which also has jumpers to select software serial pins for one of them. For example:

Although not the smallest, the Bees Shield proves very useful for experimenting and busy wireless data transmit/receive systems. More about the Bees Shield can be found on their product wiki.

Quick Start 

 In the past many people have told me that bluetooth connectivity has been too difficult or expensive to work with. In this article I want to make things as simple as possible, allowing you to just move forward with your ideas and projects. One very useful function is to control an Arduino-compatible board with an Android-based mobile phone that has Bluetooth connectivity. Using the BtB we can create a wireless serial text bridge between the phone and the Arduino, allowing control and data transmission between the two.

We do this by using a terminal application on the Android device – for our examples we will be using “BlueTerm” which can be downloaded from Google Play – search for “blueterm” as shown below:

In our Quick Start example, we will create a system where we can turn on or off four Arduino digital output pins from D4~D7. (If you are unsure about how to program an Arduino, please consider this short series of tutorials). The BtB is connected using the Bees shield. This is based on the demonstration sketch made available on the BtB Wiki page - we will use commands from the terminal on the Android device to control the Arduino board, which will then return back status.

Question – Which company invented Bluetooth?

As the BtB transmit and receive serial data we will have it ‘listen’ to the virtual serial port on pins 9 and 10 for incoming characters. Using a switch…case function it then makes decisions based on the incoming character. You can download the sketch from here. It is written for Arduino v23. If you were to modify this sketch for your own use, study the void loop() section to see how the incoming data is interpreted, and how data is sent back to the Android terminal using blueToothSerial.println

/* BluetoothBee Demo Code - Flowcontrol Based Implementation 2010,2011 Copyright (c) Seeed Technology Inc. All right reserved.  Author: Visweswara R Modified: John Boxall, April 2012  This demo code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.  You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA  For more details about the product please check http://www.seeedstudio.com/depot/  */
#include <NewSoftSerial.h> //Software Serial Port #define RxD 9 #define TxD 10 #define DEBUG_ENABLED 1
NewSoftSerial blueToothSerial(RxD,TxD); char incoming;
void setup() { pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); setupBlueToothConnection(); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT);
}
void setupBlueToothConnection() { blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400 delay(1000); sendBlueToothCommand("\r\n+STWMOD=0\r\n"); sendBlueToothCommand("\r\n+STNA=SeeeduinoBluetooth\r\n"); sendBlueToothCommand("\r\n+STAUTO=0\r\n"); sendBlueToothCommand("\r\n+STOAUT=1\r\n"); sendBlueToothCommand("\r\n +STPIN=0000\r\n"); delay(2000); // This delay is required. sendBlueToothCommand("\r\n+INQ=1\r\n"); delay(2000); // This delay is required. }
//Checks if the response "OK" is received void CheckOK() { char a,b; while(1) { if(blueToothSerial.available()) { a = blueToothSerial.read();
if('O' == a) { // Wait for next character K. available() is required in some cases, as K is not immediately available. while(blueToothSerial.available()) { b = blueToothSerial.read(); break; } if('K' == b) { break; } } } }
while( (a = blueToothSerial.read()) != -1) { //Wait until all other response chars are received } }
void sendBlueToothCommand(char command[]) { blueToothSerial.print(command); CheckOK(); }
void loop() { // get character sent from Android device incoming=blueToothSerial.read(); // decide what to do with it switch (incoming) { case '1': digitalWrite(4, HIGH); blueToothSerial.println("pin 4 HIGH"); break;
case '2': digitalWrite(5, HIGH); blueToothSerial.println("pin 5 HIGH"); break;
case '3': digitalWrite(6, HIGH); blueToothSerial.println("pin 6 HIGH"); break;
case '4': digitalWrite(7, HIGH); blueToothSerial.println("pin 7 HIGH"); break;
case '5': digitalWrite(4, LOW); blueToothSerial.println("pin 4 LOW"); break;
case '6': digitalWrite(5, LOW); blueToothSerial.println("pin 5 LOW"); break;
case '7': digitalWrite(6, LOW); blueToothSerial.println("pin 6 LOW"); break;
case '8': digitalWrite(7, LOW); blueToothSerial.println("pin 7 LOW"); break; } delay(100); }

Before using it for the first time you will need to pair the BtB with your Android device. The PIN is set to a default of four zeros. After setting up the hardware and uploading the sketch, wait until the LEDs on the BtB blink alternately – at this point you can get a connection and start communicating. In the following video clip you can see the whole process:


Where to from here?

There are many more commands that can be set using terminal software from a PC with a Bluetooth adaptor, such as changing the PIN, device name and so on. All these are described in the BtB Wiki page along with installation instructions for various operating systems.

Once again I hope you found this article interesting and useful. The Bluetooth Bees are an inexpensive and useful method for interfacing your Arduino to other Bluetooth-compatible devices. For more information and product support, visit the Seeedstudio product pages.

Bluetooth Bees are available from Seeedstudio and their network of distributors.

Disclaimer - Bluetooth Bee products used in this article are promotional considerations made available by Seeedstudio.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

1 comment:

  1. Thanks for giving such an interesting post. You gave some valuable points which are very useful for all readers to understand Xbee quick start. It's a very good source for installing XBee module in Linux.

    ReplyDelete