HC05 cannot send or receive data using android phone

Hi,

I have been troubleshooting my circuit. I'm using arduino uno and I'm using HC-05. Using serial monitor of arduino software I can send and receive data. I just send 1 or 0 and it will on or off the led respectively. The arduino also will send LED On and OFF during the command which can be seen in the serial monitor. I'm using PIN 0 (RX) and 1 (TX) for the serial communication. I also use voltage divider for the RX of my HC-05. I'm currently connecting my arduino to my PC via USB connection. But when I connect it via bluetooth it cannot send or receive the data. I use S2 terminal app in my android phone to simulate 1 or 0 in ASCII however it does not receive any.

Any advice on this?

I also tried disconnecting it from my pc and supply 5v via charger then connect my phone via bluetooth. But send and receive does not work.

Hoping for any advice.

Thank you

int state;

void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600); // Default communication rate of the Bluetooth module
}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
}
if (state == '0') {
digitalWrite(LED_BUILTIN, LOW); // Turn LED OFF
Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
state = 0;
}
else if (state == '1') {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED: ON");
state = 0;
}

}

Pins 0&1 are used by the Arduino serial port, which is being used by yr sketch. It’s the inbuilt method for the monitor to communicate with the board.

You can connect an external device to pins 0&1 to communicate with the Arduino.

BUT – you cannot do both.

You need to explore software serial and use two pins other than 0&1 for the bluetooth connection.

Play with the following sketch which I think is called “serial_10_11”. Don’t recall where I got it.

#include <SoftwareSerial.h>
SoftwareSerial EEBlue(3, 4); // RX | TX
char data = 0; //Variable for storing received data

void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT); //Sets digital pin 11 as output pin
Serial.begin(9600);
EEBlue.begin(9600); //Baud Rate for command Mode.
Serial.println(“Enter AT commands!”);
}

void loop()
{

// Feed any data from bluetooth to Terminal.
if (EEBlue.available()){
data = EEBlue.read(); //Read the incoming data & store into data
Serial.println(data); //Print Value inside data in Serial monitor

if(data == ‘1’)digitalWrite(13, HIGH); //If value is 1 then LED turns ON
if(data == ‘0’)digitalWrite(13, LOW); //If value is 1 then LED turns ON

// Checks whether value of data is equal to 1
}
//
// Feed all data from termial to bluetooth
if (Serial.available())
EEBlue.write(Serial.read());
}

@OP

You may carryy out the following steps to do functional check of your BT and Android based BT.
1. Build the following circuit between UNO and HC05.
HC5-uno.png

2. Upload the following sketch.

#include<SoftwareSerial.h> //use software serial port as hardware port is engaged with IDE/SM
SoftwareSerial SUART(2, 3);  //SRX = DPin-2; STX = DPin-3

[code]char c = ' ';

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  if (SUART.available())  //check character/data is coming from Android
  {
    c = SUART.read();    //data has come from Android and read it
    Serial.write(c);         //show the received character on Serial Monitor
  }

  if (Serial.available())   //check if data/character is coming from InputBox of Serial Monitor
  {
    c =  Serial.read();     //data has come from Serial Monitor and read it.
    Serial.write(c);          //send it back to the OutputBox of Serial Monitor
    SUART.write(c);         //send it to Android via BT
  }
}

3. Open BT Terminal of the Android phone and pair it with BT of UNO.

4. Send A from Android and check that the character has appeared on the Serial Monitor of UNO.

5. Send B from the InputBox of Serial Monitor and check that it has appeared on the BT screen of Android.
SerialMonitor.png

6. Modify sketch of Step-2 to ON/OFF L (built-in LED of UNO) by sending commands from Android.

HC5-uno.png

SerialMonitor.png

Your problem may be as much procedural as programming. Is Bluetooth wired correctly to Arduino - Rx>Tx and Tx>Rx? 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

And it might be best to stick with PC for power to start with

