Skip to main content

How to make an obstacle avoiding robot

How to make an obstacle avoiding robot

In this article we will learn how to make an obstacle avoiding Robot ourself,I have listed some steps in making it below.



Materials
1.arduino Uno
2.motor driver
3.switch
4.servo motor
5.battery connector
6.wheel
7.ultrasonic sensor
8.four DC motors.

Now the steps: 

Step 1: making the chasiss
The chassis is the frame work of any robot i made mine from plywood so if you can't get that you can use a thick cardboard or cartoon.

Step 2 : connections
(a)Gum the Arduino uno to the chasiss with either a gum or a double sided sticker
(b) connect by plugging the motor driver to the Arduino.
(c) also gum the motors to the chassis.


(d) connect the motors ,servos,sensor and the driver to the Arduino by following the circuit diagram below
B




You can upload the code below to the Arduino Uno board.
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 100
#define MAX_SPEED 80 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ); 
AF_DCMotor motor4(4, MOTOR12_1KHZ);

Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

myservo.attach(9); 
 myservo.write(115); 
 delay(2000);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL = 0;
 delay(40);
 
 if(distance<=30)
 {
 moveStop();
 delay(100);
 moveBackward();
 delay(300);
 moveStop();
 delay(200);
 distanceR = lookRight();
 delay(200);
 distanceL = lookLeft();
 delay(200);

if(distanceR>=distanceL)
 {
 turnRight();
 moveStop();
 }else
 {
 turnLeft();
 moveStop();
 }
 }else
 {
 moveForward();
 }
 distance = readPing();
}

int lookRight()
{
 myservo.write(50); 
 delay(500);
 int distance = readPing();
 delay(100);
 myservo.write(115); 
 return distance;
}

int lookLeft()
{
 myservo.write(170); 
 delay(500);
 int distance = readPing();
 delay(100);
 myservo.write(115); 
 return distance;
 delay(100);
}

int readPing() { 
 delay(70);
 int cm = sonar.ping_cm();
 if(cm==0)
 {
 cm = 250;
 }
 return cm;
}

void moveStop() {
 motor1.run(RELEASE); 
 motor2.run(RELEASE);
 motor3.run(RELEASE); 
 motor4.run(RELEASE);
 } 
 
void moveForward() {

if(!goesForward)
 {
 goesForward=true;
 motor1.run(FORWARD); 
 motor2.run(FORWARD); 
 motor3.run(FORWARD); 
 motor4.run(FORWARD); 
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
 {
 motor1.setSpeed(speedSet);
 motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
 motor3.setSpeed(speedSet);
 motor4.setSpeed(speedSet+MAX_SPEED_OFFSET);
 delay(5);
 }
 }
}

void moveBackward() {
 goesForward=false;
 motor1.run(BACKWARD); 
 motor2.run(BACKWARD); 
 motor3.run(BACKWARD); 
 motor4.run(BACKWARD); 
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
 {
 motor1.setSpeed(speedSet);
 motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
 motor3.setSpeed(speedSet);
 motor4.setSpeed(speedSet+MAX_SPEED_OFFSET);
 delay(5);
 }
}

void turnRight() {
 motor1.run(BACKWARD);
 motor2.run(BACKWARD); 
 motor3.run(FORWARD);
 motor4.run(FORWARD); 
 delay(300);
 motor1.run(BACKWARD);
 motor2.run(BACKWARD); 
 motor3.run(FORWARD);
 motor4.run(FORWARD); 
 delay(300);
 motor1.run(BACKWARD);
 motor2.run(BACKWARD); 
 motor3.run(FORWARD);
 motor4.run(FORWARD); 
 delay(300);
 motor1.run(FORWARD); 
 motor2.run(FORWARD); 
 motor3.run(FORWARD); 
 motor4.run(FORWARD); 
} 
 
void turnLeft() {
 motor1.run(FORWARD); 
 motor2.run(FORWARD); 
 motor3.run(BACKWARD);
 motor4.run(BACKWARD); 
 delay(300);
 motor1.run(FORWARD); 
 motor2.run(FORWARD); 
 motor3.run(BACKWARD);
 motor4.run(BACKWARD); 
 delay(300);
 motor1.run(FORWARD); 
 motor2.run(FORWARD); 
 motor3.run(BACKWARD);
 motor4.run(BACKWARD); 
 delay(300);
 motor1.run(FORWARD); 
 motor2.run(FORWARD);
 motor3.run(FORWARD); 
 motor4.run(FORWARD);
}

 

Comments

Popular posts from this blog

How to program an arduino with a smartphone.

H ow to program an arduino with a smartphone Before, we can only program an arduino with a laptop or desktop computer ,but most hobbyist doesn't can't afford to buy one but thanks to the new era of Technology we can now program our board with our smart phone any where we like and at any time.To program an arduino you need to download Arduino droid or arduino commander from the Google play store, I prefer arduino droid to Arduino commander because of it flexibility and ability to understand.To start coding with your phone you need a knowledge of c++ language,the Arduinodroid is both a compiler and an editor that is you can compile your skecth right after writing it. How to program with your smartphone . You will need an OTG cord to connect your Arduino to your phone for skecth uploading since the arduino cord can't directly connect to it.you can only use an android phone with version 2.0 or higher. When trying to upload your sketch the board will feed current from your phon...

How to make a powerbank with 9v battery.

Home made 9v power bank. U  In this article we will learn how to make a power bank for our phones in case of emergency,smartphone has became a very unique device in the life of men but the only problem is the power source to keep it running on.we have many diy power banks we can make like rechargeables,4000mah power bank,non rechargeables,hand driven charger etc.So let's cut our story shot and go straight to the point. Materials. 1.ic7805cv that is for regulating 9v_5v 2.old USB. 3.wires for connection 4.LED,.1k resistor,if you wish to add an indicator bulb. 5.9v battery. Circuit diagram below. good luck!!!!

Beginner guide to electrical components and description.

Electrical components are commonly found on circuit boards and their presence is ubiquitous in mordern engineering here are some lists of materials you need to be familiar at: 1.Capacitor Symbol of capacitors. It consists of two metals separated by a dielectric material,it is use for storing charges.types: Ceramic, electrolytic,film and vairable capacitors.electrolytic is grouped into aluminum and tantalumcapacitor.Applications range from storing electric charge, stabilizing current, reducing riples in voltage and use in radio for tuning. 2.Diode Diode symbol. It is use for rectifying DC from ac since it only allows current in one direction,the part with white strip is the negative pole known as the cathode while the other black area is the positive side called anode.types of diodes:Zener,LED known as light emmiting diode, photodiodes,avalanche diode. 3.Transistors Transistors. it is basically used for amplification,switching, regulating and control it is made of semiconductors and has...