Project showcase
Arduino Obstacle Avoidance Robot with Ultrasonic HC-SR04

Arduino Obstacle Avoidance Robot with Ultrasonic HC-SR04 © GPL3+

Arduino Nano based object avoidance robot which uses 3 HC-SR04 sensors in order to detect potential obstacles and correct its trajectory.

  • 20,919 views
  • 9 comments
  • 43 respects

Components and supplies

Apps and online services

About this project

alseTv1

alseTv1 is an Arduino Nano based object avoidance robot which uses three HC-SR04 ultrasonic sensors in order to detect potential obstacles.

alseTv1 is the first iteration of a more sophisticated project which will make use of an additional Raspberry Pi in order to process signals from some sensors which already appear in the robot pictures, such as the Raspberry Pi Camera Module or an AMG8833 Grid-Eye infrared array sensor.

About Arduino

Wikipedia:

Arduino is an open source computer hardware and software company, project, and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices and interactive objects that can sense and control objects in the physical world.

Click here to access the full article

Main features

  • Four-Wheel traction.
  • Object avoidance capabilities.
  • Taking decissions based on data reported by 3 HC-SR04 sensors.
  • Really small size (A6 paper sized - 300 grams).
  • Powered by a simple USB battery.

Bill of materials

  • 1 x Arduino Nano (compatible) ~ 3.00 USD.
  • 1 x Coretec 'Tiny 4WD' Robot Rover chassis: ~55.00 USD.
  • 3 x HC-SR04 Ultrasonic sensor: ~4.50 USD.
  • 1 x 3.000 mAh USB battery: ~7.00 USD.
  • 1 x L9110 dual channel motor driver module: ~0.60 USD.
  • 1 x Jumper cables and some other basic stuff: ~2.00 USD.

Total cost: ~72.10 USD.

Schematics

Component pictures

  • Coretec 'Tiny 4WD' Robot Rover chassis:
  • Arduino Nano (compatible):
  • HC-SR04 Ultrasonic Sensors:
  • L9110 Dual Channel motor driver:

Code

