#1 Avoiding Obstacle Robot

This robot avoids obstacles in its way by using the ultrasonic ranging sensor HC-SR04. This sensor allows the robot to sense any object in front of it. In combination with this, I used the L298N H-Bridge to control the wheels of the robot. When an object is detected in close proximity, the robot will stop, go in reverse, and look for a clear path with the help of the ranging sensor.

Step 1: Step 1: Get the Parts

Parts:

1x Arduino Uno Microcontroller: https://amzn.to/2OF3iXb

1x L298N H-Bridge : https://amzn.to/2w7YPVd

1x SG90 Servo : https://amzn.to/2w7YPVd

2x DC Motor Wheel : https://amzn.to/2w7YPVd

1x HC-SR04 Bracket : https://amzn.to/2w7YPVd

1x Mini Breadboard : https://amzn.to/2w7YPVd

2x 9V Battery : https://amzn.to/2w7YPVd

1x DC 9V Battery Power Cable : https://amzn.to/2w7YPVd

1x 9V Battery Clip : https://amzn.to/2w7YPVd

F to F Jumpers, M to M Jumpers : https://amzn.to/2w7YPVd

Step 2: Step 2 : Learn About Some of the Key Components

Key explanations:

L298N H-Bridge

  • Allows for speed and direction control of two DC motors at the same time.
  • Able to control DC motors that have voltages from 5 to 35 V.
  • On-board 5 V regulator which can be used as input or output. If VCC < 12 V, one can enable 5 V regulator to use as an output to power Arduino board. If VCC > 12 V, it is recommended not to use 5 V regulator since VCC could damage it.
  • There is a 2 V drop caused by the IC (integrated circuit ) so the voltage at the motor terminals is 10 V which means that full speed won't be achieved by motors.
  • Enable A and Enable B pins are used to control the speed of the motor.Input pins 1, 2, 3, and 4 control the switches of the H-Bridge. Input 1 and 2 control one motor and input 3 and 4 control the other motor.
  • Switches are turned on and off based on logic gates, for example, if input 1 is low and input 2 is high, then motor will spin forward and vice versa.

Ultrasonic Sensor HC-SR04

  • Uses sonar to determine distance to an object.
  • It has 4 pins: VCC, TRIG, ECHO, and GND.
  • Transmitter(TRIG pin) sends a signal, when signal finds object, signal is reflected and received by the receiver (ECHO pin).
  • TRIG pin needs to be on high state for 10 us in order to send out ultrasonic bursts ( 40 kHz 8 cycle sonic bursts).
  • Velocity of ultrasonic burst is 340 m/s in air.

Helpful formulas:

If there is an object at 10 cm away from the sensor, then to measure the time that the sound wave will take to travel from TRIG pin to object, we have to use the following formula: time=distance/velocity = (10 cm)/(0.034 cm/s) = 294 us.

ECHO pin will double that time since this pins measures the time to and from the object. If a wave takes 100 us to bounce back from object, then we use the following formula to calculate the distance:distance = time * (0.034 cm/s) / 2 = 100 us * (0.034 cm/s )/ 2 = 1.7 cm

Step 3: Step 3 : Connections

Fritzing Diagram:

See above.

Step 4: Step 4 : Arduino Code

Arduino Code:

Download NewPing library from link below:

New Ping Library

Move NewPing folder to libraries folder which is inside the Arduino folder which should be somewhere on your computer.

//Obstacle Avoiding Robot
//Subscribe to the youtube channel: balt94engineering #include <Servo.h> //Add servo motor library #include <NewPing.h> //Add ultrasonic sensor library

//L298N input pins // Forward = Fwd ; Backward = Bwd

const int LeftMotorFwd = 7; //input1 on L298N driver const int LeftMotorBwd = 6; //input2 on L298N driver const int RightMotorFwd = 4; //input3 on L298N driver const int RightMotorBwd = 5; //input4 on L298N driver

//sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2

#define maximum_cm_distance 250 //maximum distance is 500cm boolean goFwd = false; int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_cm_distance); //ultrasonic sensor function Servo servo1; //servo name

void setup(){

pinMode(RightMotorFwd, OUTPUT); pinMode(LeftMotorFwd, OUTPUT); pinMode(LeftMotorBwd, OUTPUT); pinMode(RightMotorBwd, OUTPUT);

servo1.attach(10); //pin servo is attached to

servo1.write(140); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); }

void loop(){

int rightDistance = 0; int leftDistance = 0; delay(50); if(distance <= 20){ moveStop(); delay(300); moveBackward(); delay(400); moveStop(); delay(300); rightDistance = lookRight(); delay(300); leftDistance = lookLeft(); delay(300); if (rightDistance >= leftDistance){ turnRight(); moveStop(); } else{ turnLeft(); moveStop(); } } else{ moveForward(); } distance = readPing(); }

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

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

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

void moveStop(){

digitalWrite(RightMotorFwd, LOW); digitalWrite(LeftMotorFwd, LOW); digitalWrite(RightMotorBwd, LOW); digitalWrite(LeftMotorBwd, LOW); }

void moveForward(){

if(!goFwd){

goFwd=true;

digitalWrite(LeftMotorFwd, HIGH); digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW); digitalWrite(RightMotorBwd, LOW); } }

void moveBackward(){

goFwd=false;

digitalWrite(LeftMotorBwd, HIGH); digitalWrite(RightMotorBwd, HIGH);

digitalWrite(LeftMotorFwd, LOW); digitalWrite(RightMotorFwd, LOW);

}

void turnRight(){

digitalWrite(LeftMotorFwd, HIGH); digitalWrite(RightMotorBwd, HIGH);

digitalWrite(LeftMotorBwd, LOW); digitalWrite(RightMotorFwd, LOW);

delay(500);

digitalWrite(LeftMotorFwd, HIGH); digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW); digitalWrite(RightMotorBwd, LOW);

}

void turnLeft(){

digitalWrite(LeftMotorBwd, HIGH); digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorFwd, LOW); digitalWrite(RightMotorBwd, LOW);

delay(500);

digitalWrite(LeftMotorFwd, HIGH); digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW); digitalWrite(RightMotorBwd, LOW); }

Step 5: Step 5 : More Content

Visit the website for more:

balt94engineering.wordpress.com

Subscribe to the youtube channel:

balt94engineering

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    Discussions

    0
    None
    tomatoskins

    3 months ago

    This is so cool! I'd love to see any pictures you have of putting your robot together!