Introduction:
An autonomous robot car is a vehicle that can navigate and perform tasks without the need for human intervention. A 2WD autonomous robot car is a type of robot car that is powered by two wheels, as opposed to four or more. These types of robots are commonly used for educational purposes, research and development, and in industries where manual labor is dangerous or impractical. 2WD autonomous robot cars typically use a combination of sensors, software, and control systems to make decisions and navigate their surroundings. These robots are capable of performing a variety of tasks, such as object detection, path planning, and obstacle avoidance, making them versatile and useful tools in many different applications.
Motivation for the project:
Recently, most of our customers that purchased the 2WD Robot Cars Chassis have been asking us for schematic and instructions on how to use the module, hence, we decided to do this project as an instruction material for anybody that is also interested in building 2WD Robot car.
Parts list for the 2WD Autonomous Obstacle Avoiding Robot Car:
Parts | Quantity | Link to buy part |
2WD Robot Car Chassis Kit | 1 | Buy here |
Arduino UNO x1 | 1 | Buy here |
L298N Motor Driver Module | 1 | Buy here |
HC-SR04 Ultrasonic Sensor | 1 | Buy here |
SG90 180 Degrees 9G Micro Servo Motor | 1 | Buy here |
Ultrasonic Sensor Mount | 1 | Buy here |
SG90 Micro Servo mount | 1 | Buy here |
20CM 40 Lines Male to Female Dupont line | Buy here | |
65 Pieces Breadboard Jumper Wires | Buy here |
The schematic Diagram
The sketch (Code)
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Project: 2WD Autonomous Obstacle Avoiding Robot Car using Arduino and Ultrasonic Sensor
Copyright (C) 2023 Izuchukwu Madueke,
For: www.DIYMall.com.ng
Project link:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
#include <Servo.h> //Servo motor library.
#include <NewPing.h>//Ultrasonic sensor library.
//L298N control pins
const int left_motor_speed = 3;//ENB, PWM Pin
const int left_motor_forward = 5;//IN3
const int left_motor_backward = 4;//IN4
const int right_motor_speed = 11; //ENA, PWM pin
const int right_motor_forward = 8; //IN1
const int right_motor_backward = 7;//IN2
//Ultrasonic Sensor pins
#define trig_pin A1 //ultrasonic Sensor Trig pin
#define echo_pin A2 //ultrasonic Sensor Echo pin
#define maximum_speed 255 //0-255
#define maximum_distance 300 //in cm
boolean moving_forward = false;
int distance = 150; //in cm
NewPing sonar(trig_pin, echo_pin, maximum_distance); //Initialise ultrasonic Sensor
Servo servo_motor; //Initialise servo motor
void setup(){
Serial.begin(9600);
//Set all motor pins as output
pinMode(right_motor_forward, OUTPUT);
pinMode(left_motor_forward, OUTPUT);
pinMode(left_motor_backward, OUTPUT);
pinMode(right_motor_backward, OUTPUT);
pinMode(right_motor_speed, OUTPUT);
pinMode(left_motor_speed, OUTPUT);
servo_motor.attach(10); //set servo motor pin, Arduino pin 10
servo_motor.write(115); //initial servo motor position
delay(2000);
distance = check_distance();
delay(100);
distance = check_distance();
delay(100);
distance = check_distance();
delay(100);
distance = check_distance();
delay(100);
}
void loop(){
int distance_right = 0;
int distanc_left = 0;
delay(50);
if (distance <= 30){
stop_car();
delay(300);
move_backward();
delay(300);
stop_car();
delay(300);
distance_right = look_right();
delay(300);
distanc_left = look_left();
delay(300);
if (distance_right >= distanc_left){
turn_left();
stop_car();
}
else{
turn_right();
stop_car();
}
}
else{
move_forward();
}
distance = check_distance();
}
void move_forward(){
if(!moving_forward){
moving_forward = true;
Serial.println("Move Forward");
digitalWrite(left_motor_forward, HIGH);
digitalWrite(right_motor_forward, HIGH);
digitalWrite(left_motor_backward, LOW);
digitalWrite(right_motor_backward, LOW);
for (int i = 0; i < maximum_speed; i += 5){
analogWrite(right_motor_speed, i);
analogWrite(left_motor_speed, i);
delay(10);
}
}
}
void move_backward(){
moving_forward = false;
Serial.println("Move Backward");
digitalWrite(left_motor_backward, HIGH);
digitalWrite(right_motor_backward, HIGH);
digitalWrite(left_motor_forward, LOW);
digitalWrite(right_motor_forward, LOW);
for (int i = 0; i < maximum_speed; i += 5){
analogWrite(right_motor_speed, i);
analogWrite(left_motor_speed, i);
delay(10);
}
}
void turn_right(){
Serial.println("Turn Right");
digitalWrite(left_motor_forward, HIGH);
digitalWrite(right_motor_backward, HIGH);
digitalWrite(left_motor_backward, LOW);
digitalWrite(right_motor_forward, LOW);
delay(500);
digitalWrite(left_motor_forward, HIGH);
digitalWrite(right_motor_forward, HIGH);
digitalWrite(left_motor_backward, LOW);
digitalWrite(right_motor_backward, LOW);
}
void turn_left(){
Serial.println("Turn Left");
digitalWrite(left_motor_backward, HIGH);
digitalWrite(right_motor_forward, HIGH);
digitalWrite(left_motor_forward, LOW);
digitalWrite(right_motor_backward, LOW);
delay(500);
digitalWrite(left_motor_forward, HIGH);
digitalWrite(right_motor_forward, HIGH);
digitalWrite(left_motor_backward, LOW);
digitalWrite(right_motor_backward, LOW);
}
void stop_car(){
// Decelerate from maximum speed to zero
for (int i = maximum_speed; i >= 0; i-=5) {
analogWrite(right_motor_speed, i);
analogWrite(left_motor_speed, i);
delay(20);
}
digitalWrite(right_motor_forward, LOW);
digitalWrite(left_motor_forward, LOW);
digitalWrite(right_motor_backward, LOW);
digitalWrite(left_motor_backward, LOW);
}
int look_right(){
servo_motor.write(50);
delay(500);
int distance = check_distance();
delay(100);
servo_motor.write(115);
return distance;
}
int look_left(){
servo_motor.write(170);
delay(500);
int distance = check_distance();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
//
int check_distance(){
delay(50);
int distance_in_cm = sonar.ping_cm();
if (distance_in_cm == 0){
distance_in_cm = 300; //cm
}
return distance_in_cm;
}