First codeArduino
const int frontEchoPin = 7;
const int frontTriggerPin = 6;
const int leftEchoPin = 11;
const int leftTriggerPin = 10;
const int rightEchoPin = 9;
const int rightTriggerPin = 8;
const int motorL1 = 2;
const int motorL2 = 3;
const int motorR1 = 4;
const int motorR2 = 5;
volatile float maxFrontDistance = 25.00;
volatile float frontDuration, frontDistanceCm, leftDuration, leftDistanceCm, rightDuration, rightDistanceCm;
volatile float maxLeftDistance, maxRightDistance = 20.00;
void setup() {
  // serial
  Serial.begin(9600);
  // ultrasonic
  pinMode(frontTriggerPin, OUTPUT);
  pinMode(frontEchoPin, INPUT);
  pinMode(leftTriggerPin, OUTPUT);
  pinMode(leftEchoPin, INPUT);
  pinMode(rightTriggerPin, OUTPUT);
  pinMode(rightEchoPin, INPUT);
  // motors
  pinMode(motorL1, OUTPUT);
  pinMode(motorL2, OUTPUT);
  pinMode(motorR1, OUTPUT);
  pinMode(motorR2, OUTPUT);
}
void loop() {
  // front distance check
  checkFrontDistance();
  if (frontDistanceCm < maxFrontDistance) {
    Serial.println("Too close");
    checkLeftDistance();
    delay(20);
    checkRightDistance();
    delay(20);
    if (leftDistanceCm < rightDistanceCm)
      moveRight();
    else if (leftDistanceCm > rightDistanceCm) {
      moveLeft();
    }
  }
  else {
    Serial.println("OK");
    moveForward();
  }
  // left distance check
  checkLeftDistance();
  if (leftDistanceCm < maxLeftDistance) {
    Serial.println("Left too close");
    delay(20);
    checkLeftDistance();
    delay(20);
    checkRightDistance();
    delay(20);
    if (leftDistanceCm > rightDistanceCm)
      moveForward();
    else if (leftDistanceCm < rightDistanceCm) {
      moveRight();
    }
  }
  // right distance check
  checkRightDistance();
  if (rightDistanceCm < maxRightDistance) {
    Serial.println("Right too close");
    delay(20);
    checkRightDistance();
    delay(20);
    checkLeftDistance();
    delay(20);
    if (rightDistanceCm > leftDistanceCm)
      moveForward();
    else if (rightDistanceCm < leftDistanceCm) {
      moveLeft();
    }
  }
}
void checkFrontDistance() {
  digitalWrite(frontTriggerPin, LOW);  //para generar un pulso limpio ponemos a LOW 4us
  delayMicroseconds(4);
  digitalWrite(frontTriggerPin, HIGH);  //generamos Trigger (disparo) de 10us
  delayMicroseconds(10);
  digitalWrite(frontTriggerPin, LOW);
  frontDuration = pulseIn(frontEchoPin, HIGH);  //medimos el tiempo entre pulsos, en microsegundos
  frontDistanceCm = frontDuration * 10 / 292 / 2;  //convertimos a distancia, en cm
  Serial.print("Distance: ");
  Serial.print(frontDistanceCm);
  Serial.println(" cm");
}
void checkLeftDistance() {
  digitalWrite(leftTriggerPin, LOW);  //para generar un pulso limpio ponemos a LOW 4us
  delayMicroseconds(4);
  digitalWrite(leftTriggerPin, HIGH);  //generamos Trigger (disparo) de 10us
  delayMicroseconds(10);
  digitalWrite(leftTriggerPin, LOW);
  leftDuration = pulseIn(leftEchoPin, HIGH);  //medimos el tiempo entre pulsos, en microsegundos
  leftDistanceCm = leftDuration * 10 / 292 / 2;  //convertimos a distancia, en cm
  Serial.print("Left distance: ");
  Serial.print(leftDistanceCm);
  Serial.println(" cm");
}
void checkRightDistance() {
  digitalWrite(rightTriggerPin, LOW);  //para generar un pulso limpio ponemos a LOW 4us
  delayMicroseconds(4);
  digitalWrite(rightTriggerPin, HIGH);  //generamos Trigger (disparo) de 10us
  delayMicroseconds(10);
  digitalWrite(rightTriggerPin, LOW);
  rightDuration = pulseIn(rightEchoPin, HIGH);  //medimos el tiempo entre pulsos, en microsegundos
  rightDistanceCm = rightDuration * 10 / 292 / 2;  //convertimos a distancia, en cm
  Serial.print("Right distance: ");
  Serial.print(rightDistanceCm);
  Serial.println(" cm");
}
void moveBackward() {
  Serial.println("Backward.");
  digitalWrite(motorL1, HIGH);
  digitalWrite(motorL2, LOW);
  digitalWrite(motorR1, HIGH);
  digitalWrite(motorR2, LOW);
}
void moveForward() {
  Serial.println("Forward.");
  digitalWrite(motorL1, LOW);
  digitalWrite(motorL2, HIGH);
  digitalWrite(motorR1, LOW);
  digitalWrite(motorR2, HIGH);
}
void moveLeft() {
  Serial.println("Left.");
  digitalWrite(motorL1, LOW);
  digitalWrite(motorL2, HIGH);
  digitalWrite(motorR1, HIGH);
  digitalWrite(motorR2, LOW);
}
void moveRight() {
  Serial.println("Right.");
  digitalWrite(motorL1, HIGH);
  digitalWrite(motorL2, LOW);
  digitalWrite(motorR1, LOW);
  digitalWrite(motorR2, HIGH);
}
GitHub alseTv1 repository

Schematics

Fritzing schematics for alseTv1
alsetv1_e7BXGwMJOB.fzz

Comments

Similar projects you might like

Robotic Car controlledover Bluetooth with Obstacle Avoidance

Project showcase by S.Ranjith Reddy

  • 9,582 views
  • 5 comments
  • 17 respects

Graphical Programming Obstacle Avoidance Robot

Project tutorial by Team KittenBot

  • 2,885 views
  • 1 comment
  • 16 respects

Project 1: 2WD Obstacle Avoiding Robot

Project showcase by HDA Robotics

  • 7,567 views
  • 6 comments
  • 17 respects

Ground-Based Obstacle Avoiding Robot

Project showcase by Xavier Tan

  • 5,317 views
  • 1 comment
  • 20 respects
Add projectSign up / Login