HC05 - Arduino IDE freezes after attempting to send serial message

Hello! I'm working on an RC car project that will be controlled by serial messages sent to an Arduino from my computer. I am using an Arduino Uno R3 and Windows 10. The HC05 shows up on my Bluetooth and is connected. I have the HC-05 physically set up exactly like this image:

When I attempt to send a message to my Arduino/HC-05 from my Arduino IDE, the IDE freezes (I have to Ctrl-Alt-Del to exit) and nothing happens on my project. I'm not sure if I have the code written correctly, and was hoping you all would be willing to double check it. Here is the code:

#define enA 9
#define enB 10
#define in1 4
#define in2 5
#define in3 6
#define in4 7

int state = 0;
int motorSpeed = 255/2;

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400);
}
 
void loop() {
if(Serial.available()){
  state = Serial.read();
  Serial.println(state);
}

  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed); 
 
if(state == '0'){                 //STOP (all motors stop)
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}

else if(state == '1'){            // Move Forward (all motors rotate in forward direction)
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}
 
else if(state == '2'){            // Move Reverse (all motors rotate in reverse direction)
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
}
 
else if(state == '3'){            // Turn Right (left side motors rotate in forward direction, right side motors doesn't rotate)
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}
 
else if(state == '4'){            // Turn Left (right side motors rotate in forward direction, left side motors doesn't rotate)
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}
 
//delay(100); // This was in the original code and may or may not be needed
}

Leave Serial (pins 0 and1) for uploading code, code output viewing and debugging. Put the HC05 on one of the other serial ports (Serial1, Serial2 or Serial3).

Serial.begin(38400)

The default baud rate for my HC05s is 9600 for regular communication, 38400 to talk to the HC05 in AT mode.

The serial input basics tutorial may be of interest.

Hello, thank you for the reply. The Arduino I am using is the Uno R3, I don't think I have Serial1/2/3. It's a little late but I'll change the code tomorrow to 9600 and see what happens. Thanks!

I see you actually say Uno, but you confuse things by saying "exactly", and showing a Mega. It is quite OK to use hardware serial, and indeed preferable. All you have to do is make sure that you have Bluetooth disconnected while uploading, but I guess you already know that. That delay(100) is probably a good idea, and should be retained to start with. I believe all HC-05s are 9600 for comms by default so, unless you have reconfigured it, you will definitely need to use 9600.

Sorry for the confusion, you're right about what I had said. I suppose I was referring to the pins being used, but I can see how that would get misunderstood.

I'm pretty new to Arduino; do you mind explaining why the delay(100) is a good idea?

I'm not sure I'm the best person to explain it, but you see that sort of thing put in so that Arduino can settle things down. And without it you have no timing control and the time through the loop depends what happens in the conditionals. It may be falling over itself.

It is quite OK to use hardware serial, and indeed preferable.

I can't disagree, but, on an Uno, the hardware serial port is one's main debugging tool. The HC05 works fine on a software serial port at 9600 baud so you are not losing much by using software serial (a couple of pins) but you are gaining the ability to monitor program flow and such.

I edited the code to be 9600 and for the delay(100) to be uncommented, but the same result happens: When I connect to COM6 and select Serial Monitor, I get a message saying that the port is busy. When I connect to COM4 and select Serial Monitor, the SM window pops up, which is good. The problem is that when I type "1" into the SM and hit send, the Arduino IDE program freezes.

I asked a friend and he said that the same thing happened to him when he connected to the wrong COM port. If this is the problem, how do I find which COMM port is for the HC05? If that's not the problem, what should I try?

If you have the HC05 connected to hardware serial you can't use the same port for the serial monitor. That is why I suggested that you use a software serial port for the HC05.

I appreciate your suggestion, and it's not that I don't want to, it's that all of the pins on the Arduino will be used for this project, so I don't want to use pins and code now that won't be in the final result.

Regardless of that detail, I am not connected via USB when the Arduino IDE freezes; the Uno is receiving power from a battery and is separated from the computer, meaning I am not connected to the hardware serial.

As I am not connected to the hardware serial, what would cause the Arduino IDE to freeze when I send a message from the IDE's Serial Monitor?

I suppose maybe some details I didn't think were necessary may be helpful:

The pins 0 and 1 are going to the HC05 as shown in the photo (but with an Uno R3, not a Mega).

Pins 4,5,6,7,9,10 are going to an L298N.

A 7.2V battery is going into the positive power input of the L298N, with another lead going from that input to the VCC input on the Arduino. According to this tutorial (https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/), that is how to supply the Arduino with power, as well as my motors with power, and all from a single battery.

The goal is to control an RC car from a computer via Bluetooth with the HC05. Everything is connected properly as far as the L298N is concerned, and everything is connected properly as far as the HC05 is concerned. I am absolutely sure the hardware aspect of this is A-Okay.

My suspicion is that my code isn't adequate for establishing a connection and communicating with the HC05 properly. Maybe some sort of loop is happening, as when the Arduino IDE freezes I have to shut it off with Task Manager, attempting to close the window doesn't work.

You are going at it blind so it is hard to troubleshoot non-working code.

Use the Software Serial temporarily until you get your code working and move it back to 0/1 or keep guessing.

Your time if you want to waste it.

.

Have you paired and connected the Uno HC05 to the computer Bluetooth from the computer end?

Your Windows device manager should tell you which ports you are using. I understand that some PCs use TWO ports, one in and one out, but I have never seen that. My laptops always have the port around COM40. Whatever it is, it won't be the same as the serial monitor and you cannot use the monitor to communicate via Bluetooth. You need to use a proper terminal, like RealTerm.

I get the impression you don't need to debug anything but, if you do, you can disconnect Bluetooth and use the serial monitor. That way, you are essentially using the serial monitor as a dummy Bluetooth. This means that the code you are debugging is exactly the code that Bluetooth will be using, and any problems subsequently incurred will be down to wiring or configuration.

I don't know anything about the wisdom of using that battery, but I think it would be smart to use a proper wall wart on Uno, leave all that other stuff out of the game if you can, and just get Bluetooth working.

My suspicion is that my code isn't adequate for establishing a connection and communicating with the HC05 properly. Maybe some sort of loop is happening, as when the Arduino IDE freezes I have to shut it off with Task Manager, attempting to close the window doesn't work.

This doesn't make sense. You have already proven that code communicates just fine. It may be just a matter of nomenclature, but I'm getting confused about what the project really is. The connection and communication is not done by your code, it is either done by PC software or, at most, by the configuration of the HC-05 as a master. Your IDE should not be being used with Bluetooth, which is a very good way of ensuring it won't freeze.