Home ARDUINO Build obstacle avoiding robot using Arduino

Build obstacle avoiding robot using Arduino

0

So in this tutorial, we will learn how to build obstacle avoiding robot Building obstacle avoiding robot is a simple fun way to start learning with  Arduino and electronics for Electrical Engineers. So in this article a lot of useful explanation, but you will be blocked if you can’t get the same parts in your region but that is not the big problem for sure because these parts can be bought now easily from AliExpress China Website. In this article, I’ll explain how to build a simple and minimal robot using the cheapest parts from Aliexpress.

We will use open source hardware such as Arduino UNO boards and ultrasonic sensors. If the robot detects the front object with a small servo motor which will place at the above side of the robot. So it will scan the left and right areas for the best possible way and the detect the avoidance from the above, right and left side.

So I just program also a notification LED, a buzzer that will activate when an object is detected near the robot, and a button that will change the robot’s function (stop/move forward).It’s very easy to build so a beginner of Electronics or a noob can be done this easily.

So, let’s get started!

Collect the Hardware

For this project you will need:

  • ROBOT CAR (Buy it)
  • Arduino UNO (Buy it)
  • Mini breadboard (Buy it)
  • L298 motor driver module (Buy it)
  • HC-SR04 ultrasonic sensor (Buy it)
  • Micro servo motor (Buy it)
  • Button
  • Red LED
  • 220 Ohm resistor
  • 9V battery holder (with or without power jack) (Buy it)

Power:

We will use an 9V battery to power on our robot because it’s small and cheap, but it’s not very powerful and will be empty after about one hour. Consider if you want to use a rechargeable battery pack (min 6V, max 7V) that will be more powerful but it will be also more expensive and bigger than the 9V battery.

Put the hardware on the ROBOT

Wired up everything as the mentioned circuit below

Button = 2 ;     Button pin to pin 2

Led    = 3 ;     Led pin (resistor) to pin 3

Buzzer = 4 ;    Buzzer pin to pin 4

MotorA1= 6; motor A positive (+) pin to pin 6 (PWM) (from L298 module!)

MotorA2= 9 ;  motor A negative (-) pin to pin 9 (PWM)

MotorB1=10 ; motor B positive (+) pin to pin 10 (PWM)

MotorB2=11 ;  motor B negative (-) pin to pin 11 (PWM)

So let’s make our circuit! The connections are pretty easy as you can see in the above image of my robot with breadboard also you can check the circuit schematic which is mention above.

Tip: Use the breadboard only for the LED, button and buzzer because this will help you to work easily and and wiring can be done without soldering, and also this will keep it simple and you can use the remain area to add more stuff in the future.

Collect Software Requirements

Download Arduino IDE

Download Libraries

Servo.h

UltraSonic.h

If you are Beginner and don’t know how to upload code, then please have a look on my previous Article

Beginner’s guide for Arduino

Upload this Code

 

/*          Arduino Obstacle Avoiding Robot

with a servo motor and an ultrasonic sensor HC-SR04

LED and buzzer

www.techincalustaad.com

*/

 

//Libraries

#include <Servo.h>

#include “Ultrasonic.h”

 

//Constants

const int button = 2;     //Button pin to pin 2

const int led    = 3;     //Led pin (throught resistor) to pin 3

const int buzzer = 4;     //Buzzer pin to pin 4

const int motorA1= 6;     //motor A positive (+) pin to pin 6 (PWM) (from L298 module!)

const int motorA2= 9;         //motor A negative (-) pin to pin 9 (PWM)

const int motorB1=10;     //motor B positive (+) pin to pin 10 (PWM)

const int motorB2=11;     //motor B negative (-) pin to pin 11 (PWM)

 

Ultrasonic ultrasonic(A4 ,A5); //Create Ultrsonic object ultrasonic(trig pin,echo pin)

Servo myservo;          //Create Servo object to control a servo

 

//Variables

int distance;         //Variable to store distance from an object

int checkRight;

int checkLeft;

int function=0;         //Variable to store function of robot: ‘1’ running or ‘0’ stoped. By default is stoped

int buttonState=0;            //Variable to store the state of the button. By default ‘0’

