0
\$\begingroup\$

I have an Arduino Uno, HC-05 module and an LED. I want to connect the HC-05 to my phone and with an app be able to turn on and off the LED and at the same time sending a message from the Arduino to the Phone that the LED has been updated.

I have built this circuit: (But Rx is pin 12 and TX is pin 13 and with 1kΩ and 2kΩ resistors instead) th The HC-05 blinks properly (so I assume it's not broken).

Here's the Arduino code: enter image description here (I tried to put the code using the code sample button but it didn't work properly sorry)

Now, I have two problems with the system:
- bt.println(...) should send text to the phone but the phone side doesn't receive anything.
- Serial.println(LEDState) should print what the phone has sent to the Arduino and it does but it is all ASCII characters and are printed on new lines.

I believe the problems are connected with the baud rate but I tried using 9600, 38400 and both mixed but it doesn't work. I also tried reversing the places of Rx and Tx in the SoftwareSerial initialization.

\$\endgroup\$
9
  • \$\begingroup\$ Rx argument comes first in the Software Serial declration line - ensure this is correct in your hardware. Also, check the resistors are connected to the right pins - you may have reversed the declaration but the resistive divider might still be wrong. An image of your setup would really help here. \$\endgroup\$
    – QuickishFM
    Commented Jan 11, 2020 at 12:33
  • \$\begingroup\$ @QuickishFM I fixed the SoftwareSerial RX and TX positions but now no information is being sent or received anywhere. \$\endgroup\$ Commented Jan 11, 2020 at 12:37
  • \$\begingroup\$ Could you try replacing println with write? 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\$
    – QuickishFM
    Commented Jan 11, 2020 at 12:39
  • \$\begingroup\$ @QuickishFM I changed all println to write (except Serial.println(LEDState) because it didn't allow me) and still no information flowing. I tried changing the baud rate to 38400 as well but it didn't work. \$\endgroup\$ Commented Jan 11, 2020 at 12:43
  • \$\begingroup\$ Can you connect to the HC-05 using hardware serial, go into AT mode and find out the UART rate? Type AT+UART?. Connect TX to TX (yes, you heard that right) via the resistors and RX to RX \$\endgroup\$
    – QuickishFM
    Commented Jan 11, 2020 at 12:44

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.