About: I am an Electric Electronic Engineer student and freelancer . I want to share staff what i build with you.

Hey everyone. Today I will show how can you make an OBSTACLE AVOIDER ROBOT WITH ULTRASONIC SENSOR . You can easly make your OBSTACLE AVOIDER ROBOT and have fun. Let's look steps...

Step 1: PARTS

Here is a list of parts that i used to make this project:

  • Arduino UNO
  • 2*DC Motor
  • L298 Motor Driver
  • On/Off Switch
  • 10*1.2V Battery (9V battery will work)
  • HC 04 Ultrasonic Sensor
  • Drunk Wheel
  • Jumper Wires (male to male and male to female)

Step 2: SCHEMA

Step 3: CODE

The code has explanations of code functions. You will easly understand it. If you have a problem , you can contact me.


const int in1 = 9; //BACK RIGHT const int in2 = 8; //FORWARD RIGHT const int in3 = 10; //FORWARD LEFT const int in4 = 11; //BACK LEFT

const int enA = 5; //left motor speed const int enB = 6; //right motor speed //!enable pins have to connect with arduino's PWM pins! const int trig1=A1; //HC 04 ultrasonic sensor trigpin const int echo1=A2; //HC 04 ultrasonic sensor echopin

void setup(){ pinMode(in1, OUTPUT); //all motor pins are output pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);

pinMode(enA, OUTPUT); pinMode(enB, OUTPUT);

pinMode(trig1,OUTPUT); //trigpin is output because it sends sound wave pinMode(echo1,INPUT); //echopin is ınput because it receives sound wave Serial.begin(9600); }

void loop(){ long mesafe1; //we define distance long sure1; //we define time digitalWrite(trig1,LOW); //first, we start trigpin low delay(50); digitalWrite(trig1,HIGH); //second, we open trigpin and send sound made delay(50); digitalWrite(trig1,LOW); //then, we close again for other loop sure1=pulseIn(echo1,HIGH); //then, we open echopin and receive sound made and //we measure the time with pulseIn() function. mesafe1=sure1/2/29.1; //finally, we measure the distance between obstacle and the robot if(mesafe1<20){ //if distance is smaller than 20cm,we adjust robot for //turning left analogWrite(enA, 255); digitalWrite(in1,HIGH); digitalWrite(in2, LOW);

analogWrite(enB, 255); digitalWrite(in3,HIGH); digitalWrite(in4, LOW); } else{ //if distance is bigger than 20cm,we adjust tobot for //going forward analogWrite(enA, 255); digitalWrite(in1, LOW); digitalWrite(in2,HIGH);

analogWrite(enB, 255); digitalWrite(in3,HIGH); digitalWrite(in4, LOW); } }