HC-05 Keeps Disconnecting

Im building an arduino bluetooth robot and HC-05 keeps disconnecting after i give it a command to run the motors. Im using 2x 18650 batteries which have a combined power of 7.4v.
Components: Arduino Uno, HC-05, L298N, 4 DC Motors
![bluetooth-rc-car-arduino-program|690x471](upload://9GdXrAaJqdK121AdXsxso1fmslz.jpeg

#define in1 5 //L298n Motor Driver pins.
#define in2 6
#define in3 10
#define in4 11
#define LED 13
int command; //Int to store app command state.
int Speed = 204; // 0 - 255.
int Speedsec;
int buttonState = 0;
int lastButtonState = 0;
int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
int brakeTime = 45;
int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(LED, OUTPUT); //Set the LED pin.
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
}

void loop() {
  if (Serial.available() > 0) {
    command = Serial.read();
    Stop(); //Initialize with motors stoped.
    switch (command) {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'G':
        forwardleft();
        break;
      case 'I':
        forwardright();
        break;
      case 'H':
        backleft();
        break;
      case 'J':
        backright();
        break;
      case '0':
        Speed = 100;
        break;
      case '1':
        Speed = 140;
        break;
      case '2':
        Speed = 153;
        break;
      case '3':
        Speed = 165;
        break;
      case '4':
        Speed = 178;
        break;
      case '5':
        Speed = 191;
        break;
      case '6':
        Speed = 204;
        break;
      case '7':
        Speed = 216;
        break;
      case '8':
        Speed = 229;
        break;
      case '9':
        Speed = 242;
        break;
      case 'q':
        Speed = 255;
        break;
    }
    Speedsec = Turnradius;
    if (brkonoff == 1) {
      brakeOn();
    } else {
      brakeOff();
    }
  }
}

void forward() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speed);
}

void back() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speed);
}

void left() {
  analogWrite(in3, Speed);
  analogWrite(in2, Speed);
}

void right() {
  analogWrite(in4, Speed);
  analogWrite(in1, Speed);
}
void forwardleft() {
  analogWrite(in1, Speedsec);
  analogWrite(in3, Speed);
}
void forwardright() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speedsec);
}
void backright() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speedsec);
}
void backleft() {
  analogWrite(in2, Speedsec);
  analogWrite(in4, Speed);
}

void Stop() {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}

void brakeOn() {
  //Here's the future use: an electronic braking system!
  // read the pushbutton input pin:
  buttonState = command;
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == 'S') {
      if (lastButtonState != buttonState) {
        digitalWrite(in1, HIGH);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, HIGH);
        delay(brakeTime);
        Stop();
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }
}
void brakeOff() {

}

have nominal voltage of 7.4V.
Depending on the state of charge it can be anything between 6V and 8.5V.
What are you powering with this battery?

An arduino Uno, HC-05, L298N, 4 DC motors

Hopefully HC is not connected to battery. What motors, max current draw?

The HC-05 is recieving the 5V from the arduino

How is the HC-05 (its other pins) connected to the Arduino?

By your sketch you´re probably making wrong use of the L298N on the speed side. Take a look in This Tutorial to see if everything looks like your circuit.

this is what i have been using

its the exact same thing i used to make this

Brand name fully charged 18650 should be capable for that.
So the whole setup keeps on working, just HC is disconnecting?

yes, but it only disconnects after i give it a command to move the motors

2 things I didn´t like on this article:

  1. it ignores the fact that HC-05 logic level is 3.3V, which requires a voltage divider on the HC-05 Rx line.

  2. it tries to control the motor speed using the direction pins on the L298N driver.

Considering that you have to show this car tomorrow (It's not my job to scold you, but I think you started this work too late...), I would try to connect the HC-05 Rx/Tx using SoftwareSerial in other Arduino pins, keeping 0 and 1 to talk to the Serial Monitor. This will help you to see what's happening and debug the sketch.

So you are sure arduino doesn't reset??

Really appreciate the help, will check if it works

The "tut" is taking the '5V' from the motor board to the 'uno' VIN.
Not good.

1 Like

What do you suggest I should do instead?

Power arduino from battery voltage to Vin.

Thanks for the help guys, the project seems to be working now without issues

1 Like