Arduino Bluetooth Car Program: Code and Troubleshooting Guide

Creating a Bluetooth-controlled car using Arduino is a fantastic project for hobbyists and those learning about robotics. This guide provides the code you need to get started and addresses a common issue: beeping sounds from your motor driver.

Arduino Bluetooth Car Program Code Explained

Below is the Arduino code designed to control a car using a Bluetooth module and a smartphone app. This program is built for use with an L298N motor driver and allows for forward, backward, left, right movements, and speed adjustments.

/* Code Name: Arduino Bluetooth Control Car Code URI: https://circuitbest.com/category/arduino-projects/ Author: Make DIY Author URI: https://circuitbest.com/author/admin/ Description: This program is used to control a robot using a app that communicates with Arduino through a bluetooth module. App URI: https://bit.ly/2BlMAea Version: 1.0 License: Remixing or Changing this Thing is allowed. Commercial use is not allowed. */
#define in1 4  //L298n Motor Driver pins.
#define in2 5
#define in3 6
#define in4 7
#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() {}

Code Breakdown:

  • Motor Control Pins: in1, in2, in3, in4 are defined to connect to the L298N motor driver.
  • LED Pin: LED (pin 13) can be used for debugging or indicating status.
  • Command Variable: command stores the character received from the Bluetooth app, determining the car’s action.
  • Speed Control: Speed variable (0-255) sets the motor speed using analogWrite().
  • Turning Radius: Turnradius allows for adjusting the turning radius, though in the comments it’s noted to keep this value lower than Speed.
  • Braking System: The code includes functions for brakeOn() and brakeOff(), although brakeOff() is currently empty. brakeOn() attempts to implement an electronic braking system, but it is triggered by the character ‘S’ which is not used in the main command switch case.
  • setup() Function: Initializes pin modes and starts serial communication at 9600 baud for Bluetooth communication.
  • loop() Function: Continuously checks for incoming serial data from the Bluetooth module.
    • Reads the command character.
    • Calls Stop() to ensure motors are initially stopped.
    • Uses a switch statement to execute different motor actions based on the received command (‘F’ for forward, ‘B’ for back, ‘L’ for left, ‘R’ for right, ‘G’ for forward-left, ‘I’ for forward-right, ‘H’ for back-left, ‘J’ for back-right).
    • Speed can be adjusted using number keys ‘0’ to ‘9’ and ‘q’ for maximum speed.
    • Calls brakeOn() or brakeOff() based on brkonoff setting.
  • Movement Functions: forward(), back(), left(), right(), forwardleft(), forwardright(), backleft(), backright() control the motors using analogWrite() to achieve variable speed.
  • Stop() Function: Stops all motors by setting all motor control pins to 0.

Troubleshooting Beeping Sound from L298N Motor Driver

A user reported experiencing a beeping sound from the L298N motor driver when using this code. The most likely reason for this beeping is insufficient power supply.

Why Power Supply is Crucial:

  • Laptop USB Power is Limited: Powering your Arduino and motor driver directly from a laptop’s USB port often doesn’t provide enough current, especially when motors are involved. Motors, especially DC motors used in car projects, require significant current to operate, particularly when starting up or under load.
  • L298N Needs Adequate Voltage and Current: The L298N motor driver needs a sufficient voltage and current supply to properly power the motors. If it doesn’t receive enough power, it can behave erratically, which may manifest as a beeping sound. This sound could be the driver attempting to operate but failing due to power starvation, or it could be some form of undervoltage protection kicking in, if the driver has such a feature.

Solution: Use an External Power Supply

To resolve the beeping issue and ensure your Arduino Bluetooth car operates correctly, you should use an external power supply dedicated to the motor driver. Here’s how to address the power issue:

  1. Disconnect USB Power: When using an external power supply for the motors, disconnect the USB cable from your laptop or computer to the Arduino (for motor power). The Arduino can still be powered via USB for programming and Bluetooth communication, or it can be powered separately as well.
  2. Choose the Right Power Supply:
    • Voltage: Check the voltage requirements of your DC motors and the L298N motor driver. Typically, a voltage in the range of 6V to 12V is suitable for small DC motors.
    • Current: The power supply must be able to provide enough current for your motors. The current draw depends on the motors you are using and the load they are under. It’s always better to have a power supply with a higher current rating than the estimated maximum draw of your motors to ensure stable operation. A 1A to 2A power supply is often sufficient for small robot car projects, but check your motor specifications.
  3. Connect External Power: Connect your external power supply to the appropriate terminals on the L298N motor driver. Refer to the L298N datasheet and your motor driver module documentation for correct wiring. Ensure correct polarity!

Other Potential (Less Likely) Causes for Beeping:

While power is the most probable cause, other possibilities include:

  • Wiring Issues: Double-check all wiring connections between the Arduino, L298N motor driver, and motors. Loose or incorrect wiring can sometimes cause unexpected behavior, although beeping specifically points more strongly to power issues.
  • Code Issues (Less Likely): While less likely to directly cause beeping, incorrect code logic could theoretically cause the motor driver to behave in ways that might produce sound. However, given the provided code and the symptom, power is the primary suspect.

In summary, if you hear beeping from your L298N motor driver, especially when powering it through Arduino from a laptop USB, immediately suspect a power supply issue. Switching to an adequate external power supply is almost certainly the solution.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *