Embark on an exciting journey into robotics with this step-by-step guide to building your very own obstacle-avoiding car using Arduino! This project is perfect for beginners eager to learn about electronics, programming, and the fascinating world of autonomous vehicles. We’ll utilize the popular Arduino UNO, along with a few readily available components, to create a smart car that can navigate its way around obstacles. Let’s dive in and bring your robotic car to life!
Components You’ll Need
To get started, gather these components. They are commonly used in Arduino robotics projects and are easily accessible:
- Arduino UNO: The brain of your car, processing code and controlling components.
- L298N Motor Driver: An essential module to control the DC motors, enabling forward, backward, and turning movements.
- DC Motors with Wheels: Provide the locomotion for your robot car.
- Servo Motor (MG90S): Used to pan the ultrasonic sensor, allowing the car to “see” its surroundings.
- Ultrasonic Sensor (HC-SR04): The “eyes” of your robot, measuring distances to detect obstacles.
- Jumper Wires: For connecting components to the Arduino and motor driver.
- 9V Battery or Power Source: To power the Arduino and motors.
- Chassis (Optional): A platform to mount all the components, making your car sturdy and mobile.
Alt Text: An Arduino UNO microcontroller board, the central processing unit for the obstacle avoiding car project.
Wiring it All Together
Connecting the components correctly is crucial. Here’s a basic wiring scheme to follow:
-
L298N Motor Driver to Arduino:
- L298N IN1 to Arduino Pin 7
- L298N IN2 to Arduino Pin 6
- L298N IN3 to Arduino Pin 4
- L298N IN4 to Arduino Pin 5
- L298N EN-A to Arduino Pin (PWM – e.g., 9, but not used in this code directly for speed control)
- L298N EN-B to Arduino Pin (PWM – e.g., 10, but not used in this code directly for speed control)
- L298N VCC (Motor Power) to Battery Positive (+)
- L298N GND to Battery Negative (-) and Arduino GND
- L298N 5V to Arduino 5V (for logic power)
-
DC Motors to L298N:
- Motor 1 to L298N OUT1 and OUT2
- Motor 2 to L298N OUT3 and OUT4
-
Servo Motor to Arduino:
- Servo Signal Wire (usually yellow or orange) to Arduino Pin 11
- Servo VCC (usually red) to Arduino 5V
- Servo GND (usually brown or black) to Arduino GND
-
Ultrasonic Sensor to Arduino:
- Ultrasonic Sensor VCC to Arduino 5V
- Ultrasonic Sensor GND to Arduino GND
- Ultrasonic Sensor Trig to Arduino Analog Pin A1
- Ultrasonic Sensor Echo to Arduino Analog Pin A2
Alt Text: A L298N dual H-bridge motor driver module, used to control the robot car’s DC motors.
Arduino Code Explained
Now, let’s break down the Arduino code that makes your car intelligent. Make sure you have the Servo.h
library (standard) and the NewPing.h
library installed in your Arduino IDE. You may need to install the NewPing library through the Arduino Library Manager (Sketch > Include Library > Manage Libraries…).
#include <Servo.h> // Servo motor library. This is standard library
#include <NewPing.h> // Ultrasonic sensor function library. You must install this library
// Define L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
// Define sensor pins
#define trig_pin A1 // analog input 1
#define echo_pin A2 // analog input 2
#define maximum_distance 200 // Maximum distance the ultrasonic sensor will measure (in cm)
boolean goesForward = false;
int distance = 100; // Initial distance value
NewPing sonar(trig_pin, echo_pin, maximum_distance); // Create a NewPing object for the ultrasonic sensor
Servo servo_motor; // Create a Servo object for the servo motor
void setup() {
// Set motor control pins as OUTPUT
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(11); // Attach the servo to pin 11
servo_motor.write(85); // Center the servo (90 degrees is often center, but 85 used here)
delay(2000); // Wait for servo to settle
// Initial distance readings to calibrate (optional but recommended)
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50); // Short delay to prevent অতি দ্রুত looping
if (distance <= 25) { // If obstacle detected within 25cm
moveStop(); // Stop the car
delay(300);
moveBackward(); // Move backward briefly
delay(400);
moveStop(); // Stop again
delay(300);
distanceRight = lookRight(); // Check distance to the right
delay(200);
distanceLeft = lookLeft(); // Check distance to the left
delay(300);
if (distanceRight >= distanceLeft) { // If right is clearer or equal
turnRight(); // Turn right
moveStop();
} else { // If left is clearer
turnLeft(); // Turn left
moveStop();
}
} else {
moveForward(); // If no obstacle, move forward
}
distance = readPing(); // Update distance reading for next loop
}
// Function to look right and return distance
int lookRight() {
servo_motor.write(0); // Turn servo to the right (0 degrees)
delay(500);
int distance = readPing(); // Read distance
delay(100);
servo_motor.write(85); // Return servo to center
return distance;
}
// Function to look left and return distance
int lookLeft() {
servo_motor.write(170); // Turn servo to the left (170 degrees)
delay(500);
int distance = readPing(); // Read distance
delay(100);
servo_motor.write(85); // Return servo to center
return distance;
delay(100); // Extra delay - likely unnecessary
}
// Function to read distance using ultrasonic sensor
int readPing() {
delay(70); // Delay before pinging
int cm = sonar.ping_cm(); // Get distance in centimeters
if (cm == 0) {
cm = 250; // Return a large value if no reading (out of range or error)
}
return cm;
}
// Function to stop the car
void moveStop() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
// Function to move the car forward
void moveForward() {
if (!goesForward) { // Check if already moving forward to prevent redundant commands
goesForward = true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
// Function to move the car backward
void moveBackward() {
goesForward = false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
// Function to turn the car right
void turnRight() {
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500); // Turn duration - adjust as needed
moveStop(); // Stop after turning (removed redundant forward movement after turn in original code)
}
// Function to turn the car left
void turnLeft() {
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500); // Turn duration - adjust as needed
moveStop(); // Stop after turning (removed redundant forward movement after turn in original code)
}
Alt Text: A snippet of Arduino code for the obstacle avoiding car program, showing the loop function and obstacle detection logic.
Code Breakdown:
- Includes: Imports necessary libraries for servo motor and ultrasonic sensor control.
- Pin Definitions: Assigns Arduino pins to motor driver inputs and sensor pins for easy reference.
setup()
function:- Initializes pin modes (INPUT/OUTPUT).
- Attaches the servo motor to a pin.
- Centers the servo motor.
- Takes initial distance readings.
loop()
function: The heart of the program, continuously running:- Reads distance from the ultrasonic sensor.
- Obstacle Detection: If an obstacle is closer than 25cm:
- Stops, backs up, then stops again.
- Servo motor looks right and left, measuring distances.
- Car turns in the direction with more clearance (right or left).
- No Obstacle: If no obstacle is detected, the car moves forward.
lookRight()
andlookLeft()
functions: Control the servo to pan right and left, returning distance readings.readPing()
function: Reads distance from the ultrasonic sensor using theNewPing
library.- Movement Functions (
moveStop()
,moveForward()
,moveBackward()
,turnRight()
,turnLeft()
): Control the motors via the L298N driver to perform car movements.
Running Your Obstacle Avoiding Car
- Upload the Code: Copy and paste the code into your Arduino IDE and upload it to your Arduino UNO board.
- Power Up: Connect a 9V battery or suitable power source to your L298N motor driver and Arduino.
- Observe: Place your robot car on a flat surface and watch it navigate! It should move forward and intelligently avoid obstacles by turning.
Next Steps and Improvements
This project is a fantastic starting point. You can expand upon it in many ways:
- Speed Control: Implement Pulse Width Modulation (PWM) to control motor speed, making movements smoother.
- More Sophisticated Obstacle Avoidance: Add more sensors (e.g., line follower sensors, infrared sensors) for more complex navigation.
- Remote Control: Integrate Bluetooth or Wi-Fi to control your car remotely.
- Custom Chassis: Design and build a custom chassis for a more robust and personalized robot.
- Machine Learning: For advanced projects, explore incorporating machine learning algorithms for more adaptive obstacle avoidance and path planning.
Building an Arduino obstacle avoiding car is a rewarding project that combines hardware and software. Experiment, modify the code, and continue to explore the exciting world of robotics!