Building a Bluetooth-Controlled Car Using Arduino

 


In this project, we'll create a simple Bluetooth-controlled car using an Arduino, a motor driver (L298n), and a Bluetooth module. This car can move forward, backward, left, and right, and we’ll use an app on our smartphone to send commands via Bluetooth to control its movement.

Components Needed:

  • Arduino Uno
  • L298n Motor Driver
  • Bluetooth Module (HC-05 or HC-06)
  • DC Motors
  • Car Chassis
  • Jumper wires
  • Battery Pack
  • LEDs (optional for indication)
  • Smartphone with a Bluetooth terminal app

Wiring Connections:

  1. Motor Driver (L298n) Pins:

    • Connect IN1 to Arduino pin 5.
    • Connect IN2 to Arduino pin 6.
    • Connect IN3 to Arduino pin 10.
    • Connect IN4 to Arduino pin 11.
  2. Bluetooth Module:

    • Connect the TX pin of the Bluetooth module to the RX pin of the Arduino.
    • Connect the RX pin of the Bluetooth module to the TX pin of the Arduino.
    • Connect VCC and GND to power the Bluetooth module.
  3. Motor Connections:

    • Connect the motor terminals to the output pins of the L298n motor driver.
  4. Powering the Motors:

    • Use a separate power source to power the motors, ensuring the motor driver gets sufficient current.

Arduino Code for Bluetooth Control

This Arduino code listens for Bluetooth commands and controls the movement of the car accordingly.


#define in1 5 // L298n Motor Driver pins #define in2 6 #define in3 10 #define in4 11 #define LED 13 int command; // Store app command state int Speed = 204; // Speed of the car (0-255) int Speedsec; int buttonState = 0; int lastButtonState = 0; int Turnradius = 0; // Radius of the turn (0-255) int brakeTime = 45; int brkonoff = 1; // Electronic braking: 1 = on, 0 = off void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(LED, OUTPUT); // LED pin setup Serial.begin(9600); // Bluetooth module baud rate } void loop() { if (Serial.available() > 0) { command = Serial.read(); Stop(); // Initialize with motors stopped // Handle movement based on received command switch (command) { case 'F': forward(); break; // Move forward case 'B': back(); break; // Move backward case 'L': left(); break; // Turn left case 'R': right(); break; // Turn right case 'G': forwardleft(); break; // Forward left case 'I': forwardright(); break; // Forward right case 'H': backleft(); break; // Backward left case 'J': backright(); break; // Backward right // Speed control from 0 to 9 and 'q' for max speed 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; // Electronic braking system control if (brkonoff == 1) { brakeOn(); } else { brakeOff(); } } } // Movement functions 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); } // Braking system functions void brakeOn() { buttonState = command; if (buttonState != lastButtonState) { if (buttonState == 'S') { // Brake on command if (lastButtonState != buttonState) { digitalWrite(in1, HIGH); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, HIGH); delay(brakeTime); Stop(); } } lastButtonState = buttonState; } } void brakeOff() { // Implement if needed }

How It Works:

  • The car moves based on commands received via Bluetooth. Each command corresponds to a direction or speed:
    • F for forward, B for backward, L for left, R for right.
    • Speed is adjustable using numbers 0-9 and 'q' for maximum speed.
  • There is an optional electronic braking system, triggered by the 'S' command.

Bluetooth App:

You can control the car using a Bluetooth terminal app. Simply pair your Bluetooth module with the phone and send commands like 'F', 'B', 'L', 'R' to control the movement.

Conclusion:

This project allows you to create a basic Bluetooth-controlled car with speed and direction control. By integrating an electronic braking system, you can make the car even more responsive and safer to control. Experiment with different speeds, turning radii, and braking to fine-tune the car's performance.

Comments

Popular Posts