How to HC-05 bluetooth

Hi everyone, I'm playing with the HC-05 module for the first time and just can't get it to work.
I have and HC-05, firmware hc01.comV2.1.

I've checked the module status with AT commands, it's in slave mode and everything seems ok (compared to parameters found on many tutorials).

I have the HC-05 connected to an Arduino Pro Mini:
Vcc -> Pro Mini 5V
GND -> Pro Mini GND
Tx -> Pro Mini Rx
Rx -> Pro Mini Tx (with a Voltage divider 2 Ohm + 1 Ohm to give 3.3V to Rx channel)

The following code (I got it from a youtube video) is the only one giving me some feedback

int ledPin = 13; 
int state = 0;
int flag = 0; 
 
void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, HIGH);
 delay(3000);
 digitalWrite(ledPin, LOW);
 
 Serial.begin(9600); 
 Serial.println("LED: off");
}
 
void loop() {

 if(Serial.available() > 0){
 state = Serial.read();
 flag=0;
 }

 if (state == '0') {
 digitalWrite(ledPin, LOW);
 if(flag == 0){
 Serial.println("LED: off");
 flag = 1;
 }
 }

 if (state == '1') {
 digitalWrite(ledPin, HIGH);
 if(flag == 0){
 Serial.println("LED: on");
 flag = 1;
 }
 }
}

I send commands to the boards with an Android App (Arduino bluetooth controller): if I send the number "49" from the terminal (49=1 on the ASCII table) it actually turns the built-in Led on for 3 seconds, as stated in the void setup.

Well, this is what I can't understand! Why does it execute commands from the void setup and not from the Void Loop?

THANKS.

http://www.martyncurrey.com/tag/hc-05/

eliseo88:
if I send the number "49" from the terminal (49=1 on the ASCII table) it actually turns the built-in Led on for 3 seconds, as stated in the void setup.

I think that is nonsense. The way I read it, the LED is on for three seconds when you start, and bluetooth has nothing to do with it. Setup serves no other purpose than a check that LED is working. The only important lines in Setup are the last two, which send a confirmation of the bleeding obvious over bluetooth to the phone, but about which you say nothing. The activity of the LED on bluetooth is of more interest than that on the Pro Mini. Indeed, all references to LedPin could be usefully omitted.

IF you receive "LED off" on the phone, this is probably not a bluetooth problem.

Your problems may be more to do with power supply than bluetooth. Please don't tell us you are using some flea-power battery.

You might find the following background notes useful. They are specifically for Android<>Arduino

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

Bluetooth is essentially serial-by-wireless. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Hi, thanks for answering.
Well, here I attach a photo of the setup that I have.

I know it makes no sense the bluetooth executing Void Setup commands, but that just happens everytime I send the String '49' (49=1=HIGH according to ASCII) to the BT.

Anyway, the setup is as follow, the motor board L298N is attached to 12V power supply and the 5V line of this L298N board feeds both Arduino Pro Mini and the Bluetooth, the pro mini also powers an RTC module (this RTC hasn't been involved in my scripts to test the BT).

I tested the HC-05 with my arduino Mega and it works great with any code I've tried, it turns the LED on and off, no problems; but it does nothing if attached to the pro mini.
Actually, any string or command I send from the Android App makes the pro mini built-in led blink for a second then it's off till next command from android.
It's like it gives some sign of life, but nothing more.

I've tried changing BAUD rates with AT+UART=<baud_rate> ... but nothing.

If you have another Pro Mini to try that would be good.
Hardware can be broken especially these cheap modules from China.

This is why I always buy 2 of each.

eliseo88:
I tested the HC-05 with my arduino Mega and it works great with any code I've tried,

OK. You have proven that:

  1. There is nothing wrong with Bluetooth
  2. Assuming that by, "any code", you also mean the code you use on Pro Mini, there is nothing wrong with it. Despite what I have said, it does the job. The first serial.print suffices for that. And changing the baud rate is utterly futile.

And the real question is why, when using Pro Mini, your success is intermittent......(?)

So all you need do now is

  1. test Pro Mini with the usual blink and hello serial monitor to ensure that it is indeed kosher. They don't have a history of unreliability, but it could be your connections that are the problem.
  2. test ALL the things that are different between using a Pro Mini and a Mega. That includes power supply and peripherals. If these are not the same when you have success with the Mega, I guess they are the things that are suss.

This is a bluetooth question. Looking at the picture, you have now introduced a clock, a poorly described unknown power source, a motor board, and whatever might be attached to it. In the meantime, you refuse to comment on the LED activity on the HC-05. The least you can do is assure us that no motors are connected. I understand that stepper motors can consume power even when stationary.

Ok, I have nothing wired to these boards, no motors, no external LEDs (I'm testing the bluetooth with the pro mini on board LED). The RTC module is just turned on because wired to the pro mini, but I'm not using it in these tests for the bluetooth.
The power supply is a 12V - 2A power supply (from and old EXT HDD).

My serial connection should be ok, I used this setup previously, without BT, to test motors and sensors and the feedback was ok, I had all the data on serial monitor.

As I say in my opening post, the bluetooth execute the code from the Void Setup, which is blinking for 3 seconds..lately I've changed 3 sec to 0.3 sec, and in my last post I say that every command I send from the android app makes the LED blink for a second..what it's actually doing is blinking for 0.3 sec.
It's like every command I send tells pro mini to run the code from Void Setup to Void Loop etc., it's some kind of Reset.

I'll test with my second pro mini, but I've tried with an Arduino Nano, and BT just does the same stuff.

OK, despite my misgivings about the power supply, 12v 2A and no motors really should be adequate.
The fact that you get some sort of result should mean that all the hardware and wiring is OK, particularly if the problem is consistently the same..

There is also the matter of language. Your code should have the LED stay on solid for three seconds, not blink. If it blinks, it a a characteristic of the LED.

The code doesn't seem great at a glance, but I have never switched a LED, so I don't really know. I just can't see what would cause a reset. The links in reply #2 have test code that tests comms Arduino<>Android.