HC-05, Unable to Open Serial Port

:angry: I currently have an HC-05 connected to my Arduino Mega 2560. The TX of the module is connected to the RX of the Arduino (pin 0), and the RX to the TX (pin 1). I uploaded a sketch which is supposed to allow me to send a command to turn on and off an led (pin 13). Here is the sketch:


char val; // variable to receive data from the serial port
int ledpin = 13; // LED connected to pin 2 (on-board LED)

void setup()
{
pinMode(ledpin = 13, OUTPUT); // pin 13 (on-board LED) as OUTPUT

Serial.begin(115200); // start serial communication at 115200bps

}

void loop() {
if( Serial.available() ) // if data is available to read
{;}
val = Serial.read(); // read it and store it in 'val'

if( val == '0' ) // if '0' was received led 13 is switched off

{
digitalWrite(ledpin, LOW); // turn Off pin 13 off
delay(1000); // waits for a second
Serial.println("13 off");
}

if( val == '1' ) // if '1' was received led 13 on
{
digitalWrite(ledpin = 13, HIGH); // turn ON pin 13 on
delay(1000); // waits for a second
Serial.println("13 on");
}
}


Currently I am able to "pair" the HC-05 with my PC and it assigns a COM port, and the red led will stop blinking really fast and only blink once very 1-2 seconds. The problem is, when I open Putty or Tera Term, it says it cannot open a connection to the COM port and cannot open a serial connection.

I am using Windows 7 Professional. The Arduino is powered by a 6 V battery pack. I have completely had it with this thing, and any help would be greatly appreciated!

cmgibson22188:
:angry: I currently have an HC-05 connected to my Arduino Mega 2560. The TX of the module is connected to the RX of the Arduino (pin 0), and the RX to the TX (pin 1). I uploaded a sketch which is supposed to allow me to send a command to turn on and off an led (pin 13). Here is the sketch:


Serial.begin(115200); // start serial communication at 115200bps


I have never known an HC-05 to come out of the box set at 115200. This implies that you have configured it for that, and I bet you haven't. Check what the speed is, and set the code accordingly.

Currently I am able to "pair" the HC-05 with my PC and it assigns a COM port, and the red led will stop blinking really fast and only blink once very 1-2 seconds.

This suggests that the module is in AT mode. Don't ask how that happened. If you are in communication mode, the LED is usually steady so check the data sheet to ensure it really is doing what you think it is doing. On the ZS-040 module, the LED extinguishes and then flashes only when there is an actual transmission

The Arduino is powered by a 6 V battery pack.

6v is the kiss of death. Too much for the 5v USB port and below the 7v minimum for the barrel jack. Time to get a proper 9v wall wart, or at least a battery pack that is appropriate for the job, which does not mean a PP3.

I know its an old post but bare with me!

I am having the same problem.
I have used the same code with 9600 as baud rate. Whenever, I open Putty and set it for serial communication on the designated port by my PC, I get an error as : Unable to open serial port COM45

I need help!

COM45! Looks like you need to do some cleaning, look for info on Releasing COM ports in Windows.

I am not sure your problem is the same(!).
Reply #3 is probably good advice, but I have never pursued that. I have the same sort of problem with my laptop, where the on-board bluetooth is on COM 40 and, since it is on board, I assume that is where Dell in their wisdom put it. I was not, at first, aware of this being a problem because RealTerm was able to open it OK. I only found that I had a problem when Excel could not open it. I assumed the problem was with Excel or the PLX macro. I actually fixed this by using the phone instead, which I thought preferable anyway. You may find RealTerm is OK.