Troubles with HC-05 bluetooth and Mega2560

Hi everyone,

I followed this tutorial: In-Depth: Interfacing HC05 Bluetooth Module with Arduino

I'm working with a Mega2560 and trying to communicate with an HC-05 Bluetooth module. I've connected it like shown in the tutorial:

VCC → 5V
GND → GND
TXD → Arduino Pin 3
RXD → Arduino Pin 2, through a voltage divider - 3x 2KΩ resistors, for 3.3V logic (2 thirds)

like so:


(The only difference is that my board is Mega2560 and not Uno and the resistances of the resistors)

I wrote the following code to test the connection (which is taken from the tutorial):

#include <SoftwareSerial.h>

//Create software serial object to communicate with HC-05
SoftwareSerial mySerial(3, 2); //HC-05 Tx & Rx is connected to Arduino #3 & #2

void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  
  //Begin serial communication with Arduino and HC-05
  mySerial.begin(9600);

  Serial.println("Initializing...");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop()
{
  if(Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  if(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
  delay(20);
}

I've paired the HC-05 with my phone and am using a Bluetooth terminal app. I'm able to send data from the Arduino to the phone, but I can't receive anything from the phone back to the Arduino.

To debug, I've connected the TX (pin 3) to analog input A0 to test the signal that is coming from the Bluetooth module and I wrote this test code:

#include <SoftwareSerial.h>

//Create software serial object to communicate with HC-05
SoftwareSerial mySerial(3, 2); 

void setup()
{
  pinMode(A0, INPUT);
  
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  // 115200 for debug ()
  Serial.begin(115200);
  
  //Begin serial communication with Arduino and HC-05
  mySerial.begin(9600);

  Serial.println("Initializing...");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop()
{
  if(Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  if(mySerial.available()) 
  {
    Serial.println("Serial available!");
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }


  Serial.println(analogRead(A0));
  
  //delay(20);
}

As you can see in the screenshot of the analog input from pin A0, the Bluetooth module is transmitting UART-like signals. So, it seems like the HC-05 is working and sending data—but for some reason, the Mega2560 is not picking up input from the HC-05 on pin 3.

Has anyone run into this before?
Any help would be appreciated!

Then why are you using SoftwareSerial ?

The Mega has 4 hardware UARTs. Use Serial1, Serial2 or Serial3 for the HC-05

SoftwareSerial mySerial(3, 2); //HC-05 Tx & Rx is connected to Arduino #3 & #2

Software Serial will not work on pins 2/3 of the Mega

Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

As suggested you should be using one of the the additional hardware serial ports available on the Mega.

Thanks a lot that actually solved it!
It was actually my first approach but I messed up the wirings :sweat_smile:

Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

Thanks for letting me know!

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