Arduino-Based Self-Driving Car with Ultrasonic Sensors and DC Motors
View and Edit Project In Cirkit Designer
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
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor used for obstacle detection.
- Pins: VCC, TRIG, ECHO, GND
DC Motor
- Description: Motor used to drive the car.
- Pins: pin 1, pin 2
Drv8833
- Description: Motor driver used to control the DC motors.
- Pins: EEP, OUT1, OUT2, OUT3, OUT4, ULT, IN4, IN3, GND, VCC, IN1, IN2
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
Battery 12V
- Description: Power source for the system.
- Pins: +, -
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V on Arduino UNO
- TRIG: Connected to D13, D11, D9, D7, D5 on Arduino UNO
- ECHO: Connected to D12, D10, D8, D6, D4 on Arduino UNO
- GND: Connected to GND on Arduino UNO
DC Motor
Motor 1:
- pin 1: Connected to OUT4 on Drv8833
- pin 2: Connected to OUT3 on Drv8833
Motor 2:
- pin 1: Connected to OUT1 on Drv8833
- pin 2: Connected to OUT2 on Drv8833
Drv8833
- EEP: Not connected
- OUT1: Connected to pin 1 of Motor 2
- OUT2: Connected to pin 2 of Motor 2
- OUT3: Connected to pin 2 of Motor 1
- OUT4: Connected to pin 1 of Motor 1
- ULT: Not connected
- IN4: Connected to A3 on Arduino UNO
- IN3: Connected to A2 on Arduino UNO
- GND: Connected to GND on Arduino UNO and battery 12V
- VCC: Connected to + on battery 12V
- IN1: Connected to A0 on Arduino UNO
- IN2: Connected to A1 on Arduino UNO
Arduino UNO
- 5V: Connected to VCC of all HC-SR04 Ultrasonic Sensors
- GND: Connected to GND of all HC-SR04 Ultrasonic Sensors, Drv8833, and battery 12V
- Vin: Connected to + on battery 12V
- A0: Connected to IN1 on Drv8833
- A1: Connected to IN2 on Drv8833
- A2: Connected to IN3 on Drv8833
- A3: Connected to IN4 on Drv8833
- D13: Connected to TRIG of one HC-SR04 Ultrasonic Sensor
- D12: Connected to ECHO of one HC-SR04 Ultrasonic Sensor
- D11: Connected to TRIG of one HC-SR04 Ultrasonic Sensor
- D10: Connected to ECHO of one HC-SR04 Ultrasonic Sensor
- D9: Connected to TRIG of one HC-SR04 Ultrasonic Sensor
- D8: Connected to ECHO of one HC-SR04 Ultrasonic Sensor
- D7: Connected to TRIG of one HC-SR04 Ultrasonic Sensor
- D6: Connected to ECHO of one HC-SR04 Ultrasonic Sensor
- D5: Connected to TRIG of one HC-SR04 Ultrasonic Sensor
- D4: Connected to ECHO of one HC-SR04 Ultrasonic Sensor
Battery 12V
- +: Connected to Vin on Arduino UNO and VCC on Drv8833
- -: Connected to GND on Arduino UNO and Drv8833
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.