HC-05 returns garbage in AT mode

I recently got an HC-05 bluetooth module which i want to use for a project.

Entering AT mode and sending commands is no problem....but the response from the module is just "rubbish".
For example, if i send the "AT\r\n" command to see if the module is in AT mode, i get back 4 unprintable bytes ( 207 167 134 133) instead of "OK\r\n".

Well, i'm a bitt clueless what to problem is... anyting with stopbits and parity ? or simple a defect bluetooth module ? Any help/ideas are appriciated...

#include <Arduino.h>
#include <SoftwareSerial.h>

#define USE_SEROUT
#include <cout.h>

#define Rx 5
#define Tx 4
#define KEY_PIN 3

SoftwareSerial bluetooth(Rx, Tx);

void readSerial() {
	if (bluetooth.available() <= 0) {
		cout << "nothing received" << endl;
		delay(2000);
	} else {
		cout << "available " << bluetooth.available() << "bytes: ";
		String s;
		while (bluetooth.available()) {
			char c = bluetooth.read();
			cout << (uint8_t)c << " ";
			s.concat((char)c);
		}
		cout << endl << "answer: " << s << endl;
	}
}


void sendATCmd(const char* cmd) {
	digitalWrite(KEY_PIN, HIGH);
	bluetooth.print(cmd);

	delay(2000);
	readSerial();
}


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

	bluetooth.begin(38400);
	digitalWrite(KEY_PIN, HIGH);
}

void loop() {
	sendATCmd("AT\r\n");
	delay(2000);
}

Can you post a screen shot of what your getting? Try setting the bluetooth baud rate to 9600.

My modules are not called HC-05. My modules only use "AT", no new lines or carriage return. Have you tried without them?

Try different bit rates. If it were REALLY "hayes compatible", it would autobaud on the "A", but as far as I know, hardly anyone does that anymore.

thanks for all your answers

thats the module i'm using....
http://www.ebay.com/itm/301054999279?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2648

and here is the manual

@HazardMinds
The expected answer should be "OK\r\n" which is 79 75 13 10 if the chars are printes as integers.
I'm receiving the 4 value: 207 167 134 133 which are all in the extented asciii range

@westfw
i tried different baude rates - but except for 38400 i don't receive any response in AT mode. I didn't try the communication mode yet sind for my project i only need the AT mode...

any other ideas ?

If you never tried communication mode, how do you know the baud rate is right or the module is good? Since you buy from ebay, you should assume this may be a differet module than they say it is. Even tried without \r\n as I asked? Took one second only and did you try?

liudr:
If you never tried communication mode, how do you know the baud rate is right

because the 38400 baurate is the default speed in AT mode 2

liudr:
or the module is good?

I just tried the communication mode...same result. With 38400 i'm getting the correct amount of bytes, but not the right values....

liudr:
Even tried without \r\n as I asked? Took one second only and did you try?

yes, i tried without the "\r\n" before asking here ;). The module wont even respond in AT mode - in command mode it obviously doesnt matter.

So the communication mode is not working either. No point trying AT mode until you find out what went wrong.

Do you have access to a logic analyzer?

cider101:

liudr:
If you never tried communication mode, how do you know the baud rate is right

because the 38400 baurate is the default speed in AT mode 2

liudr:
or the module is good?

I just tried the communication mode...same result. With 38400 i'm getting the correct amount of bytes, but not the right values....

liudr:
Even tried without \r\n as I asked? Took one second only and did you try?

yes, i tried without the "\r\n" before asking here ;). The module wont even respond in AT mode - in command mode it obviously doesnt matter.

liudr:
So the communication mode is not working either. No point trying AT mode until you find out what went wrong.

Do you have access to a logic analyzer?

No, i don't have access to a logic analyzer...but.... i changed the code from using the soft-serial to use the hardware serial instead - and guess what... it works! So i guess i must be using the SoftSerial in a wrong way...just don't know whats wrong

