Help with bluetooth HC-05

I followed a code i saw online to control a LED with my phone via bluetooth but it seems like the HC-05 is having issues. I am able to connect to the HC-05 but when i enter 1, 0 or a, b the led doesnt light up as intended. So i checked the serial monitor and each time i input something the serial monitor shows "?". I thought maybe RX/TX were flipped but same issue. I tried configuring in AT mode but the serial monitor will show something like "xxxx????xxx??x". Im not sure what went wrong. I have tried multiple different codes and all have the same outcome. What might be the issue here?

It might be the code or the circuit or both, but impossible to say without sight of either.

This is one of the code i used:

#include <SoftwareSerial.h> 
SoftwareSerial EEBlue(4, 7); //RX | TX
const byte ledPin = 13; // built in led char val; 
char val = "";

void setup() {   
pinMode (ledPin, OUTPUT);   
Serial.begin(9600);   
EEBlue.begin(9600);  //Default Baud for comm, it may be different for your Module.
Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!."); 
}


void loop() {  
if (EEBlue.available())  //if a byte is available from BT  
{
val = (EEBlue.read());  //val = byte read from BT      
Serial.print("[");       
Serial.print(val);       
Serial.println("]");      
if (val == 'Z')  //compare byte from BT to Z       
{digitalWrite (ledPin, HIGH); //led on       
}
      if(val == 'A')       
{    

     digitalWrite(ledPin, LOW);  //led off       
}   
} 
}

val is supposed to display either a char depending on my input but it shows [?] instead of [A].

The HC-05 connection is:
VCC t o 5V
GND to Ground
RX to pin 4
TX to pin 7

Hi!

HC-05 module can be connected to 5V, but data lines are 3,3V. So, you should use a voltage divider connected in the line Arduino TX to HC-05 RX, in order to bring down the voltage of the signal.

Another tip is that default baud rate in AT mode is 38400. In order for it to work properly, serial monitor should be also set at 38400.

One last thing: your code is accepting "Z" to light up the led, not 0, 1, a or b.

Find out the proper connections for the RX/TX pins and be confident that you have it right. That's better than just swapping things over just to see if it works, don't you think?

There's several tutorials that you can follow.

What app are you running on the phone?

Hi @hakad,

What phone are you using? Android? Apple iPhone? the HC-05 does NOT work with an iPhone (I figured that out the hard way :wink:).

Although not 100% sure, 4 would be the RX on the Arduino and should be connected to the TX of the BlueTooth; 7 would be the TX on the Arduino and hould be connected to the RX of the BlueTooth.

So below would be wrong.

Please edit your post #3, select all code and click the </> button to apply code tags; next save your post.

Lastly, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Hello,

I am using Android. This is the app i use for the inputs.

Hello!

Does this affect the HC-05? For now im just doing some short tests so is it ok if i do not put a voltage divider?

As for the AT mode i followed a tutorial on how to do it but the results are as in my original post.

I also stated that i tried different codes but all have the same results, the led does not light up and when i look at the serial monitor, it shows ? instead of 1 or 0.

This is another code:

char Incoming_value = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0)
    {
      Incoming_value = Serial.read();
      Serial.print(Incoming_value);
      Serial.print("/n");
      if (Incoming_value == '1')
        digitalWrite(13,HIGH);
      else if(Incoming_value == '0')
        digitalWrite(13,LOW);
    }
}

This is what i get when i enter either 1 or 0 on my phone. Hope this gives you a clearer picture!

Thanks for the advice!

Alright i have done the necessary changes, thanks!

In the Serial Bluetooth Terminal settings, go to Send and set Newline to None. That prevents the terminal from sending a line feed and/or carriage return with the send output.

I am using an Uno to test. Using the code from the OP connect the HC05 TX pin to Uno pin 4, the HC05 RX to Uno pin 7 through a voltage divider (as described in many tutorials), 5V to HC05 Vcc and ground to ground.

I made only a slight change to your code, only using characters, no strings. The LED turns on with 'Z' and off with 'A'.

