Hello. I have search quite a bit for questions regarding HC-05 issues and couldn't find anything quite like what I am experiencing.
I actually do not have any problem connecting with it, and at first glance it looks like the serial communication is working well. But actually, I have found that about 50% of the time, the message is passed immediately and the LED changes state and 50% of the time, the messages are buffered and will be all execute "at once" a few seconds later. The pattern is very obvious while looking at the HC-05. While connected, but not communicating, it has 2 blinks and a long pause. While communicating, it turns to a roughly 2 seconds between blinks pattern. As I am sending packets via my phone app, as soon as the HC-05 LED blinks, the communication is buffered and the arduino LED will not change. On the following HC-05 LED blink, all the messages are sent at once and communication is smooth again for some time. Until the next blink, where it all goes to the buffer again... I have also tried with software serial on pin (4,5) so I could use the Serial monitor and print to stdout. The behaviour is identical and I can see the messages stopping and then all being printed in a batch.
My code was originally much more complicated, but here is the current distilled version that reproduces my issue:
int flag = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
flag = Serial.read();
if (flag == '1')
{
digitalWrite(LED_BUILTIN, HIGH)
}
else if (flag == '0')
{
digitalWrite(LED_BUILTIN, LOW);
}
}
As for the schematic, I have also disconnect everything else and only the VCC, GND, TX -> RX, RX 1k -> TX, 2k -> GND remains.
My arduino is connected via USB to my PC for power and updating the code.
I am at a lost on how to solve this issue now. Any help would be appreciated.
@amitf Indeed it doesn't really do anything. It is a remnant of debugging and experiments. I've tested with your snippet and reproduced the issue. I've also edited my code in the question to reflect the change.
@jremington You are right. I have a lot of commented out code in my test code and missed that essential var when copy/pasting it here.
As for the debugging, I have written the android app and I am running it in debug mode, so I can see my logs immediately when I click on the On/Off. There is no difference from that side whether the message goes directly or is buffered.
Whenever I send the 1 or 0 in relationship to the two blinks the response is within 100 ms. I have added a reply from the HC05 back to the phone and monitor and the time on the phone between the send and the receive is within 100 ms. I can not reproduce the delay in relationship to the two blinks.
@cattledog Thank you for the great suggestion. Using this app, I still experience the same behaviour! So we can rule that out now.
Now, if the code provided by @amitf is good and should not have that issue and it's not the app's fault, I can think of 3 other possibilities:
Bad wiring. It's very simplified, but I have provide a picture just in case.
Bad configuration. The default baud rate should be 9600 and other options that I tried didn't work at all. I have NOT manually changed the mode by pressing the button and connecting the key pin, but if someone recognize that behaviour from at other mode of HC-05, maybe that is the issue?
Depending on the locations, I did see some places claiming 9600 or 38400. I did try before, but now I tried again with a "loop back" program paired with the Serial Bluetooth application suggested by @cattledog.
Not only these 2 baud rates, but I tried 1200, 2400, 4800, 9600, 19200, 38400, 57600 and 115200 too. ALL of them exhibit the same behaviour.
In the serial monitor, everything other than 9600 appeared mangled. The '0' and '1' did not appear as such.
I think that there maybe some HC05's with this default, but most are at 9600 in communication mode.
Regarding the possibility of a defective HC05, this forum has seen many postings of strange behavior which were ultimately attributed to defective units.
The most reliable source that I know of is DSD Tech.
Their branded products are guaranteed, and there may be technical support available.
Sadly this brand doesn't seem to be offered in my area (Japan) and shipping would cost as much as the module itself.
In any case, I guess I'll try to buy an other one from a different manufacturer and try again, since it seems the most likely culprit at this point is a defective unit.
I think it is only a rumour - spread around in Instructables.
Further, using Software serial at 38400 or higher is inviting disaster, and this
is likely to be evidence of the fact.
The pictures are marginal, but it seems that Bluetooth is connected to hardware serial, while the code suggests it should be on pins 4,5 under software serial. In the light of that, I don't know what the hell is going on, but it probably isn't what you think it is.
While there are duds and fakes around, you haven't proven you have got one. I understand even fakes can work OK so long as you don't mess about too much with configuration.
The unusual delay and strange indicator light flashing patterns indicate something is wrong with the hardware since the delay is seen with Bluetooth Serial Terminal app.
@Nick_Pyner The original code, as seen in the first post is connected directly with pin 0 and 1 hardware serial. The connection pictures shows that state and not my latest trials with SoftwareSerial. If you still don't think it's a defective hardware issue, I would love to know your ideas to confirm if it's the case or not! At this point, I can't think of anything else myself.
@cattledog I wanted to see that the issue still exist for software serial as well as different arduino pins. On top of that, it allowed me to display the message in the arduino serial monitor. Finally, it's also faster to quickly upload new code without having to disconnect/reconnect the RX/TX pins every time. I'll go ahead and buy a new one today. Worst case scenario, it is reproduced with the new one and then it would be less likely that it's a hardware issue.
Suggests it is more likely to be kosher than fake, and the problems are down to user error.
The best, indeed I submit, the only, way to be sure you are on the right track with HC-05, is to send a "Hello, Android here!" message from a terminal on the phone. This is a real world task and means there is no chance of misunderstanding what is going on.
Also re-read the last sentence of post #16 below....
When the module is connected to the hardware serial pins after upload, Serial.print() will both appear on the monitor and send the message over bluetooth. When connected this way, serial input can only come from the phone.
The inconvenience of disconnecting when revising code is a factor, but often not as problematic as the use of software serial.
After 3 months, I finally had some time to test out the new modules I bought. It turned out to be a faulty module!
Using the code from my original post and @cattledog 's proposed Serial Bluetooth Terminal, I could confirm that both new modules can consistently and continuously communicate without having the weird pausing behaviour that I encountered with my previous module.
Nonetheless, thank you for the support and suggestions.