Arduino Leonardo and HC-05 Bluetooth Module

I have a HC-05 Bluetooth module that works fine with an Arduino Uno board (I was able to send and receive data using a phone app and even enter the AT mode and make sure the settings are all right).

The problem starts when I try to connect the module to my Arduino Leonardo board. The only difference I know is the fact that I must use Serial1 instead of Serial, but that is pretty much it.

Data doesn't send from my phone to the module, and when I try to send data from the module, on my phone appears a single "?" character. And of course, I can't enter the AT mode either.
After this, I've tried the SoftwareSerial library and it has the same behavior as described above.

Can someone please explain what I'm doing wrong and how to make it work,
it would be much appreciated!

The following is the code that I've used with the SoftwareSerial lib:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3,2); // RX, TX

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
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

This is the wiring that I've done except for the Tx and Rx pins that I've changed in regards to the SoftwareSerial library usage.

on the picture is wrong board

Welcome to the forum

The topic title says Leonardo and the picture shows a Mega. Why ?

You say

Put the Bluetooth on Serial1 using pins 0 and 1, print to the Serial monitor using Serial.print() and do away with SoftwareSerial

Can you show the code used with the Leonardo?

Do you have the USB cable connected? While (!Serial) causes the code to wait for a valid USB connection, if that is not connected it will wait forever.

On a Leonardo, Serial1 is the Rx/Tx pins, Serial is the serial over USB to the computer. Commonly Serial1 would be used instead of SoftwareSerial, I don't see from your code how it would be used as a replacement for Serial.

It was just a picture I've found that matches my wiring, the board I use is Leonardo.

I have the USB connected to print on my pc what I receive from my phone.

Bellow I have the code with Serial1:

void setup() {
  // Open serial communications for USB connection
  Serial.begin(9600);
  while (!Serial) { // !Serial.available()
    ; // wait for Serial port to connect.
  }

  // set the data rate for the Serial1 (bluetooth module)
  Serial1.begin(9600);
}

void loop() { // run over and over
  if (Serial1) { // Serial1.available()
    Serial.write(Serial1.read());
  }
  if (Serial) { // Serial.available()
    Serial1.write(Serial.read());
  }
}

Loop should be:

void loop() { // run over and over
  if (Serial1.available()) { // Serial1.available()
    Serial.write(Serial1.read());
  }
  if (Serial.available()) { // Serial.available()
    Serial1.write(Serial.read());
  }
}

If (Serial1) checks to see if the Serial1 port exists, if (Serial1.available()) checks to see if any characters have been received.

I've tried with your modification and I receive "?" over and over again without sending anything. The large number of question marks you see in the photo I provide at the end is sent to the phone as well, but whatever I send from the phone does not get sent to the PC.

Are you sure the Tx and Rx lines to the HC-05 are wired correctly?

Yes, I've double-checked them, currently tx is connected to the 0(rx) pin of Leonardo and rx to the 1(tx) pin of Leonardo.

Should I have used the wiring that is in this image? Without using the resistors when connecting the rx pin?

You need the resistors if the HC-05 Rx pin is not 5V tolerant.

So is there something wrong with the code? I really do not know what else to do in order to solve this issue.

I don't see anything wrong, hopefully someone else can help you.

I've tried using Serial1 (see above responses) but to no avail.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.