jpom:
Pins 0&1 are used by the Arduino serial port, which is being used by yr sketch. It’s the inbuilt method for the monitor to communicate with the board.

You can connect an external device to pins 0&1 to communicate with the Arduino.

BUT – you cannot do both.

You need to explore software serial and use two pins other than 0&1 for the bluetooth connection.

Play with the following sketch which I think is called “serial_10_11”. Don’t recall where I got it.

#include <SoftwareSerial.h>
SoftwareSerial EEBlue(3, 4); // RX | TX
char data = 0; //Variable for storing received data

void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT); //Sets digital pin 11 as output pin
Serial.begin(9600);
EEBlue.begin(9600); //Baud Rate for command Mode.
Serial.println(“Enter AT commands!”);
}

void loop()
{

// Feed any data from bluetooth to Terminal.
if (EEBlue.available()){
data = EEBlue.read(); //Read the incoming data & store into data
Serial.println(data); //Print Value inside data in Serial monitor

if(data == ‘1’)digitalWrite(13, HIGH); //If value is 1 then LED turns ON
if(data == ‘0’)digitalWrite(13, LOW); //If value is 1 then LED turns ON

// Checks whether value of data is equal to 1
}
//
// Feed all data from termial to bluetooth
if (Serial.available())
EEBlue.write(Serial.read());
}

Thank you so much guys.

I really appreciated it. Now I learned that I cannot use the pins 0 and 1 for serial communication of the sketch and bluetooth. I’m using the program above. Thank you jpom and GolamMostafa

raffy122890:
Now I learned that I cannot use the pins 0 and 1 for serial communication of the sketch and bluetooth.

You can use Bluetooth on pins 0,1, and there are significant advantages in doing so. About the only times when you cannot use pins 0,1 are

  1. when you are sending AT commands to configure Bluetooth, and even then it is not vital.

  2. when you need to communicate from serial monitor through Arduino to some other Bluetooth device, which I submit is actually quite rare, and usually a dumb idea.

Nick_Pyner:
You can use Bluetooth on pins 0,1, and there are significant advantages in doing so. About the only times when you cannot use pins 0,1 are

  1. when you are sending AT commands to configure Bluetooth, and even then it is not vital.

  2. when you need to communicate from serial monitor through Arduino to some other Bluetooth device, which I submit is actually quite rare, and usually a dumb idea.

Hello Nick,

I am having a problem again. I am using JPOMS code. I connected the bluetooth RX and TX to TX(pin1) and RX(pin0) respectively. I disconnected the usb cable and have a different power supply (9V). Thus, if my understanding is correct I can use the RX and TX of UNO via bluetooth without conflicting with my PC. However I seem cannot send or receive the correct data.

I have my android app and can receive the "Enter AT commands!".

But when I send a data to the arduino it is a "?" symbol. Is because of the baud rate?.

I had changed the baud rate from 9600 to all the baud rates available except for 200k+ baud rates but still it doesn't work. What could be the problem?

char data = 0; //Variable for storing received data

void setup()
{

pinMode(10, OUTPUT); //Sets digital pin 11 as output pin
Serial.begin(9600);
Serial.println("Enter AT commands!");
}

void loop()
{

if (Serial.available()){
data = Serial.read(); //Read the incoming data & store into data
Serial.println(data); //Print Value inside data in Serial monitor

if(data == '1')
{
digitalWrite(10, HIGH); //If value is 1 then LED turns ON
Serial.println("LED ON");
}
if(data == '0')
{
digitalWrite(10, LOW);
Serial.println("LED OFF");
}
}

}

Hi raffy.

Things are getting very confused.

The sketch I quoted was for you to explore software serial using pins other than 0 & 1.

On my non genuine UNO, I connect the TX of the HC-05 to pin 1 on the UNO, and RX of the HC-05 to pin 0 of the UNO. I don't care about the UNO's pin labels -- this is the way it works on my board. Others say theirs' work the other way around. Observing the on-board LEDs labelled TX and RX is helpful.

