Can't connect to HC-06

I've been trying to connect with my HC-06 on my laptop, but everytime i connect with it, it immediatly disconnects. The HC-06 says it's paired but the laptop says it isn't connected. I thought this might be a little glitch or bug but my test codes don't seem to wanna work either. Does anybody know what might be the cause that my laptop keeps automatically disconnecting from the HC-06?

It might be a power issue, have you tried wiring it directly to the arduino without the resistors?

Try this sketch, the wiring is included at the top in the comments:

#include "<softwareserial.h>" // remove the inverted commas after you copy the code to the IDE
SoftwareSerial BT(10, 11); 
// creates a "virtual" serial port/UART
// connect BT module TX to D10
// connect BT module RX to D11
// connect BT Vcc to 5V, GND to GND
void setup()  
{
  // set digital pin to control as an output
  pinMode(13, OUTPUT);
  // set the data rate for the SoftwareSerial port
  BT.begin(9600);
  // Send test message to other device
  BT.println("Hello from Arduino");
}
char a; // stores incoming character from other device
void loop() 
{
  if (BT.available())
  // if text arrived in from BT serial...
  {
    a=(BT.read());
    if (a=='1')
    {
      digitalWrite(13, HIGH);
      BT.println("LED on");
    }
    if (a=='2')
    {
      digitalWrite(13, LOW);
      BT.println("LED off");
    }
    if (a=='?')
    {
      BT.println("Send '1' to turn LED on");
      BT.println("Send '2' to turn LED on");
    }   
    // you can add more "if" statements with other characters to add more commands
  }
}
1 Like

I was alreadt using that code to test, u might be right that it's power related. After removing the 2 resistors it connected properly for a while. But after re uploading the code it now does the same disconnect routine

1 Like

any reason why you are using softwareserial on a Mega which has three spare hardware serial ports?
see Use Multiple Serial Ports on the Arduino Mega

Have you tried using different pins other than pins D0 and D1? I did a bit of research and people have said that using pins 0 and 1 will cause issues while uploading on most boards.

I have never used an arduino mega and could not see the pin configuration on the image shown. I just had a look at the pinout a minute ago and noticed that pins D14-D19 are all hardware serial ports.

HC-06 connected to Mega Serial 1 pin 18 Tx pin 19 Rx

code (for Due but works on a Mega)

// HC-06 Bluetooth module AT commands or transmit text

// note Serial1 baudrate is 9600
// using Arduino Due/Mega connect HC-06 Tx to due pin 19
// using Arduino Due/Mega connect HC-06 Rx to due pin 18

// when paired (code 1234) with PC creates two COM ports, e.g. COM13 and COM14
//  using teraterm at 9600 baud connect to COM13
//  send/receives OK

// AT mode - if not paired (LED fashing) the device is by default in AT mode
//  the Arduino Serial Monitor should have "No line ending"

void setup()
{
  Serial.begin(115200);
  Serial.println("HC-06 Enter AT commands or transmit text:");
  Serial1.begin(9600);//38400);       
}

void loop()
{
  if ( Serial1.available())    // read from HC-05 and send to Arduino Serial Monitor
  Serial.write( Serial1.read());

  if (Serial.available())     // Keep reading from Arduino Serial Monitor and send to HC-05
  Serial1.write(Serial.read());
}

serial monitor output shows text received from Android phone

HC-06 Enter AT commands or transmit text:
hello from phone phone test 2 123456789

phone displays running Bluetooth Terminal (works for both Bluetooth Classic and BLE)

So it works, I think. But I can't seem to control a led via the serial monitor.

this code is for ESP32 Bluetooth Classic but may give you some ideas
if a '1' is received LED is turned ON if a '0' it is turned OFF

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

int led=15;

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  pinMode(led, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    char ch=SerialBT.read();
    Serial.write(ch);
    if(ch == '1') digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    if(ch == '0') digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  }
  delay(20);
}

The two resistors have nothing to do with power and everything to do with applying a signal of the right voltage to Bluetooth Rx.

You might indeed have a power problem, but I don't recall you describing the source.

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