Leonardo and HC05 bluetooth device

Hello together,

I'd like to operate my leonardo without a cable connection. The power connection was easy through the jack, but replacing
the usb-cable with a bluetooth device is giving me a headache...

I bought this HC05 device on ebay: http://www.ebay.de/itm/300923286111?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

It has five pins: KEY, Rx, Tx, 3.3V, 5.0V, GND

I've connected these as follows:
KEY and 5.0V empty (since I do not want to change any settings and on ebay it said 3.3V...).
RX on BT -> TX on Leonardo
TX on BT -> RX on Leonardo
GND to Leonardo GND

Now, when I power up the device, my Laptop finds a HC05 which I can pair with the code 1234.
The LED then goes from blinking rapidly to slowly.

Now I wanted to connect and send some signals.... I know that I have to use serial1 instead on serial on the leonardo.
So, I tried the following:

setup{
Serial1.begin(9600);
}

loop{
Serial1.print("Hello")
}

The thing is, that I can not receive anything... My PC finds two COM-ports (7 and 8) and I also can connect to these.
However I can not see any Hello on either of these.

What did I do wrong?

Thanks :slight_smile:

and on ebay it said 3.3V

Which is not the same as not powered, which is what it appears your device is now.

Using that device with a 5V Arduino is not a really good idea. Perhaps a 3.3V Due would work better, but a 5V bluetooth device is probably cheaper.

The usual issue is the rx/tx wiring - double check it.
If the Leonardo is 5V then use a divider for the tx signal (from Leonardo to HC-05), as those modules are 3V. Or at least use 1k resistors in series - put it in the rx and in the tx line, for example.
You are missing ; in your code, btw.
The easiest check is the touching the rx line of the hc-05 with 3V3 and/or GND - you shall see random characters coming into your PC-terminal.
Also when you connect tx and rx line of the hc-05 together you shall see (on your PC) the characters you type in (a loop).

Elarion:
I bought this HC05 device on ebay
It has five pins: KEY, Rx, Tx, 3.3V, 5.0V, GND

I've connected these as follows:
KEY and 5.0V empty (since I do not want to change any settings and on ebay it said 3.3V...).

The LED then goes from blinking rapidly to slowly.

What did I do wrong?

Probably not much, although I think it would help to buy from somebody who knows what he is selling. Those HC-05s are indeed 3v3 devices but the the boards they are on typically allow you to use 5v, and there is nothing to suggest the slightly atypical board you are using is any different. The 5v pin is clearly identified.

I don't know how different a Leo is from a plain-vanilla Uno, but on the Uno you don't do anything to communicate. The slow pulse indicates a connection, you do the rest in Windows, not Arduino. Disconnect the USB cable. Do not use the serial monitor. Establish the connection in Windows and then use a separate terminal programme like RealTerm. My blueteeth are on COM 40,41 and 42. Yours should be similar. The commands are the same as if you were using the serial monitor.

Wow, so many anwers, thank you :slight_smile:

First off: I forgot to mention that I powered the device with 3.3V and not 5V :slight_smile:
5V is empty and 3.3 is connected to 3.3 :slight_smile:

Since the device was always offline, I installed the Toshiba BT Stack. I was once able to get some Hellos, but not anymore... at least a partial success :slight_smile:
So, do I still need voltage dividers if I was able to receivce some hellos once?

BTW: Why cant use the serial monitor?

There was no need to use the 3v3, but evidently no harm if you do, and there should be no need to change anything on that front. You don't say how you read the signal. Are you using RealTerm?

I'm using HTerm 0.81 Beta since I'm familiar with it.

I could read the signal immediately after installing the toshiba stack. Now when I want to connect all I get is again a device that is marked as offline....
So, my Wiring is correct?
Why can't I use the Serial monitor?

Elarion:
I'm using HTerm 0.81 Beta since I'm familiar with it.

OK. One term is much the same as another, and If you have had a result with it, stick with it.

I could read the signal immediately after installing the toshiba stack. Now when I want to connect all I get is again a device that is marked as offline....

I don't know anything about Toshiba. I believe my laptop has it but I didn't install it. There has been discussion on this forum about it. I'm a bit suss about it. My experience is that, once I had bluetooth going, it works perfectly ever after. For months, my only communication was by bluetooth. Note that my experience is confined to Uno and Mega, but the big secret I found, is that there is no secret....

So, my Wiring is correct?

Probably

Why can't I use the Serial monitor?

I think this an issue at the PC end, probably about use of COM ports. Arduino neither knows nor cares about who is listening on bluetooth. You can have a desktopwith serial monitor and USB cable and a laptop on bluetooth at the same time.

