How to Make Arduino Bluetooth +obstacle Avoiding Car

by ideasu-6 in Circuits > Arduino

22 Views, 0 Favorites, 0 Comments

How to Make Arduino Bluetooth +obstacle Avoiding Car

Screenshot (50).png

Welcome to my Arduino Bluetooth + Obstacle-Avoiding Car project! This DIY project combines creativity, electronics, and programming to build a robotic car that can avoid obstacles autonomously while also being controlled via Bluetooth.

The car is equipped with an ultrasonic sensor to detect and avoid obstacles in its path, ensuring smooth and intelligent navigation. Additionally, with the help of a Bluetooth module, you can manually control the car's movements using a smartphone or Bluetooth-enabled device.

This project is perfect for beginners and enthusiasts who want to explore the world of robotics, Arduino programming, and sensor integration. Whether you're creating this for fun, education, or as a submission for a competition, it’s a fantastic way to learn about robotics and showcase your skills.

Supplies

Supplies for the Arduino Bluetooth + Obstacle-Avoiding Car

Here’s the complete list of components and materials you’ll need to build this project:

Electronics

  1. Arduino Uno/Nano – 1 unit (main microcontroller board).
  2. L298N Motor Driver – 1 unit (to control the motors).
  3. HC-05 Bluetooth Module – 1 unit (for Bluetooth connectivity).
  4. HC-SR04 Ultrasonic Sensor – 1 unit (for obstacle detection).
  5. DC Gear Motors – 2 or 4 units (for car movement).
  6. Wheels – 2 or 4 units (to attach to the motors).
  7. Battery (7.4V Li-ion or 9V) – 1 unit (power source).
  8. Battery Holder – 1 unit (to secure the battery).
  9. Jumper Wires – 10–20 pieces (for wiring).
  10. Switch – 1 unit (to turn the car on and off).

Tools and Accessories

  1. Car Chassis – 1 unit (base to mount components).
  2. Screwdriver – For assembly.
  3. Double-sided Tape or Screws – To secure components.
  4. Breadboard (optional) – For testing connections.
  5. Hot Glue Gun (optional) – To secure components to the chassis.

Software

  1. Arduino IDE – For programming the Arduino.
  2. Bluetooth Controller App – A mobile app to control the car via Bluetooth.

Steps to Make It

Steps to Build the Arduino Bluetooth + Obstacle-Avoiding Car

Step 1: Assemble the Chassis

  1. Take the car chassis and attach the DC gear motors to the slots provided.
  2. Mount the wheels onto the motor shafts securely.
  3. If your chassis includes a caster wheel, attach it to the front or rear for stability.
  4. Secure the battery holder to the chassis using double-sided tape or screws.

Step 2: Wire the Motor Driver (L298N)

  1. Connect the DC motors to the output terminals of the L298N motor driver (labeled OUT1, OUT2, OUT3, OUT4).
  2. Left motor: OUT1 and OUT2.
  3. Right motor: OUT3 and OUT4.
  4. Connect the +ve and -ve terminals of the battery to the motor driver’s power inputs (VCC and GND).
  5. Connect the 12V jumper pin from the motor driver to the 5V pin to power the Arduino.

Step 3: Connect the Ultrasonic Sensor (HC-SR04)

  1. Mount the ultrasonic sensor on the front of the chassis using screws or glue.
  2. Wire it to the Arduino:
  3. VCC to 5V.
  4. GND to GND.
  5. TRIG to digital pin 9.
  6. ECHO to digital pin 10.

Step 4: Connect the Bluetooth Module (HC-05)

  1. Place the HC-05 module on the chassis.
  2. Wire it to the Arduino:
  3. VCC to 5V.
  4. GND to GND.
  5. TXD to Arduino pin 11 (RX).
  6. RXD to Arduino pin 10 (TX) using a voltage divider (to prevent over-voltage).

Step 5: Arduino Connections

  1. Connect the motor driver input pins to the Arduino:
  2. IN1 to pin 2.
  3. IN2 to pin 3.
  4. IN3 to pin 4.
  5. IN4 to pin 5.
  6. Connect the Enable pins of the motor driver:
  7. ENA to pin 6.
  8. ENB to pin 7.

Step 6: Upload the Code

  1. Open the Arduino IDE and load your obstacle-avoidance and Bluetooth control code.
  2. Verify and upload the code to the Arduino via USB.
  3. Ensure the Bluetooth module is disconnected during the upload process.

Step 7: Test the Car

  1. Power the car using the battery.
  2. Pair the Bluetooth module with your smartphone using a Bluetooth controller app.
  3. Test the obstacle avoidance by placing objects in the car’s path.
  4. Use the Bluetooth app to control the car manually and switch between modes.

Step 8: Fine-Tune and Record

  1. Adjust the ultrasonic sensor’s range if necessary.
  2. Record a demo video showcasing both automatic and Bluetooth-controlled modes.


Coding

Bluetooth + Obstacle-Avoiding Car project. The code integrates obstacle avoidance using an ultrasonic sensor and manual Bluetooth control using the HC-05 module.

Code Explanation:

  1. The car avoids obstacles when no Bluetooth commands are received.
  2. Bluetooth commands control the car manually.
  3. 'F': Forward
  4. 'B': Backward
  5. 'L': Left
  6. 'R': Right
  7. 'S': Stop

Arduino Code

cpp
Copy code
// Pin Definitions
#define ENA 6 // Left motor speed
#define ENB 7 // Right motor speed
#define IN1 2 // Left motor forward
#define IN2 3 // Left motor backward
#define IN3 4 // Right motor forward
#define IN4 5 // Right motor backward

#define TRIG_PIN 9 // Ultrasonic Sensor TRIG
#define ECHO_PIN 10 // Ultrasonic Sensor ECHO

// Variables
char command; // Bluetooth command
long duration;
int distance;

// Function to measure distance
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2; // Convert to centimeters
}

// Motor Control Functions
void stopCar() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void setup() {
// Motor pins
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

// Bluetooth communication
Serial.begin(9600);

// Set motor speed
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}

void loop() {
// Check for Bluetooth command
if (Serial.available()) {
command = Serial.read();
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':
stopCar();
break;
}
} else {
// Automatic Obstacle Avoidance Mode
distance = getDistance();
if (distance < 20) { // If an obstacle is closer than 20 cm
stopCar();
delay(500);
moveBackward();
delay(500);
turnRight();
delay(500);
stopCar();
} else {
moveForward();
}
}
}

How It Works:

  1. Bluetooth Control:
  2. When a command is received via Bluetooth, the car performs the corresponding action.
  3. Obstacle Avoidance:
  4. If no Bluetooth command is received, the ultrasonic sensor continuously checks for obstacles. If an obstacle is detected within 20 cm, the car stops, moves backward, and turns.