I have a DSD Tech HC-05 bluetooth module connected to an Arduino Mega 2560. It is able to connect to a remote master and transmit and receive data to the Arduino, verified using the BluTerm app on an iMac and using a simple pass-through program on the Arduino to pass serial data between the HC-05 serial port and the Arduino IDE serial port, both at 9600 bps. When I power up the HC-05 while holding the button, it enters terminal mode as evidenced by the LED blinking very slowly. However, the Arduino never sees any output from the HC-05 in this mode, e.g. no response to "AT" or "AT+VERS?". I first tried using 38400 bps for the HC-05 connection, then when that didn't work I tried 9600, 115200, 19200, 1200, and 2400. None work.
Here is how the HC-05 is connected:
Wall Wart ground to HC-05 GND
Wall Wart +5V to HC-05 VCC
Arduino Mega pin 17 (RX2) to HC-05 TXD
Arduino Mega pin 18 (TXD) to 1K resistor, other side of it connects to HC-05 RXD and to a 2K resistor that connects to ground.
Here is the code, at 38400 for the HC-05:
void setup() {
Serial.begin(9600);
Serial2.begin(38400); // Required bps for AT mode?
}
void loop() {
if (Serial2.available()) Serial.write(Serial2.read());
if (Serial.available()) Serial2.write(Serial.read());
}
Some posts suggest setting pin 17 (RX2) to input with pull-up, but that makes no difference, and besides, it can receive from the HC-05 just fine in regular mode.
Any suggestions?
Addendum with answer:
I was working with the wrong data sheet, the HC-10 rather than HC-05. Reading the correct one, I saw something and tried it out and sure enough, that was the problem. What was it? Every command must end in '\r\n' (carriage return followed by line feed). But the Arduino was sending only a line feed '\n' at the end of each line. Adding '\r' before it was all it took to make it work! Here is the new code:
void setup() {
Serial.begin(9600);
Serial2.begin(38400); // Required for AT mode
}
void loop() {
if (Serial2.available())
Serial.write(Serial2.read());
if (Serial.available()) {
char ch = Serial.read();
if (ch == '\n') Serial2.write('\r');
Serial2.write(ch);
}
}
A sad discovery
After getting the HC-05 working, I discovered I can't connect to the iPhone, the HC-05 doesn't work with IOS, apparently due to some sort of legal issue. I'm going to switch to an HM-10.