HC-05 Bluetooth and Arduino Mega

I'm trying to build a basic setup for bluetooth communication with my Arduino MEGA2560 (AZ Delivery CH340 clone). I'm using hardware serial1 and two LEDs to show me whats going on: One Common Anode RGB Led that mirrors the state of serial connection and one basic red LED that should turn on when AT Mode is entered.

This is the circuit:

This is the guide I'm currently trying to follow:
https://www.martyncurrey.com/using-an-arduino-mega-with-a-hc-05-zs-040-at-mode/

And this is my code so far:

// Basic Bluetooth test sketch 5a for the Arduino Mega. 
// AT mode using button switch
// HC-05 with EN pin and button switch
//
// Uses serial with the host computer and serial1 for communication with the Bluetooth module
//
//  Pins
//  BT VCC to Arduino 5V out. Disconnect before running the sketch
//  BT GND to Arduino GND
//  BT RX (through a voltage divider) to Arduino TX1 (pin 18)
//  BT TX  to Arduino RX1 (no need voltage divider)   (pin 19)
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the Bluetooth module and display the result.
//

int redPin= 11;
int greenPin = 10;
int bluePin = 9;

char serialByte = '0';
const byte  LEDPIN = 13; 
 
void setup() 
{
    pinMode(LEDPIN, OUTPUT);
    setColor(255, 0, 0); // Red Color -> System Powered
    delay(1000);

    // communication with the host computer
    Serial.begin(9600);  

    setColor(0, 0, 255); // Blue Color -> Serial Connection Established
    delay(1000);
 
    Serial.println("Do not power the BT module");
    Serial.println(" ");
    Serial.println("On the BT module, press the button switch (keep pressed, and at the same time power the BT module");
    Serial.println("The LED on the BT module should now flash on/off every 2 seconds");
    Serial.println("Can now release the button switch on the BT module");
    Serial.println(" ");
    Serial.println("After entering AT mode, type 1 and hit send");
    Serial.println(" ");
 
 
    // wait for the user to type "1" in the serial monitor
    while (serialByte !='1')
    {
        if ( Serial1.available() )   {  serialByte = Serial1.read();  }
    }  
 
 
    // communication with the BT module on serial1
    Serial1.begin(38400);
 
    // LED to show we have started the serial channels
    digitalWrite(LEDPIN, HIGH);  
    setColor(0, 255, 0); // Green Color -> System Connected
    delay(1000);

    Serial.println(" ");
    Serial.println("AT mode.");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("The HC-05 accepts commands in both upper case and lower case");
    Serial.println(" "); 
}
 
 
void loop() 
{
    // listen for communication from the BT module and then write it to the serial monitor
    if ( Serial1.available() )   {  Serial.write( Serial1.read() );  }
 
    // listen for user input and send it to the HC-05
   if ( Serial.available() )   {  Serial1.write( Serial.read() );  }
}


void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, 255 - redValue);
  analogWrite(greenPin, 255 - greenValue);
  analogWrite(bluePin, 255 - blueValue);
}

What I'm doing/whats happening:

  • Disconnect VCC from HC-05
  • Upload Software to Arduino
  • Red LED flashes briefly, then goes dark.
  • RGB LED goes RED (on) then BLUE (serial with Arduino established)
  • Then I'm holding the button on the HC-05 and connect VCC
  • The HC-05 is blinking in it's 1sec on/of mode. So it SHOULD be in AT Mode.

But regardless of what I enter in serial monitor, both on 9600 and 38400 BAUD; I don't get any response on serial or from the LEDs. I tried "1" (without Quotes), "AT", "AT+VERSION".

Any Idea, what I'm doing wrong?

Maybe this should be before you try to read Serial1?

Else I would not know.

Sadly that does not make any difference. I set the Serial1.begin(38400) right after the Serial.beginn(9600).

I should add that if I'm not doing the Disconnect/Hold Button routine the HC-05 starts up as intended into pairing mode (fast blinking LED) and it's possible to connect to it from a smartphone. So the BT module should be wired correctly and functional.

