Communication between Android and HC05

Dear Friends

I am using HC05 bluetooth device for the purpose to control equipment like On/Off LED etc. along with Arduino Uno R3.

My program is working fine when I issue command from console. I can switch on/off LED by issuing command from console.

Problem is, I am not able to send command from Android phone. My Android phone is successfully connected with HC05. I used multiple program from Play store but none of them are able to control the LED.

Used following program from play store
ArduDroid, Arduino Bluetooth and Bluetooth control.

I will appreciate your help.

Attaching screen shot of my code and console output.

Thanks in advance.

Zafrul Umar

Please post your code in the proper manner using the </> tags. Has your HC-05 been configured to run at 38400? If not, alter the code to the default speed 9600. A standard terminal programme on Android should suffice.

Appreciate your reply Nick.

Below is code which I took from google posted by some friend. Yes HC05 is configured to run at 38400. As I mentioned in my main post, I am able to on/off LED from console. I mean when I send 'a' LED is coming ON and when I press 's' LED is off. Problem is, I am not able to do same from Android phone while HC05 is paired and connected with Android.


#define ledPin 13
int state = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(38400);
}
void loop() {
if(Serial.available() > 0){
state = Serial.read();
}
if (state == 's') {
digitalWrite(ledPin, LOW);
Serial.println("LED: OFF");
Serial.println(state);
state = 0;
}
else if (state == 'a') {
digitalWrite(ledPin, HIGH);
Serial.println("LED: ON");;
Serial.println(state);
state = 0;
}
}


Regards
Zafrul

Ardudroid comes with a sketch to put on the Arduino. If you don't want to use that sketch, you need to still look at it and see what it sends when a button in the app is used.

I ask the question because the code is proven kosher, and in the most sensible manner. The problem is therefore probably one of procedure. By that I include wiring.

Having HC-05 paired and connected with Android means only that. It does not mean that HC-05 is properly connected with Arduino. Have you connected Tx>Rx and Rx>Tx?

Yes HC05 is configured to run at 38400

How do you know this?

You will still be better off sending useful data than messing about with LEDs.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

When you say console do you mean the Arduino serial monitor or a terminal app on the Android device?

If you haven't already, get a basic connection working using a Android terminal app such as Bluetooth Terminal. Once you have the connection working you can use the terminal app to test the Arduino sketch.

Hello everyone. Thanks for reply. Let me check the links you have shared.
When I say console means serial monitor.

Thanks to all.

zafrulumar:
When I say console means serial monitor.

That's OK. Just use it to look at what's going on between Android and Arduino.

Hello Nick.

It was quite useful links. I don't know the actual problem but I wrote another sketch with baud rate 9600 and everything works fine. Idea of changing baud rate came from the code available on the link you have shared.

Still as I said I don't know the resolution but it works fine with baud rate 9600, i will again check with baud rate 38400 and will share the result.

Thanks again and thanks to all friends who have commented on my problem.

Zafrul

zafrulumar:
Yes HC05 is configured to run at 38400.

No it wasn't. And, if it works now at 9600, you have proven that that is what it was configured to, and I give you my ironclad guarantee that it won't work at 38400 until you configure Bluetooth to run at 38400, in which event it will not work at 9600.

i will again check with baud rate 38400 and will share the result.

Don't bother. And if you must change the rate, you might as well go for 115200. I rather feel that 38400 is pretty pointless, unless you are obliged to use software serial, and even then barely worth the effort. I think most people get by fine with the default 9600 anyway.

Nick_Pyner:
I think most people get by fine with the default 9600.

This is the internet. When you make statements like "well most people do ____ ", every special snowflake thinks they are the exception :slight_smile: