Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Self-Driving Car with Ultrasonic Sensors and DC Motors

View and Edit Project In Cirkit DesignerImage of Arduino-Based Self-Driving Car with Ultrasonic Sensors and DC Motors

Self-Driving Car Project Documentation

Summary

This project involves the design and implementation of a self-driving car using an Arduino UNO microcontroller. The car uses five HC-SR04 ultrasonic sensors to detect and avoid obstacles. Two DC motors drive the car, controlled via a DRV8833 motor driver. The system is powered by a 12V battery.

Component List

  1. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor used for obstacle detection.
    • Pins: VCC, TRIG, ECHO, GND
  2. DC Motor

    • Description: Motor used to drive the car.
    • Pins: pin 1, pin 2
  3. Drv8833

    • Description: Motor driver used to control the DC motors.
    • Pins: EEP, OUT1, OUT2, OUT3, OUT4, ULT, IN4, IN3, GND, VCC, IN1, IN2
  4. Arduino UNO

    • Description: Microcontroller used to control the entire system.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  5. Battery 12V

    • Description: Power source for the system.
    • Pins: +, -

Wiring Details

HC-SR04 Ultrasonic Sensor

DC Motor

Drv8833

Arduino UNO

Battery 12V

Code Documentation

/*
 * Self-Driving Car Project
 * This Arduino sketch controls a self-driving car using 5 HC-SR04 ultrasonic
 * sensors to detect and avoid obstacles. The car is driven by two DC motors
 * controlled via a DRV8833 motor driver.
 */

// Pin definitions for ultrasonic sensors
const int trigPins[5] = {13, 11, 9, 7, 5};
const int echoPins[5] = {12, 10, 8, 6, 4};

// Pin definitions for motor driver
const int motorPins[4] = {A0, A1, A2, A3};

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize ultrasonic sensor pins
  for (int i = 0; i < 5; i++) {
    pinMode(trigPins[i], OUTPUT);
    pinMode(echoPins[i], INPUT);
  }

  // Initialize motor driver pins
  for (int i = 0; i < 4; i++) {
    pinMode(motorPins[i], OUTPUT);
  }
}

void loop() {
  // Array to store distances from sensors
  long distances[5];

  // Read distances from all sensors
  for (int i = 0; i < 5; i++) {
    distances[i] = readUltrasonic(trigPins[i], echoPins[i]);
  }

  // Determine car movement based on sensor readings
  controlMotors(distances);

  // Small delay to avoid excessive processing
  delay(100);
}

long readUltrasonic(int trigPin, int echoPin) {
  // Send a 10us pulse to trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo pin and calculate distance
  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;
  return distance;
}

void controlMotors(long distances[]) {
  // Example logic to avoid obstacles
  if (distances[0] < 20 || distances[1] < 20 || distances[2] < 20 ||
      distances[3] < 20 || distances[4] < 20) {
    // If any sensor detects an obstacle within 20 cm, stop the car
    stopCar();
  } else {
    // Otherwise, move forward
    moveForward();
  }
}

void stopCar() {
  // Stop both motors
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], LOW);
}

void moveForward() {
  // Move both motors forward
  digitalWrite(motorPins[0], HIGH);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], HIGH);
  digitalWrite(motorPins[3], LOW);
}

This documentation provides a comprehensive overview of the self-driving car project, including the components used, their wiring details, and the code running on the Arduino UNO microcontroller.