The UNO's on board LED labelled "L" is driven off pin 13. That's all you need.

The default baud rate of the HC-05 is 9600. You shouldn't need to worry about it.

I think you need to duplicate exactly somebody's working design, get it to work and then make your own changes one at a time.

Good luck.

You’re still going to have a conflict as you are using the h/w serial and connecting the HC to pins 0 & 1. This will not work. Tthe IDE can indeed communicate with the HC via pins 0&1 but ONLY IF YR SKETCH DOES NOT USE Serial.begin.

Enter text in the IDE input box, press send or and it goes to whatever serial device is connected to pins 0&1. If that device sends text it will appear in the IDE output box. You do not need the serial commands Serial.print & Serial.read. It just happens cos the h/w serial channel is builtin and the IDE knows about it. (IN NON GENUINE ARDUINOS THE COMMS MIGHT ONLY WORK IN ONE DIRECTION – FROM THE EXTERNAL SERIAL DEVICE TO THE IDE.)

Here’s some experimenting that might be enlightening:

  1. load the Blink sketch from the IDE’s examples (WITH NOTHING CONNECTED TO PINS 0 OR 1)
  2. Connect the TX pin of the HC to pin 0 then pin 1; the HC’s TX pin transmits data on the wired connection
  3. Observe the Arduino’s TX & RX LEDS; they shud be lit when u connect the HC’s TX pin; this tells u which one is the Arduino’s pin that receives data from the external device (on mine it’s pin 1)
  4. Don’t connect the HC’s RX pin to anything
  5. From yr phone’s serial terminal app connect to yr bluetooth
  6. What u send from the terminal app ought to appear in the IDE output box (sometimes u have to “wake up” the IDE monitor by closing it and reopening it)
  7. Edit the Blink sketch by inserting " Serial.begin(9600);"; this initiates the h/w serial channel;
  8. Upload the sketch with nothing connected to 0 & 1; then reconnect the HC’s TX pin
  9. Now yr terminal app will not be able to send to the IDE;
  10. Press & hold the Arduino’s reset button (or a jumper from the RESET pin to GND);
  11. Now the terminal app WILL be able to send to the IDE;
  12. This proves that when a sketch implements the Serial.begin statement pins 0 & 1 are not available to an external device;

I know Nick & others say there are plusses to using the h/w serial, but it causes beginners lots o trouble.

Good luck,
John.

raffy122890:
I am using JPOMS code.

That code is for configuring Bluetooth via software serial, not communicating with the phone using hardware serial.

I connected the bluetooth RX and TX to TX(pin1) and RX(pin0) respectively.

This is correct, transmitters transmit to receivers. I have never heard of any Arduino with wrongly described pins, only people misunderstanding what the labels mean.

I disconnected the usb cable

You only need to do this when using pins 0,1 for bluetooth and then only when uploading the programme.

and have a different power supply (9V).

If you mean a 9v PP3 battery, it is not a good idea. It may seem not a bad idea today, but it will be tomorrow. if you must use a battery, use a proper one, and probably better not with a Uno

Thus, if my understanding is correct I can use the RX and TX of UNO via bluetooth without conflicting with my PC.

Correct. See my notes

However I seem cannot send or receive the correct data…I have my android app and can receive the “Enter AT commands!”.

Your gear is doing exactly what you have programmed it to do. “Enter AT commands!”. is in Setup for transmission via hardware serial and Bluetooth. It also suggests your wiring is kosher.

But when I send a data to the arduino it is a “?” symbol. Is because of the baud rate?.

Probably not. The code is wrong. For communication in hardware serial, try the code in reply #3. You can test it using the serial monitor after uploading, i.e. Bluetooth still disconnected and, needless to say, proper power from a cable.

I had changed the baud rate from 9600 to all the baud rates available except for 200k+ baud rates but still it doesn’t work.

It surely won’t, so don’t. Stick with 9600 everywhere until you know what you are doing and can configure Bluetooth accordingly. You may never need to change it anyway.