I failed to pick up the "cue". Software serial is not fit for 38400 baud rate or just barely handles this rate. Once you enter AT mode and reduce baud rate to something like 19200 then try software serial again.

liudr:
I failed to pick up the "cue". Software serial is not fit for 38400 baud rate or just barely handles this rate. Once you enter AT mode and reduce baud rate to something like 19200 then try software serial again.

great...the implementation of the SoftSerial contains tables for speeds up to 115200, isn't able to handle 38400 and doesnt even let you know its not able.

I'll see if i can set it to 9600 and use AT-Mode 1 - in the AT-Mode 2 (which i'm currently using) the baudrate is fixed at 38400....

Maybe a stupid question, but...: Using the HW-Serial, everything which is sent to the Serial is also echoed on the serial monitor. But how can i echo/display the answers of the bluetooth module in the monitor without "sending" it again ? If i do something like

String s;
while(Serial.available) {
   s.concat((char)Serial.read());
}

nothing is display unless i print it out again (which then confuses the BT module) uhm...hope you understand what i mean.

great...the implementation of the SoftSerial contains tables for speeds up to 115200, isn't able to handle 38400 and doesnt even let you know its not able.

Depending on what else the code is doing, and how much data is being handled, SoftwareSerial CAN handle data correctly at 115200. But, if there is a lot of other interrupt-based stuff happening, or you are sending War and Peace to the Arduino via a software serial port, there will be problems.
'
The usual solution is to quit being a cheapskate and get a Mega with 4 hardware serial ports. Quit blaming tools you don't understand.

PaulS:
The usual solution is to quit being a cheapskate and get a Mega with 4 hardware serial ports. Quit blaming tools you don't understand.

getting a Mega is not an option - not because of the additional 10 bucks, but because i only need 1 serial port (in the project).
I admit, my comment above was a little harsh - and i appologize for that . But i was somewhat upset after 10 hours of try end errors to get that stuff working.

Anyway - whats wrong with my code above ? I'm not sending peace and war over the line nor is there anything else happening. Just sending 4bytes and receiving 4 bytes - that shouldn't be to much workload.

btw: i also tried to code from Instructable:

SoftwareSerial BTSerial(5,4); // RX | TX

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{

  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());

  // Keep reading from Arduino Serial Monitor and send to HC-05
  if (Serial.available())
    BTSerial.write(Serial.read());
}

same results...receiving the correct amount of bytes - but not the correct values

but because i only need 1 serial port (in the project).

Really? Then, why are you not using the one hardware serial port, then?

You are using TWO serial ports - one hardware and one software.

You probably shouldn't be waiting for all n bytes to arrive IN THE BACKGROUND while you sleep for two seconds. Swserial needs to go through some serious hacks to pretend to have interrupts. I'm not sure exactly how that works, but...

PaulS:

but because i only need 1 serial port (in the project).

Really? Then, why are you not using the one hardware serial port, then?

You are using TWO serial ports - one hardware and one software.

I know i'm using TWO ports in the example - but just because i want to "debug" the BT-Module (or better say - figure out what/which AT commands i need in my real project) - once the debugging session is done, there is no need for a second serial port.
Again, - whats wrong with my code above ? There must be something - otherwise it would work (which it obviously doesnt)

westfw:
You probably shouldn't be waiting for all n bytes to arrive IN THE BACKGROUND while you sleep for two seconds. Swserial needs to go through some serious hacks to pretend to have interrupts. I'm not sure exactly how that works, but...
hmmm... I can/will try that - I assumed, the soft-serial is using pin-interrupts since there is code for that...and the example form instructable doesn't do any sleeps...

Sorry for digging up a old topic but it was the only one I could relate to.
I had exactly the same issue. I was loosing my mind.
Turned out that using a beta version of Arduino IDE is a big no-no.
Probably there is a bug in software serial library.
I was using 1.5.8 beta. When I downgraded to 1.0.6 it worked just fine.
I hope it helps someone in the future.

I had the same issue.AltsoftSerial library is worked for me.

http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html