int pos=90;             //Variable to store the servo position. By default 90 degrees – sensor will ‘look’ forward

int flag=0;           //Useful flag to keep the button state when button is released

 

 

void setup()

{

myservo.attach(5);      //Servo pin connected to pin 5

myservo.write(pos);        // tell servo to go to position in variable ‘pos’

pinMode(button, INPUT_PULLUP);

pinMode(led,  OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(motorA1,OUTPUT);

pinMode(motorA2,OUTPUT);

pinMode(motorB1,OUTPUT);

pinMode(motorB2,OUTPUT);

 

}

 

void loop()

{

//Check button state

buttonState = digitalRead(button);

unsigned long currentMillis = millis(); //counting…

 

//Change main function (stoped/running) when button is pressed

if (buttonState == LOW) {//If button is pressed once…

delay(500);

if ( flag == 0){

function = 1;

flag=1; //change flag variable

}

else if ( flag == 1){  //If button is pressed twice

function = 0;

flag=0; //change flag variable again

}

}

 

if (function == 0){   //If button is unpressed or pressed twice then:

myservo.write(90);    //set servo 90 degress – sensor will look forward

stop();         //robot remain stoped

noTone(buzzer);     //buzzer off

digitalWrite(led, HIGH);// and led on

 

}

else if (function == 1){//If button is pressed then:

//Read distance…

distance = ultrasonic.Ranging(CM); //Tip: Use ‘CM’ for centimeters or ‘INC’ for inches

//Check for objects…

if (distance > 10){

forward(); //All clear, move forward!

noTone(buzzer);

digitalWrite(led,LOW);

}

else if (distance <=10){

stop(); //Object detected! Stop the robot and check left and right for the better way out!

tone(buzzer,500); // play a tone

digitalWrite(led,HIGH); // turn the led on

//Start scanning…

for(pos = 0; pos < 180; pos += 1){  //goes from 0 degrees to 180 degrees

myservo.write(pos);             //tell servo to go to position in variable ‘pos’

delay(10);                      //waits 10ms for the servo to reach the position

}

 

checkLeft = ultrasonic.Ranging(CM);   //Take distance from the left side

 

for(pos = 180; pos>=0; pos-=1){     //goes from 180 degrees to 0 degrees

myservo.write(pos);             //tell servo to go to position in variable ‘pos’

delay(10);                      //waits 10ms for the servo to reach the position

}

 

checkRight= ultrasonic.Ranging(CM);

 

myservo.write(90);                   // Sensor “look” forward again

 

//Finally, take the right decision, turn left or right?

if (checkLeft < checkRight){

left();

delay(400); // delay, change value if necessary to make robot turn.

}

else if (checkLeft > checkRight){

right();

delay(400); // delay, change value if necessary to make robot turn.

}

else if (checkLeft <=10 && checkRight <=10){

backward(); //The road is closed… go back and then left 😉

left();

}

}

}

 

}

 

 

void forward(){

digitalWrite(motorA1, HIGH);

digitalWrite(motorA2, LOW);

digitalWrite(motorB1, HIGH);

digitalWrite(motorB2, LOW);

}

 

void backward(){

digitalWrite(motorA1, LOW);

digitalWrite(motorA2, HIGH);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, HIGH);

}

 

void left(){

digitalWrite(motorA1, HIGH);

digitalWrite(motorA2, LOW);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, HIGH);

}

 

void right(){

digitalWrite(motorA1, LOW);

digitalWrite(motorA2, HIGH);

digitalWrite(motorB1, HIGH);

digitalWrite(motorB2, LOW);

}

 

void stop(){

digitalWrite(motorA1, LOW);

digitalWrite(motorA2, LOW);

digitalWrite(motorB1, LOW);

digitalWrite(motorB2, LOW);

}

/////////////////////////////////////////////////////////////////

Note: if your robot doesn’t move forward after upload, try to change the pins of the motors (motorA1 and motorA2 or motorB1 and motorB2). This will work fine then

Robot Build Enjoy!

Now your own Arduino Based Obstacle Avoiding Robot!

Watch the above video to get an idea of what you could build by following this article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here