After a chat with OP to debug connections, here is how the problem was (somewhat) solved:
HC-05 should be connected:
- VCC and GND to the Arduino's VCC and Ground respectively
- TX of the Arduino (the designated TX pin; the second argument of the SoftwareSerial declaration) goes into a resistive divider of 1k:2k, and the output goes into the HC-05's RX pin
- RX of Arduino straight into TX of HC-05
If hardware serial is used to talk directly to the HC-05 from the Serial Monitor (i.e. using it as a USB-to-TTL converter by bridging RESET to GROUND) then the RX and TX will be reversed on the Arduino - the Uno's RX pin goes into the resistive divider and Uno's TX goes directly to HC-05 Tx. If the Arduino is connecting using Serial (normal hardware serial used as Serial.read() and Serial.write() in code) then it is conventional (un-reversed) with pin 0 going into the TX of the HC-05 and pin 1 going into the voltage-divider.
AT commands should be issued with Newline set to "both NL and CR" - thats what the HC-05 expects and AT commands should be issued at 38400 baud.
It had become apparent that SoftwareSerial was the culprit here as connecting the HC05 to the hardware serial pins worked (pin 0 to HC-05 TX and pin 1 to HC-05 RX through voltage divider), for:
Talking directly to the HC-05 using the Arduino as a USB-to-TTL converter - at 9600 baud, messages from phone over Bluetooth can be sent and received from the Serial Monitor
Replacing the original code SoftwareSerial with just Serial, and including a character as a receive buffer and testing this character against '1' and '0' to turn on the LED also worked. See code below:
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Serial link established.");
}
char recv;
void loop() { // run over and over
if (Serial.available()) {
recv = Serial.read();
if (recv == '1')
Serial.write("on"); //No newline produced here
else if (recv == '0')
Serial.write("off");
}
}
If you want to use Strings, then you have to make a character array and every time Serial is available, write to a new indexed character of that array and increment indexer - break this loop when '\n' (newline) is received and reset the indexer counting variable, for example. This is because Serial.read() returns a char at a time, so an entire string cannot just be read as the original post might suggest. This is also why Serial.println()
printed the character one by one followed by a newline - println
ends with a newline and Serial received one byte at a time.
OP's code was changed to utilise normal hardware serial for communication with the HC-05 which avoided the use of Software Serial. If anyone knows why Software Serial causes this, please comment why.
println
withwrite
? Also, please give an image showing what the Arduino sends back (the "ascii characters and new lines" ) just so its clearer to debug what you mean here \$\endgroup\$AT+UART?
. Connect TX to TX (yes, you heard that right) via the resistors and RX to RX \$\endgroup\$