0
0
AutocadHow-ToBeginner · 4 min read

Arduino Project for Bluetooth Controlled Car: Simple Guide

To build a Bluetooth controlled car with Arduino, connect a Bluetooth module (like HC-05) to the Arduino and control motor drivers via Bluetooth commands from a smartphone app. Use Serial communication to receive commands and control the car's movement by driving motors accordingly.
📐

Syntax

This project uses the Serial interface to communicate with the Bluetooth module and control motors via digital pins connected to a motor driver.

  • Serial.begin(9600); initializes serial communication at 9600 baud rate.
  • Serial.available() checks if data is received from Bluetooth.
  • Serial.read() reads the incoming command.
  • Motor control pins are set as OUTPUT and controlled with digitalWrite().
arduino
void setup() {
  Serial.begin(9600); // Start serial communication
  pinMode(2, OUTPUT); // Motor A forward
  pinMode(3, OUTPUT); // Motor A backward
  pinMode(4, OUTPUT); // Motor B forward
  pinMode(5, OUTPUT); // Motor B backward
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    // Commands: 'F' forward, 'B' backward, 'L' left, 'R' right, 'S' stop
    switch (command) {
      case 'F':
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case 'B':
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
      case 'L':
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case 'R':
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
      case 'S':
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
    }
  }
}
💻

Example

This example shows a complete Arduino sketch to control a car's motors using Bluetooth commands sent from a phone app. Commands 'F', 'B', 'L', 'R', and 'S' move the car forward, backward, left, right, and stop respectively.

arduino
void setup() {
  Serial.begin(9600); // Initialize Bluetooth serial communication
  pinMode(2, OUTPUT); // Motor A forward
  pinMode(3, OUTPUT); // Motor A backward
  pinMode(4, OUTPUT); // Motor B forward
  pinMode(5, OUTPUT); // Motor B backward
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    switch (command) {
      case 'F': // Forward
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case 'B': // Backward
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
      case 'L': // Left
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case 'R': // Right
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
      case 'S': // Stop
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
    }
  }
}
⚠️

Common Pitfalls

1. Incorrect wiring: Connecting the Bluetooth module or motors incorrectly can cause the car not to respond. Always double-check connections.

2. Baud rate mismatch: The Bluetooth module and Arduino must use the same baud rate (usually 9600).

3. Missing motor driver: Motors cannot be powered directly from Arduino pins; use a motor driver like L298N.

4. Not reading serial data properly: Forgetting to check Serial.available() before reading causes errors.

arduino
/* Wrong way: Reading serial without checking availability */
void loop() {
  char command = Serial.read(); // May read nothing or garbage
  // ...
}

/* Right way: */
void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    // ...
  }
}
📊

Quick Reference

  • HC-05 Bluetooth Module: Connect VCC to 5V, GND to GND, TX to Arduino RX (pin 0), RX to Arduino TX (pin 1) via voltage divider.
  • Motor Driver Pins: Connect motor driver inputs to Arduino pins 2, 3, 4, 5.
  • Commands: 'F' = forward, 'B' = backward, 'L' = left, 'R' = right, 'S' = stop.
  • Serial Baud Rate: 9600 for communication.

Key Takeaways

Use Serial communication to receive Bluetooth commands and control motors.
Always check Serial.available() before reading data to avoid errors.
Use a motor driver to safely power motors from Arduino.
Match baud rates between Arduino and Bluetooth module for proper communication.
Test wiring carefully to ensure correct connections for Bluetooth and motors.