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.

  • 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). – Mikael Patel Jan 21 '18 at 22:37
  • Make sure your RX and TX pins actually connect to the right pins. TX to RX and RX to TX. – Majenko Jan 22 '18 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 Jan 22 '18 at 20:45
  • As Mikael noted, 38400 baud is a challenge for SoftwareSerial to reach without errors. Try 9600. – jose can u c Jan 22 '18 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 Jan 23 '18 at 20:14
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.

  • 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 – Michel Keijzers Jan 23 '18 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 Jan 23 '18 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 Jan 25 '18 at 8:55
  • 1
    @MichelKeijzers I have updated the answer to include the full description. – xaratustra Jan 25 '18 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 Jan 25 '18 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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