Obstacle Avoiding Robot

by mkarvonen in Circuits > Arduino

38649 Views, 502 Favorites, 0 Comments

Obstacle Avoiding Robot

Kansikuva.jpg
Obstacle avoiding robot
DSCN4804.JPG
DSCN4805.JPG

What could be more awesome than building your own robot.

This is how to do it your self.

Banana for scale in the last picture.

For mobile users. Watch the video from here!

Find the building kit from here!

Components.

DSCN4727.JPG
DSCN4731.JPG

I used my previous project's shield and ultrasound sensor in this project.

Learn how to make the Radar from HERE! The build is explained there.

The base for the whole build is an old HDD lid. It is light weight and it hold's its shape well.

The wheels are just from an old toy car.

Two servos for the wheels is needed and one servo for the radar.

And of course some kind of Arduino based board is needed. I used Intel Edison+ Arduino breakout board.

Making a Forever Rotating Servo.

DSCN4732.JPG
DSCN4733.JPG
DSCN4734.JPG
DSCN4735.JPG
DSCN4736.JPG
DSCN4737.JPG
DSCN4738.JPG
DSCN4739.JPG
DSCN4740.JPG
DSCN4741.JPG
DSCN4742.JPG
DSCN4743.JPG

Forever rotating servo means that it will turn more than the basic 0-180 degrees.

The forever rotating servo is great for this type usage. You can change the speed by entering differed value to the servo and also you can change the direction of rotation.

First find a simple test code from the internet to set up your servo.

I used a basic potentiometer on a shield and set the servo to 90 degrees. This way it is in the middle of its original rotation and it is easiest to program. Also the rotation speed of the servo is the same in both 0 and 180 degree.

Then remove the cap from the servo and pull out the gears.

In one gear there should be a small pin that stops the servo if it is turning too much. Cut that off.

Then make absolutely sure that the potentiometer is at 90 degree position and drop a small drop of superglue on the potentiometer to jam it on its place.

Then just put all the gears back and test the workings of the servo.

DSCN4745.JPG
DSCN4747.JPG
DSCN4748.JPG
DSCN4749.JPG

Attach your servos to the wheels and attach the servos to the base of your robot.

I used hot glue for basically connecting everything since it is very durable and hold's tightly on its place.

Don't worry about the front wheels at this point. I will get to them later.

Add Connectors to the Radar Shield.

DSCN4750.JPG
DSCN4752.JPG
DSCN4758.JPG
DSCN4760.JPG

If you have not checked out my RADAR project yet, i suggest to look at it now since mostly of this pit of the project is done in my previously project.

Add two simple connectors for the two servos.

A slight modification to the radar shield had to be done however. This is just for convenience of the build.

The trigger and echo pins were moved to 7 and 8 pin.

And the two servos are connected to pin 3 and 5.

The radar servo is in pin 9.

Coding and Testing

DSCN4754.JPG
DSCN4755.JPG
DSCN4756.JPG
DSCN4761.JPG
DSCN4762.JPG

Lift the project on to something so the wheels will be up, this just eases the coding process.

If you have any questions about the code, just ask and i will try to answer them.

Start the code by making all the needed definitions and global variables.

To change the distance when the robot stops, change the dangerThresh to something else. like 10.

The threshold is in centimeters.

#include
#define ECHOPIN 8 #define TRIGPIN 7

const int RForward = 0; const int RBackward = 180; const int LForward = RBackward; const int LBackward = RForward; const int RNeutral = 90; const int LNeutral = 90;

const int dangerThresh = 20; int leftDistance, rightDistance; Servo panMotor; Servo leftMotor; Servo rightMotor; long duration;

Then it's time for setup.

void setup()
{ Serial.begin(115200); rightMotor.attach(5); leftMotor.attach(3); panMotor.attach(9); panMotor.write(75); pinMode(ECHOPIN, INPUT); pinMode(TRIGPIN, OUTPUT); }

And then for the main program, loop.

void loop()
{ int distanceFwd = Distance(); if (distanceFwd>dangerThresh) { leftMotor.write(LForward); rightMotor.write(RForward); } else { leftMotor.write(LNeutral); rightMotor.write(RNeutral); panMotor.write(0); delay(500); rightDistance = Distance(); delay(500); panMotor.write(150); delay(700); leftDistance = Distance(); delay(500); panMotor.write(75); delay(100); compareDistance(); } }

Then there is two subroutines.

One to compare the measured distances. This one tells the robot to go left or right, witch has more space to move.

void compareDistance()
{ if (leftDistance>rightDistance) { leftMotor.write(LBackward); rightMotor.write(RForward); delay(700); } else if (rightDistance>leftDistance) { leftMotor.write(LForward); rightMotor.write(RBackward); delay(700); } else { leftMotor.write(LForward); rightMotor.write(RBackward); delay(1000); } }

And subroutine for the distance calculation.

long Distance()
{ digitalWrite(TRIGPIN, LOW); delayMicroseconds(2); digitalWrite(TRIGPIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGPIN, LOW);

float distance = pulseIn(ECHOPIN, HIGH); distance= distance/58.2; return(distance); }

Downloads

Adding Battery Pack.

DSCN4764.JPG
DSCN4765.JPG
DSCN4766.JPG
DSCN4767.JPG
DSCN4769.JPG
DSCN4768.JPG
DSCN4770.JPG
DSCN4771.JPG
DSCN4772.JPG

The robots usage is pretty limited if it need's a cord to run, so at this point is good to make a battery pack for it.

The pack hold's six AA (LR6) battery's witch is plenty for the project. I haven't tried how long it can run.

Once again i used hot glue to hold the pack in its place.

The Edison is not hold with anything on the board. It just fits snugly on the base board so it wont need any. This means that changing the battery's is easy.

Time for the Front Wheels.

DSCN4784.JPG
DSCN4785.JPG
DSCN4786.JPG
DSCN4788.JPG
DSCN4789.JPG

These wheels are the same kind of wheels that are on the servos.

I used a BBQ stick to make the axle and a two pits of plastic to hold it in it's place.

Once again, Hot glue everything like there is no tomorrow.

Done.

DSCN4790.JPG
DSCN4791.JPG
DSCN4792.JPG
DSCN4793.JPG
DSCN4794.JPG
DSCN4795.JPG
DSCN4796.JPG

Now it is finally done.

I could say this is pretty easy project to do. It just takes some time to collect all the needed pits and parts.

Anyway, I hope you liked the build!

If you did, Make sure to follow me to get more projects!

Thank you for reading.

PS. Remember to watch the video ;)