Arduino Obstacle Avoiding Car using Servo and L298N Driver
The smart car which can find it's own way
Components and supplies
4
DC Motor, 12 V
1
SG90 Micro-servo motor
1
Jumper wires (generic)
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Solderless Breadboard Half Size
1
Arduino UNO
1
Dual H-Bridge motor drivers L298
1
9V battery (generic)
Tools and machines
1
New Ping Library
1
Hot glue gun (generic)
1
Tape, Double Sided
Apps and platforms
1
Arduino IDE
Project description
Code
CODE
c_cpp
1#include <Servo.h> //standard library for the servo 2#include <NewPing.h> //for the Ultrasonic sensor function library. 3 4//L298N motor control pins 5const int LeftMotorForward = 6; 6const int LeftMotorBackward = 7; 7const int RightMotorForward = 5; 8const int RightMotorBackward = 4; 9 10//sensor pins 11#define trig_pin A1 //analog input 1 12#define echo_pin A2 //analog input 2 13 14#define maximum_distance 200 15boolean goesForward = false; 16int distance = 100; 17 18NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function 19Servo servo_motor; 20 21void setup(){ 22 23 pinMode(RightMotorForward, OUTPUT); 24 pinMode(LeftMotorForward, OUTPUT); 25 pinMode(LeftMotorBackward, OUTPUT); 26 pinMode(RightMotorBackward, OUTPUT); 27 28 servo_motor.attach(9); //our servo pin 29 30 servo_motor.write(115); 31 delay(2000); 32 distance = readPing(); 33 delay(100); 34 distance = readPing(); 35 delay(100); 36 distance = readPing(); 37 delay(100); 38 distance = readPing(); 39 delay(100); 40} 41 42void loop(){ 43 44 int distanceRight = 0; 45 int distanceLeft = 0; 46 delay(50); 47 48 if (distance <= 20){ 49 moveStop(); 50 delay(300); 51 moveBackward(); 52 delay(400); 53 moveStop(); 54 delay(300); 55 distanceRight = lookRight(); 56 delay(300); 57 distanceLeft = lookLeft(); 58 delay(300); 59 60 if (distance >= distanceLeft){ 61 turnRight(); 62 moveStop(); 63 } 64 else{ 65 turnLeft(); 66 moveStop(); 67 } 68 } 69 else{ 70 moveForward(); 71 } 72 distance = readPing(); 73} 74 75int lookRight(){ 76 servo_motor.write(50); 77 delay(500); 78 int distance = readPing(); 79 delay(100); 80 servo_motor.write(115); 81 return distance; 82} 83 84int lookLeft(){ 85 servo_motor.write(170); 86 delay(500); 87 int distance = readPing(); 88 delay(100); 89 servo_motor.write(115); 90 return distance; 91 delay(100); 92} 93 94int readPing(){ 95 delay(70); 96 int cm = sonar.ping_cm(); 97 if (cm==0){ 98 cm=250; 99 } 100 return cm; 101} 102 103void moveStop(){ 104 105 digitalWrite(RightMotorForward, LOW); 106 digitalWrite(LeftMotorForward, LOW); 107 digitalWrite(RightMotorBackward, LOW); 108 digitalWrite(LeftMotorBackward, LOW); 109} 110 111void moveForward(){ 112 113 if(!goesForward){ 114 115 goesForward=true; 116 117 digitalWrite(LeftMotorForward, HIGH); 118 digitalWrite(RightMotorForward, HIGH); 119 120 digitalWrite(LeftMotorBackward, LOW); 121 digitalWrite(RightMotorBackward, LOW); 122 } 123} 124 125void moveBackward(){ 126 127 goesForward=false; 128 129 digitalWrite(LeftMotorBackward, HIGH); 130 digitalWrite(RightMotorBackward, HIGH); 131 132 digitalWrite(LeftMotorForward, LOW); 133 digitalWrite(RightMotorForward, LOW); 134 135} 136 137void turnRight(){ 138 139 digitalWrite(LeftMotorForward, HIGH); 140 digitalWrite(RightMotorBackward, HIGH); 141 142 digitalWrite(LeftMotorBackward, LOW); 143 digitalWrite(RightMotorForward, LOW); 144 145 delay(500); 146 147 digitalWrite(LeftMotorForward, HIGH); 148 digitalWrite(RightMotorForward, HIGH); 149 150 digitalWrite(LeftMotorBackward, LOW); 151 digitalWrite(RightMotorBackward, LOW); 152 153 154 155} 156 157void turnLeft(){ 158 159 digitalWrite(LeftMotorBackward, HIGH); 160 digitalWrite(RightMotorForward, HIGH); 161 162 digitalWrite(LeftMotorForward, LOW); 163 digitalWrite(RightMotorBackward, LOW); 164 165 delay(500); 166 167 digitalWrite(LeftMotorForward, HIGH); 168 digitalWrite(RightMotorForward, HIGH); 169 170 digitalWrite(LeftMotorBackward, LOW); 171 digitalWrite(RightMotorBackward, LOW); 172} 173 174
CODE
c_cpp
1#include <Servo.h> //standard library for the servo 2#include 3 <NewPing.h> //for the Ultrasonic sensor function library. 4 5//L298N 6 motor control pins 7const int LeftMotorForward = 6; 8const int LeftMotorBackward 9 = 7; 10const int RightMotorForward = 5; 11const int RightMotorBackward = 4; 12 13//sensor 14 pins 15#define trig_pin A1 //analog input 1 16#define echo_pin A2 //analog input 17 2 18 19#define maximum_distance 200 20boolean goesForward = false; 21int distance 22 = 100; 23 24NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function 25Servo 26 servo_motor; 27 28void setup(){ 29 30 pinMode(RightMotorForward, OUTPUT); 31 32 pinMode(LeftMotorForward, OUTPUT); 33 pinMode(LeftMotorBackward, OUTPUT); 34 35 pinMode(RightMotorBackward, OUTPUT); 36 37 servo_motor.attach(9); //our servo 38 pin 39 40 servo_motor.write(115); 41 delay(2000); 42 distance = readPing(); 43 44 delay(100); 45 distance = readPing(); 46 delay(100); 47 distance = readPing(); 48 49 delay(100); 50 distance = readPing(); 51 delay(100); 52} 53 54void loop(){ 55 56 57 int distanceRight = 0; 58 int distanceLeft = 0; 59 delay(50); 60 61 if 62 (distance <= 20){ 63 moveStop(); 64 delay(300); 65 moveBackward(); 66 67 delay(400); 68 moveStop(); 69 delay(300); 70 distanceRight = lookRight(); 71 72 delay(300); 73 distanceLeft = lookLeft(); 74 delay(300); 75 76 if 77 (distance >= distanceLeft){ 78 turnRight(); 79 moveStop(); 80 } 81 82 else{ 83 turnLeft(); 84 moveStop(); 85 } 86 } 87 else{ 88 89 moveForward(); 90 } 91 distance = readPing(); 92} 93 94int lookRight(){ 95 96 servo_motor.write(50); 97 delay(500); 98 int distance = readPing(); 99 100 delay(100); 101 servo_motor.write(115); 102 return distance; 103} 104 105int 106 lookLeft(){ 107 servo_motor.write(170); 108 delay(500); 109 int distance = readPing(); 110 111 delay(100); 112 servo_motor.write(115); 113 return distance; 114 delay(100); 115} 116 117int 118 readPing(){ 119 delay(70); 120 int cm = sonar.ping_cm(); 121 if (cm==0){ 122 123 cm=250; 124 } 125 return cm; 126} 127 128void moveStop(){ 129 130 digitalWrite(RightMotorForward, 131 LOW); 132 digitalWrite(LeftMotorForward, LOW); 133 digitalWrite(RightMotorBackward, 134 LOW); 135 digitalWrite(LeftMotorBackward, LOW); 136} 137 138void moveForward(){ 139 140 141 if(!goesForward){ 142 143 goesForward=true; 144 145 digitalWrite(LeftMotorForward, 146 HIGH); 147 digitalWrite(RightMotorForward, HIGH); 148 149 digitalWrite(LeftMotorBackward, 150 LOW); 151 digitalWrite(RightMotorBackward, LOW); 152 } 153} 154 155void moveBackward(){ 156 157 158 goesForward=false; 159 160 digitalWrite(LeftMotorBackward, HIGH); 161 digitalWrite(RightMotorBackward, 162 HIGH); 163 164 digitalWrite(LeftMotorForward, LOW); 165 digitalWrite(RightMotorForward, 166 LOW); 167 168} 169 170void turnRight(){ 171 172 digitalWrite(LeftMotorForward, 173 HIGH); 174 digitalWrite(RightMotorBackward, HIGH); 175 176 digitalWrite(LeftMotorBackward, 177 LOW); 178 digitalWrite(RightMotorForward, LOW); 179 180 delay(500); 181 182 183 digitalWrite(LeftMotorForward, HIGH); 184 digitalWrite(RightMotorForward, HIGH); 185 186 187 digitalWrite(LeftMotorBackward, LOW); 188 digitalWrite(RightMotorBackward, 189 LOW); 190 191 192 193} 194 195void turnLeft(){ 196 197 digitalWrite(LeftMotorBackward, 198 HIGH); 199 digitalWrite(RightMotorForward, HIGH); 200 201 digitalWrite(LeftMotorForward, 202 LOW); 203 digitalWrite(RightMotorBackward, LOW); 204 205 delay(500); 206 207 208 digitalWrite(LeftMotorForward, HIGH); 209 digitalWrite(RightMotorForward, HIGH); 210 211 212 digitalWrite(LeftMotorBackward, LOW); 213 digitalWrite(RightMotorBackward, 214 LOW); 215} 216 217
Downloadable files
Schematics
full schematic diagram
Schematics

CODE
CODE
CODE
CODE
Comments
Only logged in users can leave comments