#include <SoftwareSerial.h>
SoftwareSerial EEBlue(4, 7); //RX | TX
const byte ledPin = 13; // built in led char val;
char val;  

void setup()
{
   pinMode (ledPin, OUTPUT);
   Serial.begin(9600);
   EEBlue.begin(9600);  //Default Baud for comm, it may be different for your Module.
   Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");
}

void loop()
{
   if (EEBlue.available())  //if a byte is available from BT
   {
      val = (EEBlue.read());  //val = byte read from BT
      Serial.print('[');
      Serial.print(val);
      Serial.println(']');
      if (val == 'Z')  //compare byte from BT to Z
      {
         digitalWrite (ledPin, HIGH); //led on
      }
      if (val == 'A')
      {
         digitalWrite(ledPin, LOW);  //led off
      }
   }
}

If you print Incoming_value in hex or decimal, you will see what you receive

if (Serial.available() > 0)
{
  Serial.print("0x");
  if(Incoming value <0x10)
  {
    Serial.print("0");
  }
  Serial.println(Incoming_value, HEX);
}

You will be able to see what you receive and look up what it means (e.g. https://www.asciitable.com/).

Note:
If "/n" was supposed to send a newline, the character is "\n".

Hi!

I have tried the things you suggested but still the LED is unable to light up.

I still get ? for the inputs. Could the HC-05 be faulty? Its the same when i tried on different UNO boards.

Oh thanks for the advice! I am able to see in the serial monitor now.


This is what i get when i send 1 or 0.

0xFFFFFFFF equals -1 which could indicate that nothing was received. That's strange as it should never be the case as it's inside the if(Serial.available() > 0).

If your BlueTooth module is connected to pins 0 and 1 (it wasn't accorrding to earlier posts), disconnect it and test with serial monitor only.

I don't think that I have other ideas at the moment.

Ok so, i changed from HEX to DEC and it shows -1 whenever i input anything onto the serial terminal on my phone. When i disconnected RX/TX, and input 1 or 0 onto the serial monitor, it shows 49 and 48 instead. I'm not sure what this means.

Could it be due to faulty HC-05?

Edit: I tried on another UNO board and i was able to control the led through serial monitor. However, still no luck with bluetooth.

UPDATE:

I am mainly using this code now.

char Incoming_value;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0)
    {
      Incoming_value = Serial.read();
      Serial.print(Incoming_value);
      Serial.print("\n");
      if (Incoming_value == '1'){
        digitalWrite(13,HIGH);
      }
      else if(Incoming_value == '0'){
        digitalWrite(13,LOW);
    }
    }
}

I can control LED through serial monitor by typing 1 or 0 but bluetooth still doesnt work. The issues with bluetooth is still the same. @sterretje has helped me quite abit and found out that for some reason the bluetooth is sending -1 to the UNO from my phone's serial terminal.

I am starting to think that the HC-05 may be faulty. What are your thoughts?

(Thanks for being patient with me :sweat_smile:)

If your BlueTooth module is connected to pins 0 and 1 on the Uno, stop using serial monitor; you will have to solely rely on the BlueTooth application on your phone.

Or go back to using Software Serial for the BlueTooth and Serial monitor for debugging.

If you have board with additional serial ports (e.g Leonardo, Mega), use that; Serial1 connected to the BlueTooth module (pins depend on the model) and Serial for debugging.

You can test your module / BlueTooth communication.

  1. Load an empty sketch (!!); this is important.
  2. Connect TX to TX; you can leave RX disconnected or connect RX to RX; the latter is only needed if you want to send data from serial monitor to your the BleuTooth application on your phone.
  3. Open serial monitor.
  4. Observe behaviour while sending from the BlueTootyh application on your phone.

I have tested it according to your instruction and i got this as a result.

When i sent over data from the serial monitor it shows up as ? on my phone, vice versa. I am getting a new HC-05 in a few weeks time so i will test again. Hopefully its because that this HC-05 is faulty.

Once again, thanks for your help! :grin: