1

I connect an HC-05 module to an Arduino nano with CH341 serial chip (voltage divider on RX pin). I am trying to make a bridge for a test, i.e. serial in, echo local and echo to Bluetooth and vice versa. I modified the echo code from here.

int incomingByte = 0;    // for incoming serial data

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX


void setup() {
    BTSerial.begin(38400);    // opens serial port, sets data rate to 9600 bps
    Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {

    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print((char)incomingByte);
    BTSerial.print((char)incomingByte);
  }

  if (BTSerial.available() > 0) {

    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print((char)incomingByte);
    BTSerial.print((char)incomingByte);
  }

under OSX I open two terminals. In the first terminal using screen /dev/cu.wchusbserial1410 9600 I see local echo as desired. But on the second terminal using screen /dev/cu.HC-05-SPPDev 38400 I see nothing after pairing. The only effect is that by typing the latter command, the module stops blinking rapidly and blinks only two times with long pause in between. Exiting the screen environment, then again blinks rapidly.

Appreciate any hints what I am doing wrong...

=====

intermediate success but not quite there yet:

I simplified the code and corrected the wrong connection. Local echo on serial works as usual, even between the two, characters are being transferred, but they are black question marks instead of why I typed.

/*
 * echo script
 */

byte incomingByte;

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(3, 2); // RX | TX

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

void loop() {
  if (Serial.available()) {
    incomingByte = Serial.read();
    Serial.print((char)incomingByte);
    BTSerial.print((char)incomingByte);
  }

  if (BTSerial.available()) {
    incomingByte = BTSerial.read();
    Serial.print((char)incomingByte);
    BTSerial.print((char)incomingByte);
  }
}

Any ideas?

========

Edited the Buadrate to 9600. Now everything works.

5
  • 2
    Might want to check the sketch logic. "BTSerial.available() > 0" and then "incomingByte = Serial.read();". Hum, and then all the echo statements. Also comments say 9600 baud but code says 38400 (which is tough for softwareserial). Commented Jan 21, 2018 at 22:37
  • Make sure your RX and TX pins actually connect to the right pins. TX to RX and RX to TX.
    – Majenko
    Commented Jan 22, 2018 at 10:45
  • thanks for both comments. Indeed the RX and TX pins were connected at the wrong position. I got a bit of success, but I am not quite there yet (see edited post).
    – xaratustra
    Commented Jan 22, 2018 at 20:45
  • As Mikael noted, 38400 baud is a challenge for SoftwareSerial to reach without errors. Try 9600. Commented Jan 22, 2018 at 21:36
  • yes now it works. both at 9600 baud works. But I wonder how to manage higher baud rates with HC-05, if software serial does not supper higher speeds?
    – xaratustra
    Commented Jan 23, 2018 at 20:14

1 Answer 1

1

Based on the above comments I provide here the full answer in the hope it will be useful in the future.

HC-05 has two modes. The Communication Mode and AT Command Mode. In the latter mode, HC-05 responds in kind of "config" way and many default settings of the communication mode can be change here. While the baud rate of the communication mode, and many other settings can be changed, the baud rate of the AT command mode is always fixed 38400 to avoid any confusions and mistakes. On the other hand, the default factory baud rate of the communication mode, i.e. before any change, is set to 9600. So the solution to my problem was to set the baud-rate to 9600 on my newly purchased HC-05 and I was able to make an echo bridge between HC-05 Bluetooth and serial. Here is the code:

/*
 * echo script
 */

byte incomingByte;

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(3, 2); // RX | TX

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

void loop() {
  if (Serial.available()) {
    incomingByte = Serial.read();
    BTSerial.print((char)incomingByte);
  }

  if (BTSerial.available()) {
    incomingByte = BTSerial.read();
    Serial.print((char)incomingByte);
  }
}

After pairing HC-05 with OSX, I was able to open two OSX Terminals with one of these commands in the first terminal:

screen /dev/cu.wchusbserial1410 9600

and in the other:

screen /dev/cu.HC-05-SPPDev 9600

Everything worked properly, i.e. typing in one terminal produced characters in the other and vice versa. For changing the default baud-rate in the communication mode, I found a very good tutorial which can be found here.

6
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review Commented Jan 23, 2018 at 21:09
  • 1
    To make this question and answer useful in the "Stack Exchange" way, can you please change your answer to summarize what the problem was. As it is it reads like "I had a problem" and "I solved it" which doesn't really help anyone else.
    – Nick Gammon
    Commented Jan 23, 2018 at 22:32
  • @LookAlterno It is not possible to "accept" a comment, whereas the solution was in the comment. So I had to answer the question myself. It does not even allow me to like a comment, the like buttons are off.
    – xaratustra
    Commented Jan 25, 2018 at 8:55
  • 1
    @MichelKeijzers I have updated the answer to include the full description.
    – xaratustra
    Commented Jan 25, 2018 at 9:38
  • 1
    Your amended answer is much better, thank you. It will hopefully help other people in the future who have a similar problem. :)
    – Nick Gammon
    Commented Jan 25, 2018 at 10:23

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.