Strange problem with JY-MCU (HC-06) Bluetooth module

arduinobluetoothserial

Short explanation: data sent over the bluetooth serial does not arrive correctly to the Arduino.

Yesterday I connected a JY-MCU bluetooth module to my Arduino UNO R3 to test it. I connected the TXD and RXD pins to Arduino pins 10 and 11 respectively and used SoftSerial to communicate. I first tried some AT commands that I found online. After AT returned OK, I ran the code found here and got this output:

Starting config
OK
OKlinvorV1.6


OK57600
Done!

So the name and pin changed failed, but I (supposedly) changed the baud rate successfully. However, any further attempt to run any AT command failed (no output), even when I set the new baud rate (57600) in the soft modem. I then tried to pair it with my PC and used the following code to test:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);

void setup() {
  Serial.begin(9600);
  mySerial.begin(57600);
  delay(1000);
}

void waitForResponse() {
    if (mySerial.available()) {
      while (mySerial.available()) {
        int a = mySerial.read();
        Serial.println(a);
      }
      Serial.write("\n");
    }
}

void loop() {
  waitForResponse();
  delay(2000);
}

In short, I wait for the module to receive something and then print the value of every byte. What's strange is that the data I send (using cutecom on the PC) is simply not correct – for example, sending the letter A (either lowercase or uppercase) outputs the number 161. However, sending two letters A outputs the numbers 161 and 176 despite the fact they are the same letter. I have very little experience with serial communication, but this looks like the baud rate is not correct. However, if this is the case, I don't know how to change the baud rate back to 9600 since I can no longer send AT commands to the module.

Any help on how to get the module to work properly or at least an explanation on what is happening here is more than appreciated.

Best Answer

The baud rate 57600 is a pretty marginal on an AVR (the microcontroller used) at 16 MHz, normally OK with only a bit over 2% error but some modules can be picky. The site WormFood's AVR Baud Rate Calculator shows some good tables on error percentages for various clock / baud rate combinations.

Also I just noticed Majenko's comment and he's quite right the software serial you're using will be worse at that speed so for a start try it using a hardware UART.

That Arduino runs at 5 V logic levels so if you're not using a voltage divider to drop the transmit line to 3.3 V that might be worth try first. I've read a few mixed things about whether the HC modules are 5 V tolerant on the serial lines or not and it certainly won't do any harm trying it first to see if it resolves it.

But if neither of those steps work I'd recommend getting a USB to TTL serial converter, it seems for that module you should look for one with a 3.3 V logic level like this one for Sparkfun. They are also useful for a host of other things so are generally useful to have at your disposal.