Introduction: Obstacle Avoiding Robot (NT1)

This obstacle avoiding robot a.k.a. NT1 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. All of the distance and detection parameters can be changed in the Arduino code to make it do even more things.

Step 1: 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 2V 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.
  • Formulas to be used:

- 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:

t = d/v = time=distance/velocity = (10 cm)/(0.034 cm/s) = 294 us.

- ECHO pin will double that time since this pin measures the time to and from the object.

- If a wave takes 100us to bounce back from object, then we use the following formula to calculate the distance to the object:

distance = time * (0.034 cm/s) / 2 = 100 us * (0.034 cm/s )/2 = 1.7 cm

Step 2: Components:

Step 3: 3D Printed Parts

To design the 3D printed bed, I used Fusion 360 and printed out the model using my 3D printer.

Step 4: Fritzing Diagram

Step 5: Step by Step Building Gallery

  • Place and glue the universal/rolling ball underneath the bed.
  • Place the rest of components on the other side of the bed.
  • Attach the wheels to the DC motors.
  • Attach the HC-SR04 sensor to its bracket and attach it to the servo motor.

Step 6: Arduino Code

First we need to include the NewPing library, we follow the next steps:

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.

#include //Include the servo motor library
#include //This library needs to be downloaded and included so that we can use the HC-SR04 sensor //L298N (Motor driver)control pins const int LeftMotorFwd = 7; const int LeftMotorBwd = 6; const int RightMotorFwd = 4; const int RightMotorBwd = 5; //ultrasonic sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2 #define maximum_distance 200 boolean goesFwd = false; int distance = 100; NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function Servo servo_motor; //our servo name void setup(){ pinMode(RightMotorFwd, OUTPUT); pinMode(LeftMotorFwd, OUTPUT); pinMode(LeftMotorBwd, OUTPUT); pinMode(RightMotorBwd, OUTPUT); servo_motor.attach(10); //our servo pin servo_motor.write(115); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); } void loop(){ int distanceRight = 0; int distanceLeft = 0; delay(50); if (distance <= 20){ moveStop(); delay(300); moveBackward(); delay(400); moveStop(); delay(300); distanceRight = lookRight(); delay(300); distanceLeft = lookLeft(); delay(300); if (distance >= distanceLeft){ turnRight(); moveStop(); } else{ turnLeft(); moveStop(); } } else{ moveForward(); } distance = readPing(); } int lookRight(){ servo_motor.write(50); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); return distance; } int lookLeft(){ servo_motor.write(170); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); 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(!goesForward){ goesForward=true; digitalWrite(LeftMotorFwd, HIGH); digitalWrite(RightMotorFwd, HIGH); digitalWrite(LeftMotorBwd, LOW); digitalWrite(RightMotorBwd, LOW); } } void moveBackward(){ goesForward=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 7: Demo: