Creating a driverless car using Arduino
Creating a driverless car using Arduino is a complex but rewarding project. Below is a step-by-step guide to building a basic autonomous car using Arduino, sensors, and motors. This project will use an ultrasonic sensor for obstacle detection and an L298N motor driver to control the motors.
---
### **Components Required**
1. **Arduino Uno** (or any compatible board)
2. **Ultrasonic Sensor (HC-SR04)** - For obstacle detection
3. **L298N Motor Driver** - To control DC motors
4. **DC Motors (2 or 4)** - For movement
5. **Chassis with Wheels** - For the car body
6. **Battery Pack (6V or 9V)** - To power the motors and Arduino
7. **Jumper Wires** - For connections
8. **Breadboard** - For prototyping
9. **Wheel Encoders (Optional)** - For precise movement control
10. **Servo Motor (Optional)** - For rotating the ultrasonic sensor
---
### **Circuit Diagram**
1. **L298N Motor Driver Connections**:
- Connect `IN1`, `IN2`, `IN3`, and `IN4` to Arduino digital pins (e.g., 8, 9, 10, 11).
- Connect `ENA` and `ENB` to PWM pins (e.g., 5, 6) for speed control.
- Connect the motor terminals to the L298N output pins.
- Connect the L298N `VCC` to the battery pack and `GND` to the common ground.
- Connect the L298N `5V` pin to the Arduino `5V` pin.
2. **Ultrasonic Sensor Connections**:
- Connect `VCC` to Arduino `5V`.
- Connect `GND` to Arduino `GND`.
- Connect `Trig` to Arduino digital pin (e.g., 12).
- Connect `Echo` to Arduino digital pin (e.g., 13).
3. **Power Connections**:
- Connect the battery pack to the L298N motor driver and Arduino (via Vin or external power jack).
---
### **Arduino Code**
Below is the Arduino code for a basic obstacle-avoiding car:
```cpp
// Define motor control pins
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
#define ENA 5
#define ENB 6
// Define ultrasonic sensor pins
#define TRIG 12
#define ECHO 13
// Define distance threshold (in cm)
#define DISTANCE_THRESHOLD 20
void setup() {
// Set motor control pins as output
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
// Set ultrasonic sensor pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Measure distance
long distance = measureDistance();
// If obstacle is detected, stop and turn
if (distance < DISTANCE_THRESHOLD) {
stopCar();
delay(500);
turnRight();
delay(500);
} else {
moveForward();
}
}
// Function to measure distance using ultrasonic sensor
long measureDistance() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
long distance = duration * 0.034 / 2; // Convert to cm
Serial.print("Distance: ");
Serial.println(distance);
return distance;
}
// Function to move forward
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, 200); // Set speed
analogWrite(ENB, 200);
}
// Function to stop the car
void stopCar() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
// Function to turn right
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA, 200);
analogWrite(ENB, 200);
delay(500); // Adjust delay for turning angle
}
```
---
### **How It Works**
1. **Ultrasonic Sensor**:
- The sensor measures the distance to obstacles in front of the car.
- If an obstacle is detected within the threshold distance (e.g., 20 cm), the car stops and turns right.
2. **Motor Control**:
- The L298N motor driver controls the direction and speed of the motors.
- The car moves forward by default and turns when an obstacle is detected.
3. **Power Supply**:
- The battery pack powers both the Arduino and the motors.
---
### **Enhancements**
1. **Add More Sensors**:
- Use multiple ultrasonic sensors or IR sensors for better obstacle detection.
- Add a line-following sensor for path tracking.
2. **Use a Servo Motor**:
- Attach the ultrasonic sensor to a servo motor to scan the environment in multiple directions.
3. **Implement PID Control**:
- Use wheel encoders and PID control for precise movement and turning.
4. **Bluetooth/WiFi Control**:
- Add a Bluetooth or WiFi module (e.g., HC-05 or ESP8266) for remote control.
5. **Mapping and Navigation**:
- Use an advanced microcontroller (e.g., Raspberry Pi) for mapping and navigation algorithms.
---
### **Testing and Calibration**
1. Test the car in an open area with no obstacles.
2. Adjust the distance threshold and turning delay for better performance.
3. Calibrate the motor speeds to ensure smooth movement.
---
No Comments have been Posted.