Arduino Mega and HC-05 Bluetooth Communication with ESP32

Hi,

I have connected my Arduino mega with an HC-05 and am trying to get it to communicate with an ESP32 through Bluetooth. I managed to connect the two and the ESP32 serial is able to receive data from the Arduino mega, but the mega is unable to receive data from the ESP. For the ESP32 code, I am using the example Bluetooth SerialToSerialBTM code. On the Arduino side, I am using software serial. I have tried different combinations of pins (2 & 3, 4 & 5) and it doesn't seem to make a difference. I also tried using hardware serial (pins TX1 and RX1) - the Arduino mega and ESP32 connected but didn't receive data from either.

I would appreciate and help or guidance to figure out the communication between the two.

That should work. Please post the Mega code and the wiring.

This is the Mega code:

#include <SoftwareSerial.h>

void setup()
{
  Serial.begin(9600);
  Serial2.begin(9600);
}
void loop() {
  
  if(Serial2.available()){
    char c = Serial2.read();
    Serial.write(c);
    Serial.println('from module 2');
  }

  if(Serial.available()){
    Serial2.write(Serial.read());
    Serial2.println('to module 2');
  }
}

This is my wiring

The HC-05 RXD is connected to the Mega TX3
The HC-05 TXD is connected to the Mega RX3

Why then are you using Serial2 in your code?
Serial2.begin(9600);

Should I be using Serial3?

The intention was asking for the Serial1 attempt but okey.
I doubt it's possible to use Software Serial on the UART port. Try other digital pins. Check with Arduino/reference regarding Serial.

Yes, if that's the connection between the HC05 and the Mega.

My software serial code is different

#include <SoftwareSerial.h>
SoftwareSerial SUART(10, 11); // SRX = 10, STX = 11
void setup()
{
  Serial.begin(38400);
  SUART.begin(38400);
}
void loop() {
  if (SUART.available())
  {
    char x = SUART.read();
    // Serial.println(x);
    Serial.write(x);
    Serial.flush();
  }
  if (Serial.available()) {
    char c = Serial.read();
    // SUART.println(c);
    SUART.write(c);
    SUART.flush();
   
  }
  delay(50);
}

With this code:
The HC-05 RXD is connected to the Mega 11 port
The HC-05 TXD is connected to the Mega 10 port

I made the changes to Serial3 and the device does not connect

The connection between the HC05 and the ESP32 (indicated by the blink pattern of the HC05) is independent from the Mega serial port communicating with the HC05.

oh i see, any suggestions on how I can reliably connect the two?

suggestions on how I can reliably connect the two?

First, I would make certain that the HC05 is working properly and can reliably connect to a phone app like Kai Morich's Serial Bluetooth Terminal.

I think that the ESP32 needs to be reset to go through the connection process each time after a disconnection.

What is the overall goal of your project. Why do you need both a Mega and an ESP32? Using two devices makes a project much more complicated.

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