Project 3 Ultrasonic Obstacle Avoidance Smart Car
Ultrasonic obstacle avoidance is easy to realize, simple in calculation. It is easy to control it in real time with practical measuring accuracy. Therefore, it has become a common method for obstacle avoidance. For the application method of ultrasonic, please refer to “Arduino ultrasonic ranging instruction”. Below is the connection diagram for ultrasonic obstacle avoidance:

1. Connection of motor
Motor 1 to L298N MOTOA
Motor 2 to L298N MOTOB
2. Power supply of L298N
Use 1 contact of battery case of 6 cells of AA batteries to supply power for L298N motor driver module, another contact for Arduino main board. The + of the power supply for L298N motor driver module is connected to the VMS of L298N; the - to the GND. + 5V interface of L298N is not connected to anything.
3. The enable and turning function of the motor (with program)
int pinLB=5; // define pin 5 for left and back, connected to pin PWM5of the controller
int pinLF=6; // define pin 6 for left and front, connected to pin PWM6 of the controller board
int pinRB=10; // define pin 10 for right and back, connected to pin PWM10 of the controller board
int pinRF=11; // define pin 11 for right and front, connected to pin PWM11 of the controller board
4. Connection of the servo motor
myservo.attach(9); // set servo motor output as pin 9(PWM)
5.Connection of the ultrasonic sensor

4.pins for this sensor
VCC to +5V
TRIQ signal input
ECHO signal output GND to GND
int inputPin = 13; // define receiving pin for ultrasonic signal
int outputPin =12; // define sending pin for ultrasonic signal
5.Ultrasonic obstacle avoidance smart car program (ARDUINO)
Code 4
/*
L = left
R = right
F = front
B = back
*/
#include <Servo.h>
int pinLB=5; // define pin 6 as left and back
int pinLF=6; // define pin 9 as left and front
int pinRB=10; // define pin 10 as right and back
int pinRF=11; // define pin 11 as right and front
int inputPin = 13; // define receiving pin for ultrasonic signal
int outputPin =12; // define sending pin for ultrasonic signal
int Fspeedd = 0; // speed going forward
int Rspeedd = 0; // speed going right
int Lspeedd = 0; // speed going left
int directionn = 0; //F=8 B=2 L=4 R=6
Servo myservo; // set myservo
int delay_time = 250; // settling time for the servo motor moving backwards
int Fgo = 8; // going forward
int Rgo = 6; // going right
int Lgo = 4; // going left
int Bgo = 2; // going backwards
void setup()
{
Serial.begin(9600); // define motor output
pinMode(pinLB,OUTPUT); // pin 5 (PWM)
pinMode(pinLF,OUTPUT); // pin 6 (PWM)
pinMode(pinRB,OUTPUT); // pin 10 (PWM)
pinMode(pinRF,OUTPUT); // pin 11 (PWM)
pinMode(inputPin, INPUT); // define receiving pin for ultrasonic signal
pinMode(outputPin, OUTPUT); // define sending pin for ultrasonic signal
myservo.attach(9);// set servo motor output as pin 9(PWM)
}
void advance(int a) // going forward
{
digitalWrite(pinRB,LOW); // motor move right and back
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW); // motor move to left and back
digitalWrite(pinLF,HIGH);
delay(a * 100);
}
void right(int b) // turn right(1 wheel)
{
digitalWrite(pinRB,LOW); //motor move right and back
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
delay(b * 100);
}
void left(int c) // turn left(1 wheel)
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW); // motor move left and back
digitalWrite(pinLF,HIGH);
delay(c * 100);
}
void turnR(int d) // turn right( 2 wheels)
{
digitalWrite(pinRB,LOW); // motor move right and back
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW); //motor move left and front
delay(d * 100);
}
void turnL(int e)// turn left(2 wheels)
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW); // motor move right and front
digitalWrite(pinLB,LOW); // motor move left and back
digitalWrite(pinLF,HIGH);
delay(e * 100);
}
void stopp(int f)// stop
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
delay(f * 100);
}
void back(int g)// going backwards
{
digitalWrite(pinRB,HIGH); // motor move right and back
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH); // motor move left and back
digitalWrite(pinLF,LOW);
delay(g * 100);
}
void detection()// measure 3 angles(0.90.179)
{
int delay_time = 250; // settling time for the servo motor moving backwards
ask_pin_F();// read the distance upfront
if(Fspeedd < 10)// if distance less than 10cm
{
stopp(1);// clear output information
back(2);// going backwards for 0.2 second
}
if(Fspeedd < 25)// if distance less than 25cm
{
stopp(1);// clear output information
ask_pin_L();// read the distance on the left
delay(delay_time);// settling time for the servo
ask_pin_R();// read the distance on the right
delay(delay_time);// settling time for the servo
if(Lspeedd > Rspeedd) // if distance on the left is more than that on the right
{
directionn = Rgo;// going right
}
if(Lspeedd <= Rspeedd) // if distance on the left is less than that on the right
{
directionn = Lgo;// going left
}
if (Lspeedd < 10 && Rspeedd < 10) // if both distance are less than 10cm
{
directionn = Bgo;// going backwards
}
}
else// if the distance upfront is more than 25cm
{
directionn = Fgo;// going forward
}
}
void ask_pin_F() // measure the distance upfront
{
myservo.write(90);
digitalWrite(outputPin, LOW); // ultrasonic sends out low voltage 2μs delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic sends out high voltage 10μs, at least 10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW);// maintain low voltage sending
float Fdistance = pulseIn(inputPin, HIGH); // read the time difference
Fdistance= Fdistance/5.8/10;// convert time into distance(unit: cm)
Serial.print("F distance:");// output distance in cm
Serial.println(Fdistance);// display distance
Fspeedd = Fdistance;// read the distance data into Fspeedd
}
void ask_pin_L()// measure the distance on the left
{
myservo.write(9);
delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic sends out low voltage 2μs
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);// ultrasonic sends out high voltage 10μs, at least 10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW);// maintain low voltage sending
float Ldistance = pulseIn(inputPin, HIGH); // read the time difference
Ldistance= Ldistance/5.8/10;// convert time into distance(unit: cm)
Serial.print("L distance:"); //output distance in cm
Serial.println(Ldistance);// display distance
Lspeedd = Ldistance;// read the distance data into Lspeedd
}
void ask_pin_R()// measure the distance on the right
{
myservo.write(177); delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic sends out low voltage 2μs
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic sends out high voltage 10μs, at least 10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // maintain low voltage sending
float Rdistance = pulseIn(inputPin, HIGH); //read the time difference
Rdistance= Rdistance/5.8/10; // convert time into distance (unit: cm)
Serial.print("R distance:"); // output distance in cm
Serial.println(Rdistance);// display distance
Rspeedd = Rdistance;// read the distance data into Rspeedd
}
void loop()
{
myservo.write(90); // reset the servo motor and prepare it for the next measurement
detection(); // measure the angle and decide which direction to move
if(directionn == 2) //if directionn = 2
{
back(8);// going backwards
turnL(2);// slightly move to the left to avoid stuck in the dead end
Serial.print(" Reverse "); // display direction (backwards)
}
if(directionn == 6)// if direction = 6
{
back(1);
turnR(6);// turn right
Serial.print(" Right ");// display direction(right)
}
if(directionn == 4)//if direction = 4
{
back(1);
turnL(6);// turn left
Serial.print(" Left ");// display direction(left)
}
if(directionn == 8)//if direction= 8
{
advance(1);// going forward
Serial.print(" Advance "); //display direction(forward)
Serial.print(" ");
}
}