The module is also from AZ Delivery, this one specifically: HC-05 HC-06 Bluetooth Wireless RF-Transceiver-Modul RS232 serielle TTL

@corvin0
See something wrong here?

    // wait for the user to type "1" in the serial monitor
    while (serialByte !='1')
    {
        if ( Serial1.available() )   {  serialByte = Serial1.read();  }
    }  
 
 

I don't get it @jim-p - the script is running until reaching the while where it's blocked until a 1 is sent.

So..."serialByte is 0. While serialByte is not 1, listen to Serial1 and write what you hear into serialByte".

The code should continue after the while loop is resolved and accept AT Commands, right? It's true that Id also set the Serial1.begin BEFORE that while, but that didn't change anything, already tried that.

The serial monitor is connected to Serial not Serial1, unless I mis-understand what you are doing.

"Normal" Serial on 9600 for Arduino to PC over USB Cable. And Serial1 on 38400 (default Baud according to the datasheet) for the HC-05 BT to Arduino over RX/TX (Ports 18 and 19).

Serial1 and Serial telling each other what they want is solved in the void loop:

void loop() 
{
    // listen for communication from the BT module and then write it to the serial monitor
    if ( Serial1.available() )   {  Serial.write( Serial1.read() );  }
 
    // listen for user input and send it to the HC-05
   if ( Serial.available() )   {  Serial1.write( Serial.read() );  }
}

Is there something wrong?

Yes. See post #4
The code I show is from your setup() section.
You are waiting for user input on Serial1

1 Like

Aaaand we're green! A big thank you and sorry for drawing a blank :smiley:


Here's the updated code if someone needs it:

// Basic Bluetooth test sketch 5a for the Arduino Mega. 
// AT mode using button switch
// HC-05 with EN pin and button switch
//
// Uses serial with the host computer and serial1 for communication with the Bluetooth module
//
//  Pins
//  BT VCC to Arduino 5V out. Disconnect before running the sketch
//  BT GND to Arduino GND
//  BT RX (through a voltage divider) to Arduino TX1 (pin 18)
//  BT TX  to Arduino RX1 (no need voltage divider)   (pin 19)
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the Bluetooth module and display the result.
//

int redPin= 11;
int greenPin = 10;
int bluePin = 9;

char serialByte = '0';
const byte  LEDPIN = 13; 
 
void setup() 
{
    pinMode(LEDPIN, OUTPUT);
    setColor(255, 0, 0); // Red Color -> System Powered
    delay(1000);

    // communication with the host computer
    Serial.begin(9600);  

    setColor(0, 0, 255); // Blue Color -> Serial Connection Established
    delay(1000);
 
    Serial.println("Do not power the BT module");
    Serial.println(" ");
    Serial.println("On the BT module, press the button switch (keep pressed, and at the same time power the BT module");
    Serial.println("The LED on the BT module should now flash on/off every 2 seconds");
    Serial.println("Can now release the button switch on the BT module");
    Serial.println(" ");
    Serial.println("After entering AT mode, type 1 and hit send");
    Serial.println(" ");
 
    // wait for the user to type "1" in the serial monitor
    while (serialByte !='1')
    {
        if ( Serial.available() )   {  serialByte = Serial.read();  }
    }  

     // communication with the BT module on serial1
    Serial1.begin(38400);

    // LED to show we have started the serial channels
    digitalWrite(LEDPIN, HIGH);  
    setColor(0, 255, 0); // Green Color -> System Connected
    delay(1000);

    Serial.println(" ");
    Serial.println("AT mode.");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("The HC-05 accepts commands in both upper case and lower case");
    Serial.println(" "); 
}
 
 
void loop() 
{
    // listen for communication from the BT module and then write it to the serial monitor
    if ( Serial1.available() )   {  Serial.write( Serial1.read() );  }
 
    // listen for user input and send it to the HC-05
   if ( Serial.available() )   {  Serial1.write( Serial.read() );  }
}


void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, 255 - redValue);
  analogWrite(greenPin, 255 - greenValue);
  analogWrite(bluePin, 255 - blueValue);
}

You are working to hard!!!!
Take a break

1 Like

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