Okay, so I do not need a voltage divider, thats good :slight_smile:

Btw: No I'm again getting a Signal. The last thing I did yesterday was reinstalling the Toshiba Stack.
Now today, I powered everything up, operated the Leonardo via the accu pack and the jack and then paired the device.
It didn't get marked as offline after that, so I connected HTerm on COM3, which is one of the two COM-ports created.
Com 3 is HC-05 "Device-B" and Outgoing and Com 7 is incoming and just HC-05. Wherever the difference is.

Yesterday I flashed the following code on the Leonardo (I'm using Eclipse Uni with arduino plugin):

// Do not remove the include below
#include "Bt.h"

//The setup function is called once at startup of the sketch
void setup() {
	Serial1.begin(9600);
}

// The loop function is called in an endless loop
void loop() {
//	Serial1.println("Hello");

	String text = "";
		while (Serial1.available()) { // Prüfen, ob mindestens ein Zeichen vorhanden ist

			char ch = Serial1.read();
			text.concat(ch);
		}

		if (text.length() > 0) {
			text.toLowerCase();

			//Alle verfügbaren Zeichen eingelesen. Nun Werte extrahieren
			int i = text.indexOf("p");
			int j = text.indexOf("i");
			int k = text.indexOf("d");

			if (i != -1) {
				double rP = atof(text.substring(i + 1, i + 6).c_str());
				Serial1.print("P new: ");
				Serial1.println(rP);
			}
			if (j != -1) {
				double rI = atof(text.substring(j + 1, j + 6).c_str());
				Serial1.print("I new: ");
				Serial1.println(rI);
			}
			if (k != -1) {
				double rD = atof(text.substring(k + 1, k + 6).c_str());
				Serial1.print("D new: ");
				Serial1.println(rD);
			}
		}
}

Now with this I now get:
P new: 0.00
When I enter p20, which is a little strange since it should say P new 20.00 but hey, I can receive and send something.
Also the flashing goes from 1Hz after pairing to "blibk blink pause blink blink" when I connect to the device at COM3 which is nice :slight_smile:

I now am going to flash another program on the leonardo and ivestigate everything further but the problem seems to be that windows is often marking the device as offline....

Okay, now I know how to get a connection (at least I can reproduce it).

First off: forget about Offline. Even though it says offline, it actually isn’t since once I connect with a Terminal program it has a connection :slight_smile:
But: The problem is the COM-Ports!!
If I cut the power to the arduino and the hc05 connected on it, I can pair the device (of cause after 10 secs offline) and then I can see two Com-ports in the device manager :slight_smile:
Then I can also connect and see the data of my IMU :slight_smile:
However, if I then flash another program on the arduino I cannot just connect again. At least not on most cases. 1 out of 10 times it works to just reconnect, but in general I have two unplug everything again, since just removing hc05 in windows and connecting it again is ok in the case of pairing, but the com-ports aren’t visible in the device manager and thus I can’t get a connection (com port can’t be opened in hterm).

So, after cutting the power, removing the device and then pairing again it works :slight_smile:

So, what have we got so far:
Connection works with 3,3V, GND RX to TX and vice versa. No key and no 5V pin connected. Voltage dividers are not necessary. Windows 8 needs the Toshiba BT stack for a connection. It says only for Toshiba but it works with nearly all devices I suppose. I had to use the silent setup for x64.
After that I get the behaviour described above.

My questions for you:

  1. I wasn’t able to understand why I cannot use the serial monitor. It doesn’t work, I can see that, but why???

  2. Is there any way to flash a new program on the leonardo and then get BT to work, without resetting everything again?
    Would a simple power switch help so that I could power off the HC05 seperately again?

  3. Is there a way so that my Laptop can connect with the HC05 automatically? Manual connection is also ok, but automatic connection would be much better!
    I’ve read something about an AT-mode to change the settings of the device?

  4. Is there any other terminal program that can display tabulars and other characters like the degree-symbol? I’m always seeing just a rectangle for the degree-symbol and \t for a tabular…

  5. Often if I enter a long string the leonardo is too fast, so that the buffer hasn’t filled and I end up with just a substring. Is there any other way to avoid this instead of a dealy? A delay isn’t an option since I have a pid-controller and delays make that one instable…

noCOM.JPG

COM.JPG

Unbenannt.JPG

Elarion:
If I cut the power to the arduino and the hc05 connected on it, I can pair the device

Really?

1 out of 10 times it works to just reconnect, but in general I have two unplug everything again, since just removing hc05 in windows and connecting it again is ok in the case of pairing, but the com-ports aren't visible in the device manager and thus I can't get a connection (com port can't be opened in hterm).
So, after cutting the power, removing the device and then pairing again it works :slight_smile:

I think the best procedure is to set it up and debug with the serial monitor and then shut down and change to bluetooth.

Connection works with 3,3V, GND RX to TX and vice versa. No key and no 5V pin connected. Voltage dividers are not necessary. Windows 8 needs the Toshiba BT stack for a connection. It says only for Toshiba but it works with nearly all devices I suppose. I had to use the silent setup for x64.

Yes I'm using the stack on a Dell with XP

After that I get the behaviour described above.

  1. I wasn't able to understand why I cannot use the serial monitor. It doesn't work, I can see that, but why????

The serial monitor and the bluetooth use the same internal channels.

  1. Is there any way to flash a new program on the leonardo and then get BT to work, without resetting everything again?

I don't think so but, once the programme is loaded, you don't have to worry about this.

Would a simple power switch help so that I could power off the HC05 seperately again?

You could do that but I don't see the point.

  1. Is there a way so that my Laptop can connect with the HC05 automatically? Manual connection is also ok, but automatic connection would be much better!
    I've read something about an AT-mode to change the settings of the device?

Not really. I use a desktop icon to access the Toshiba bluetooth settings window and a another icon for RealTerm which starts with the proper configuration.

  1. Is there any other terminal program that can display tabulars and other characters like the degree-symbol? I'm always seeing just a rectangle for the degree-symbol and \t for a tabular...

You may be able to do this by sending ASCII numbers. I have never included them in a data transmission.

  1. Often if I enter a long string the leonardo is too fast, so that the buffer hasn't filled and I end up with just a substring. Is there any other way to avoid this instead of a dealy?

You could try a higher transmission rate. Otherwise, probably not.

Maybe the leonardo and/or the pid-controller are the reason, but I don't know why you are using a bluetooth library. I just send and receive serial. I don't know what a pid-controller is.

This code is all that I need, and maybe all you need. In practice, I just collect data.

void setup()
{
    Serial.begin(9600);
    Serial.println("OK then, you first, say something.....");
    Serial.println("Go on, type something in the space above and hit Send, or just hit the Enter key"); 
}
 
void loop()
{
  while(Serial.available()==0)
  {}
  delay(500);
  Serial.println("I heard you say:      ");
  while(Serial.available()>0)
  {
    Serial.write(Serial.read());// note it is Serial.WRITE
  }
  Serial.println("");
}

Nick_Pyner:
If I cut the power to the arduino and the hc05 connected on it, I can pair the device

Really?

Yes, because otherwise the problem is that after flashing the leonardo I cannot get a connection and if I just repair the device I do not have the com-ports recreated. So this is why I need to cut the power of the bt-device.
This is also why I thought of installing a switch, so that I can power on/off bt alone without cutting the power of the imu or the leonardo.
With that background, do you think that this is a good idea?

I think the best procedure is to set it up and debug with the serial monitor and then shut down and change to bluetooth.

Yeah, probably, still I'd like a simpler way when my program will be finished

Yes I'm using the stack on a Dell with XP
After that I get the behaviour described above.

Do you also have to cut the power after flashing something on your arduino?

  1. I wasn't able to understand why I cannot use the serial monitor. It doesn't work, I can see that, but why????

-->The serial monitor and the bluetooth use the same internal channels.

I don't get that either, sry... Bt is just sendung on a com-port, lets say com7. The serial monitor should just listen to that port and display it, right?
Where is a channel conflcit?

  1. Is there any way to flash a new program on the leonardo and then get BT to work, without resetting everything again?

I don't think so but, once the programme is loaded, you don't have to worry about this.

Would a simple power switch help so that I could power off the HC05 seperately again?

--> You could do that but I don't see the point.

Answered above :slight_smile: Good idea?

  1. Is there a way so that my Laptop can connect with the HC05 automatically? Manual connection is also ok, but automatic connection would be much better!
    I've read something about an AT-mode to change the settings of the device?
    --> Not really. I use a desktop icon to access the Toshiba bluetooth settings window and a another icon for RealTerm which starts with the proper configuration.

Not even with the AT-Mode? I just want to avoid entering the pin over and over again. Something like the HC05 trying to establish the connection with my laptop once it's powered on would be really cool!

You may be able to do this by sending ASCII numbers. I have never included them in a data transmission.

Ah, okay since it is pure ascii my degree symbol and the escape sequence don't work. That makes sense :slight_smile: Still Hterm should be able to interpret a \t as a tab :smiley: Unfortunately it seems as it isn't able to do this...

  1. Often if I enter a long string the leonardo is too fast, so that the buffer hasn't filled and I end up with just a substring. Is there any other way to avoid this instead of a dealy?
    -->You could try a higher transmission rate. Otherwise, probably not.

Good idea, thanks! Gonna try that!

The pid is a controller for a closed control loop to maintain a certain output. In my case: stabilize a segway :slight_smile:

Thanks for your code! Is using Serial.write necessary if I collect all the data, concatenate it and then like to print it?

Frankly, I am surprised how many issues you are getting with the HC-05 module. I am using several HC-05s for few years with several boards and systems, I am even uploading sketches via HC-05, no issues.

  1. if your PC drivers are properly installed (I am using generic XP drivers) you need to pair it once (I mean entering the name and pass-code for each BT module) and that is it - I did it 2y ago, afaik, I've even forgotten the pass-codes :slight_smile:

PS: You MUST enter the names and pass-codes into the BT modules via AT command, unless you are using only a single module with default name and pass-code.

  1. when powering off and on the BT I have to connect to particular COMx only (ie in TeraTerm "new connection"). Or restart Teraterm - it connects automatically. BT works as a transparent COM. It depends on application how it connects. For example in arduino IDE I only upload the sketch to the board, no previous connect to COM15 is necessary..

  2. You can use several HC-O5 modules at once - just open several TeraTerm windows and select specific COM serial for particular terminal window

  3. flashing the Leonardo has none effect on your BT module. Your HC-O5 is seen by Leonardo as 2 wires only. The baud rates must match, of course..

As I wrote to you in my above replay, take a BT module, power it from 3V3, and connect its rx and tx together (no arduino needed). What you will write/type into your terminal window will come back into your terminal window. You can paste 1GB large text into your terminal window and you have/must get it back onto your screen via HC-05, no arduino required.

So do test your BT setup, you do not need to mess with Leonardo unless your BT connectivity does not work properly. You do not need an arduino to test your BT setup.

Elarion:

Nick_Pyner:
If I cut the power to the arduino and the hc05 connected on it, I can pair the device

Really?

Yes, because otherwise the problem is that after flashing the leonardo I cannot get a connection and if I just repair the device I do not have the com-ports recreated. So this is why I need to cut the power of the bt-device.
This is also why I thought of installing a switch, so that I can power on/off bt alone without cutting the power of the imu or the leonardo.
With that background, do you think that this is a good idea?

I think the best procedure is to set it up and debug with the serial monitor and then shut down and change to bluetooth.

Yeah, probably, still I'd like a simpler way when my program will be finished

Yes I'm using the stack on a Dell with XP
After that I get the behaviour described above.

Do you also have to cut the power after flashing something on your arduino?

I think you are just making things difficult for yourself by reading too much into the situation, and confusing setup from actual operations.

The serial monitor is used for debugging and the bluetooth is used for communication. Once you have it sorted, for which there is no need to actually use the bluetooth, you don't need the debugging and the channel conflict is irrelevant. You simply switch on the Arduino, the bluetooth lights up, and you are off and running.

Not even with the AT-Mode? I just want to avoid entering the pin over and over again. Something like the HC05 trying to establish the connection with my laptop once it's powered on would be really cool!

See above. The HC-05 is permanently available and trying to establish connection. Any smarts you want are done at the PC end. I do it by clicking icons, no PINs required.

Ah, okay since it is pure ascii my degree symbol and the escape sequence don't work. That makes sense :slight_smile: Still Hterm should be able to interpret a \t as a tab :smiley: Unfortunately it seems as it isn't able to do this...

I thought you were talking about special ASCII symbols but this is starting to smell like a simple formatting problem. If you want to produce CSV files, then comma separation is all you need. Hterm should be able to interpret \t anyway. RealTerm can, so it might be a good idea to defect.

Thanks for your code! Is using Serial.write necessary if I collect all the data, concatenate it and then like to print it?

That code is just for basic testing. Use it verbatim just to prove the system, thereby getting rid of that other chaff you have. It works exactly the same either with cable and serial monitor or bluetooth. I have used it with serial on desktop and bluetooth on laptop simultaneously, but it was two-way on laptop only.

In practice I use serial.print data more less like sending it anywhere else.

Serial.print(InTemp);
lcd.print(InTemp);
myFile.print(InTemp);

no concatenating of anything. Indeed, if I understand you correctly and speed is an issue, concatenating is the kiss of death.

Pito and Nick, thank you VERY MUCH!!

You opened my eyes here… The thing is that before installing the Toshiba Stack my device was always offline and I could not get a connection. This is why I thought that an offline device means that something is wrong… but it isn’t!! This is why I started to repair it.
Now I started my Laptop and powered the arduino. Then, the led was flashing fast. After connecting to COM7 however I instantly had the readings of my IMU, without needing to repair anything again!!

Whats funny: After flashing the leonardo, hterm cannot get a connection, its true… I do not know why… Also serial monitor isn’t usable.
Tera Term however, which I downloaded since pito is using it, can!! Now I can have my connection established, flash the leonardo while the connection is still active and then after some seconds have my new program again!

Also, the degree symbol in ascii is 248, which works perfect :slight_smile: Tabulator is 09 which also works!! So I’m pretty happy with that setup :slight_smile:
Since I only have one dongle I do not need to change the pass in the at mode.

Ah, okay, I see that I can also send bytes with Tera Term. I can either send a file or do a right click and then select insert.
If I send a file, the content always gets transferred correctly. If I however enter a string via a right click like:
p19.05 i0.04 d0.67
I get an output like p19.05 i0.00 d0.60
So, as you can see some characters can’t make it because the buffer has no time to be filled. This is why atof then calculated wrong values. So, would increasing the baud rate help?

Edit:
Just found an old post of pito here:

There he puts in a delay of 3ms to let the buffer fill. WIth that delay the numbers come in correctly!!! So cool :wink:

Here’s the code that I use now to change the controller values over BT:

void readPID() {

	String text = "";

	while (Serial1.available()) { // Prüfen, ob mindestens ein Zeichen vorhanden ist
		delay(3);  //delay to allow buffer to fill
		char ch = Serial1.read();
		text.concat(ch);
	}

	if (text.length() > 0) {
		text.toLowerCase();

		//Alle verfügbaren Zeichen eingelesen. Nun Werte extrahieren
		int i = text.indexOf("p");
		int j = text.indexOf("i");
		int k = text.indexOf("d");

		if (i != -1) {
			rP = atof(text.substring(i + 1, i + 6).c_str()); //Bis zu zwei Nachkommastellen
			controller.SetTunings(rP, rI, rD);
			Serial1.print("P new: ");
			Serial1.println(rP);
		}
		if (j != -1) {
			rI = atof(text.substring(j + 1, j + 6).c_str());
			controller.SetTunings(rP, rI, rD);
			Serial1.print("I new: ");
			Serial1.println(rI);
		}
		if (k != -1) {
			rD = atof(text.substring(k + 1, k + 6).c_str());
			controller.SetTunings(rP, rI, rD);
			Serial1.print("D new: ");
			Serial1.println(rD);
		}
	}
}[\code]

So, even if delay(3) fixes my last issue and everything seems to be perfectly working due to your help, I'm still interested in how to enter the AT mode and change some settings. Cause maybe in future I'd like to use more of these HC05 :)

Is the KEY-Pin on the board necessary for this?
How to enter the mode easily?

Thanks so much for your help :) So cool to have it working!

