Arduino Self-Driving Car Project
Project Overview
This project involves building a small self-driving car using Arduino. The car will be able to
detect and follow a path or avoid obstacles autonomously. This is an excellent project for
learning basic robotics, sensor integration, and programming.
Key Features:
Line following using IR sensors
Obstacle avoidance using an ultrasonic sensor
Motor control using a motor driver
Arduino-based control system
Components Required
Component Quantity Description
Arduino Uno 1 Microcontroller board
L298N Motor Driver 1 To control DC motors
DC Motors with Wheels 2 Provides movement
Servo Motor 1 For rotating ultrasonic sensor (optional)
Ultrasonic Sensor (HC-SR04) 1 Obstacle detection
IR Sensors Module 2-5 Line detection
Chassis 1 Base of the car
Jumper Wires As needed Connections
Battery Pack 1 Power source (7.4–12V recommended)
Breadboard 1 Optional, for connections
Circuit Diagram
Here’s a simplified diagram layout:
+---------------------+
| Arduino Uno |
| |
5V ---| 5V GND ---|--- GND
GND ---| GND |
D2 ---| IR Sensor Left |
D3 ---| IR Sensor Right |
D9 ---| Motor1 PWM |
D10 ---| Motor2 PWM |
D11 ---| Motor1 Direction |
D12 ---| Motor2 Direction |
D7 ---| Trig HC-SR04 |
D8 ---| Echo HC-SR04 |
+---------------------+
L298N Motor Driver:
IN1 -> D11
IN2 -> D12
IN3 -> D13
IN4 -> D4
ENA -> D9
ENB -> D10
(I can create a professional, visual circuit diagram for you if you want. It will be easy to follow.)
Working Principle
1. Line Following
IR sensors detect the contrast between the black line and white surface.
Based on sensor output:
o Both sensors detect white → Move forward
o Left sensor detects black → Turn left
o Right sensor detects black → Turn right
Motors are controlled via L298N accordingly.
2. Obstacle Avoidance
Ultrasonic sensor continuously measures distance in front of the car.
If an object is detected within a set distance (e.g., 10 cm), the car stops or changes
direction.
Arduino Code Example
#include <Servo.h>
// Motor pins
const int motor1Pin1 = 11;
const int motor1Pin2 = 12;
const int motor2Pin1 = 13;
const int motor2Pin2 = 4;
const int motorSpeedA = 9;
const int motorSpeedB = 10;
// IR Sensor pins
const int IR_Left = 2;
const int IR_Right = 3;
// Ultrasonic pins
const int trigPin = 7;
const int echoPin = 8;
long duration;
int distance;
void setup() {
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(IR_Left, INPUT);
pinMode(IR_Right, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](9600);
}
void loop() {
// Ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 10) {
stopCar(); // Obstacle detected
} else {
lineFollow();
}
}
void lineFollow() {
int leftState = digitalRead(IR_Left);
int rightState = digitalRead(IR_Right);
if (leftState == HIGH && rightState == HIGH) {
moveForward();
} else if (leftState == LOW) {
turnLeft();
} else if (rightState == LOW) {
turnRight();
}
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(motorSpeedA, 200);
analogWrite(motorSpeedB, 200);
}
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void stopCar() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
Steps to Build
1. Assemble the chassis and attach the motors and wheels.
2. Connect the L298N motor driver to Arduino and motors.
3. Mount IR sensors under the chassis for line detection.
4. Mount the ultrasonic sensor at the front for obstacle detection.
5. Connect all sensors and motor driver to Arduino as per the diagram.
6. Upload the Arduino code.
7. Test on a track and adjust sensor thresholds if needed.
Improvements
Use more IR sensors for sharper turns.
Add Bluetooth/Wi-Fi for remote monitoring.
Implement PID control for smoother line following.
Use a camera module (like OpenCV) for advanced path recognition.