robots

Obstacle avoiding robot: As simple as it can be.

Got an arduino and want to make a fully automated robot? Well Read On.
image

Making robot is a fantasy project for lots of people and obstacle avoiding robot(OAR) is definitely the simplest form of locomotive robots. There are tons of thousands of tutorial on the internet but this one is for people who want step by step detailed instructions for making an OAR.

image

You will need:

1. Arduino ( I used Uno but it really doesn’t matter).
2. Ultrasonic sensor HC SR04
3. Two motors and two wheels with clamps.
4. L293d based motor driver chip ( see pic below).
5. 9v battery with clamp.
6. One caster wheel.
7. Chassis: You can show how creative and innovative you are by designing it yourself but if you are not that type you can always buy it online. I made mine with perforated aluminum (wooden sheets is also an alternative).

8. Some experience with programming (if you don’t have any don’t worry you can download my sketch).
image

Its working is very simple. The HC SR04 sensor senses the distance and report to the arduino. Using a simple “if” condition in the code the robot can be made to stop if there is an obstacle.
image

Power the motor driver and arduino with external battery. Remember the ground of external power and arduino should be same.

Code:
#define trigPin 13 // trig from hcsr04 //to 13 on arduino

#define echoPin 12 // echo from //hcsr04 to 12 on arduino

#define motora 3
//a1 of motor driver to 3 of arduino

#define motorb 4
//a2 of motor driver to 3 of arduino

#define motorc 5
//b1 of motor driver to 3 of arduino

#define motord 6
//b2 of motor driver to 3 of arduino

long duration, distance;

void setup() {

Serial.begin (9600); // for debuging

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(motora, OUTPUT);

pinMode(motorb, OUTPUT);

pinMode(motorc, OUTPUT);

pinMode(motord, OUTPUT);
;
}

void loop()
{

range();

if (distance < 30)
{
stopcar();
delay(200);
range();
if (distance 30)
{
forward();
}
}

void range()
{

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;
}

void forward()
{
digitalWrite(motora, HIGH);
digitalWrite(motorb, LOW);
digitalWrite(motorc, HIGH);
digitalWrite(motord, LOW);

}

void reverse()
{
digitalWrite(motora, LOW);
digitalWrite(motorb, HIGH);
digitalWrite(motorc, LOW);
digitalWrite(motord, HIGH);

}

void stopcar()
{
digitalWrite(motora, LOW);
digitalWrite(motorb, LOW);
digitalWrite(motorc, LOW);
digitalWrite(motord, LOW);

}

void left()
{
digitalWrite(motora, HIGH);
digitalWrite(motorb, LOW);
digitalWrite(motorc, LOW);
digitalWrite(motord, HIGH);

}

Advertisements