I'd like to use a divider for the tx signal from the leonardo to the device. Which resistors should I use?
What's the best option?

All you need to make a simple 5V to 3.3V voltage divider, is 3.6Kohm and 1.8Kohm resistors :slight_smile:

Where R1 is the 1.8Kohm resistor and R2 the 3.6Kohm, Vin is the TX pin of Leonardo and Vout the RX pin of HC05

Vout = ( Vin * R2 ) / ( R1 + R2 )

Thanks :slight_smile: Since I do nor have the resistors here, I ordered them.
I know about voltage dividers but since the division factor depends on the ratio of the resistors and the current on the magnutide of them, I wasn't sure wich to use.
Since it's a tx pin however currents can be expected to be low, so thank you very much for your help!

Questions:

  1. Would such a solution also be okay for the KEY-Pin when I want to enter AT-mode? Or would a 3.3V voltage regulator be better?

  2. I've read that if I pull the key-pin high while powering on the device, it enters the AT-mode. Is that correct? So is the key-pin used for powering it up?
    Is it achievable via software to power the key pin high while powering the bt-device up? Since it is connected to the +3.3V-pin, isn't my only chance to also connect
    the key-pin to +3,3V while powering it up? Isn't any software solution pulling a pin high too late?
    What is the easiest way to get into the at-mode?

You might find these useful

ftp://imall.iteadstudio.com/Modules/IM120723009/DS_IM120723009.pdf

Yeah, especially the first document is nice, thanks!
It tells me how to enter the At-mode and according to it, I can add it at any time, which is good!
Still, key seems to be 3,3V, right? I also couldn't find anything about voltage dividers in it, so:

Can I set up two 5V -> 3.3V dividers with 1,8 and 3k6 ohm resistors, one for the tx-> rx line and the other to the key-pin?