I'm trying to simply access my HC_05 bluetooth module and conduct some AT commands through the Serial Monitor. For some reason though, it only seems to be returning Null characters, though it does some to be actually responding to the command in some way (given by the length of Null characters it returns.
enter image description here
So you can clearly see that the command AT
and AT+NAMEdevice
return different data, but they are both all NUL
blocks. I am viewing the monitor in text. Changing the view to Hex or ASCII does not change results.
Code for this simple program is below:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // 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());
}
Wire Diagram is as here
NOTE I have used both a direct connection to the 5V
and also used a Voltage Divider. Neither changes the NUL
return.
2 Answers 2
First, change all Serial.write
to Serial.print
I think that might be half your problem.
Next,
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
The way you are doing this will probably put the HC-05 into 9600 baud mode. You can fix this one of two ways:
Change BTSerial.begin(38400);
to BTSerial.begin(9600);
Or, as jfpoilpret suggested, tie the Key pin directly to VCC. The reason is that in order to go into 38400 baud mode, there must be voltage on Key at the time the module is powered up. By the time the digital write in your code happens, it's too late.
If for some reason you'd like to have digital control over the Key pin rather than directly wiring it to VCC, yet you'd like the module to operate at 38400 baud, you can simply change the default baud rate by issuing the appropriate AT command - I believe it's AT+UART=
off the top of my head.
Also, I'm not sure, but jfpoilpret might be right as well about how your serial read/write handling is done within loop() but here is code that I know works:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
char myChar;
void setup()
{
...
}
void loop() {
while (BTSerial.available()) {
myChar = BTSerial.read();
Serial.print(myChar);
}
while (Serial.available()) {
myChar = Serial.read();
Serial.print(myChar); //echo
BTSerial.print(myChar);
}
}
This sounds like baud-rate mismatch. These dongles don't have a reset ability; they remember their most recent settings. See my answer to another user having trouble communicating with one these.
while (BTSerial.available())
andwhile (Serial.available())
instead ofif...
?