Simple Android to Arduino Bluetooth Communication

The Plan

In my last post, I showed basic serial communication between an Arduino Uno and a HC-06 Bluetooth module (Arduino + Bluetooth Module Bringup).  I will now show how to use that to send characters back and forth between an Android phone/tablet and the Arduino over Bluetooth using the HC-06 module.

The Setup

Arduino Side:

I have the same Arduino Uno + HC-06 Bluetooth module setup from the above referenced post.  Here is the Fritzing diagram again…

HC06BasicConnectCorrected

I used the same serial passthrough Arduino sketch as I showed in the above referenced post.

DIAGRAM UPDATED: Someone noticed that I had the HV and LV supply reversed in my original diagram. I have corrected it.

Note: I used a HC-05 Fritzing symbol here (only one I could find). The State and Key pins are not there on my HC-06 module. Not sure what they should be connected to if it were a HC-05.

Andriod Phone/Table Side:

I used an Android tablet for my testing.  To prepare for the testing, I powered up the HC-06 module and Bluetooth paired my tablet with the HC-06 using the standard Android Bluetooth settings menu.  When I searched for devices in the Android Bluetooth menu, I saw a new device with name HC-06.  When I tried to pair with it, I was asked for a pin.  I found the default pin listed in the HC-06 datasheet (http://silabs.org.ua/bc4/hc06.pdf) as 1234.  I entered 1234 and successfully paired my Android tablet with the HC-06 module.  The HC-06 data sheet gives the command to change the pin if needed but I am just going to leave it as the 1234 default for now.

Next was to find an Android App that could send and receive Bluetooth characters/strings.  Since I have been playing with MIT’s AppInventor recently (see some of my previous posts with the AppInventor tag), I decided to just build my own.

The Bluetooth Android App

I have a feeling I will be working with Bluetooth often from now on so I decided to make a very generic Android App that can be set to run as BT server or BT client.  Once a BT connection is made in either mode, I want to be able to send and receive characters to the other side.

I created the app in MIT’s AppInventor environment but it would be a little too long to post a tutorial on all the steps.  If you are interested, here is my .aia file if you want to load it into your AppInventor environment and review it on your own….

https://drive.google.com/file/d/0B1a0nPfCQQvKd3NXdkJYeTk4dGM/view?usp=sharing

If you just want to install and use it, here is the .apk file…

https://drive.google.com/file/d/0B1a0nPfCQQvKTk9PZ0JkVG1sSk0/view?usp=sharing

Here are a couple screen shots…

ManualBTscreenshotManualBTScreen2

The App can actually be used without the Arduino + HC-06 setup.  It can even be installed on 2 Android devices then:

  1. Pair the 2 Android devices first
  2. Launched the app on both devices
  3. Click the “Start as Server” button on the 1st device
  4. Click the “Start as Client” button on the 2nd device
  5. If the Bluetooth link is established, the screen on both sides should change to have Send Text / Received Text boxes and Send button.
  6. Just type a string in the Send Text box and hit the Send button.  You should see the string show up in the Received Text box on the other device.

Here is a quick video demo…

The Android to Arduino Bluetooth Test

I didn’t find any documentation on the HC-06 datasheet on how to connect to a BT server so I assumed it was default set up as a BT server.  I launched my Android App and clicked “Start as Client”.  I had previously already BT paired the HC-06 on that tablet so it popped up as one of options to connect to.  I selected HC-06 and the connection was successful.  The red LED on the HC-06 went from blinking to solid on.  When I sent something from the Android App, it showed up in the Arduino serial monitor window.  Now when I typed something in the Arduino serial monitor window, it popped up in the “Received Text” box on the Android App.  Before the Arduino serial to HC-06 was for AT commands, but as soon as a BT connection established, it looks like the HC-06 automatically switched to serial pass through mode.

To better illustrate, here is a quick video demo….

Arduino + Bluetooth Module Bringup

The Plan

I wanted to add Bluetooth communication capabilities to my Arduino projects.  I found a $5.01 Bluetooth module here… http://www.banggood.com/HC-06-Wireless-Bluetooth-Transceiver-RF-Main-Module-Serial-For-Arduino-p-80364.html.  This post shows the basic setup I did to get communications started.

The Hardware Setup

The voltage for the module from the supplier listed above was set for 3.6V – 6V so the Arduino 5V supply output is ok.

I found the data sheet for the HC-06 module here: http://silabs.org.ua/bc4/hc06.pdf.  Unfortunately the Tx and RX voltage is 3.3V still so I also purchased a cheap $1.82 level shifter here: http://www.banggood.com/Logic-Level-Converter-Bi-Directional-IIC-4-Way-Level-Conversion-Module-p-938774.html.

For the basic communication, I just hooked up the TX and RX through the level shifter to a SW UART on my Arduino UNO.  Here is the Fritzing diagram…

HC06BasicConnectCorrected

DIAGRAM UPDATED: Someone noticed that I had the HV and LV supply reversed in my original diagram.  I have corrected it.

Note: I used a HC-05 Fritzing symbol here (only one I could find).  The State and Key pins are not there on my HC-06 module.  Not sure what they should be connected to if it were a HC-05.

The Arduino Code

For the basic test, I am just using a basic pass through Arduino sketch…

/*
 Software serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 6); // RX, TX

void setup() 
{
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 mySerial.begin(9600);

 
}

void loop() // run over and over
{
 while (mySerial.available()>0 )
 Serial.write(mySerial.read());
 
 while (Serial.available()>0 ){
 mySerial.write(Serial.read());
 }
}

The Initial Test / Debug

I uploaded the Arduino sketch and opened the serial monitor and tried to send “AT” to the module.  Nothing came back.  Google’d around and found this post http://www.instructables.com/id/AT-command-mode-of-HC-05-Bluetooth-module/?ALLSTEPS.  One of the comments in the post said that you can’t send NL or CR in the serial communication to the HC-06 module.  Changed the serial monitor settings…

HC06SerialWindowBasicComm

Once I changed the serial monitor to not send any line ending, I was able to send AT+VERSION and get the response in the above screen capture.  Sending any line ending character resulted in no response.

The Next Post

I was able to Bluetooth pair the HC-06 with my Android tablet and write an Android app to send text back and forth to the Arduino.  That will have to be covered in a separate post.  I will post those development steps in my next blog post tomorrow (or sometime soon).