Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Motor

Image of Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo Motor

Circuit Documentation

Summary

This circuit is designed to control a pair of hobby gearmotors and a servo motor using an Arduino UNO microcontroller. It includes an HC-SR04 ultrasonic sensor for distance measurement. The Arduino UNO is powered by a 9V battery and controls the motors through an L298N DC motor driver. The servo motor is directly connected to the Arduino for position control. The ultrasonic sensor is used to detect obstacles and the system reacts by adjusting the motors and servo accordingly.

Component List

Arduino UNO

  • Microcontroller board based on the ATmega328P
  • It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.

Hobby Gearmotor with 48:1 Gearbox (x2)

  • A small DC motor with a 48:1 gearbox for increased torque.

Tower Pro SG90 Servo

  • A small and lightweight servo motor capable of angular position control.

HC-SR04 Ultrasonic Sensor

  • An ultrasonic distance sensor capable of measuring distances from 2cm to 400cm.

9V Battery

  • A standard 9V battery used to power the Arduino UNO and the motor driver.

L298N DC Motor Driver

  • A motor driver module capable of driving up to two DC motors with a maximum current of 2A per channel.

Wiring Details

Arduino UNO

  • 5V connected to HC-SR04 VCC and Tower Pro SG90 +5V
  • GND connected to HC-SR04 GND, Tower Pro SG90 GND, and 9V Battery -
  • Vin connected to 9V Battery +
  • D10 connected to Tower Pro SG90 Signal
  • D9 connected to HC-SR04 TRIG
  • D8 connected to HC-SR04 ECHO
  • D7 connected to L298N DC motor driver IN4
  • D6 connected to L298N DC motor driver IN3
  • D5 connected to L298N DC motor driver IN2
  • D4 connected to L298N DC motor driver IN1

Hobby Gearmotor with 48:1 Gearbox

  • pin 1 of one motor connected to L298N OUT3
  • pin 2 of the same motor connected to L298N OUT4
  • pin 1 of the other motor connected to L298N OUT1
  • pin 2 of the other motor connected to L298N OUT2

Tower Pro SG90 Servo

  • Signal connected to Arduino UNO D10
  • +5V connected to Arduino UNO 5V
  • GND connected to Arduino UNO GND

HC-SR04 Ultrasonic Sensor

  • VCC connected to Arduino UNO 5V
  • TRIG connected to Arduino UNO D9
  • ECHO connected to Arduino UNO D8
  • GND connected to Arduino UNO GND

9V Battery

  • + connected to Arduino UNO Vin
  • - connected to Arduino UNO GND

L298N DC Motor Driver

  • IN1 connected to Arduino UNO D4
  • IN2 connected to Arduino UNO D5
  • IN3 connected to Arduino UNO D6
  • IN4 connected to Arduino UNO D7
  • OUT1 and OUT2 connected to one Hobby Gearmotor
  • OUT3 and OUT4 connected to the other Hobby Gearmotor
  • GND and 12V connected to the power supply (not detailed in the net list)

Documented Code

#include <Servo.h> 
Servo Myservo;
#define trigPin 9           // Trig Pin Of HC-SR04
#define echoPin 8           // Echo Pin Of HC-SR04
#define MLa 4               // Left motor 1st pin
#define MLb 5               // Left motor 2nd pin
#define MRa 6               // Right motor 1st pin
#define MRb 7               // Right motor 2nd pin
long duration, distance;

void setup() {
  Serial.begin(9600);
  pinMode(MLa, OUTPUT);     // Set Motor Pins As O/P
  pinMode(MLb, OUTPUT);
  pinMode(MRa, OUTPUT);
  pinMode(MRb, OUTPUT);
  pinMode(trigPin, OUTPUT);  // Set Trig Pin As O/P To Transmit Waves
  pinMode(echoPin, INPUT);   // Set Echo Pin As I/P To Receive Reflected Waves
  Myservo.attach(10);
}

void loop() {
  Serial.begin(9600);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);       // Transmit Waves For 10us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // Receive Reflected Waves
  distance = duration / 58.2;        // Get Distance
  Serial.println(distance);
  delay(10);
  if (distance > 15)                 // Condition For Absence Of Obstacle
  {
    Myservo.write(90);
    digitalWrite(MRb, HIGH);         // Move Forward
    digitalWrite(MRa, LOW);
    digitalWrite(MLb, HIGH);                                 
    digitalWrite(MLa, LOW);                                                      
  }
  else if ((distance < 10) && (distance > 0)) // Condition For Presence Of Obstacle
  {
    digitalWrite(MRb, LOW);         // Stop                
    digitalWrite(MRa, LOW);
    digitalWrite(MLb, LOW);                                 
    digitalWrite(MLa, LOW);
    delay(100);
    
    Myservo.write(0);
    delay(500);
    Myservo.write(180);
    delay(500);
    Myservo.write(90);
    delay(500);
    
    digitalWrite(MRb, LOW);         // Move Backward             
    digitalWrite(MRa, HIGH);
    digitalWrite(MLb, LOW);                                 
    digitalWrite(MLa, HIGH);
    delay(500);
    digitalWrite(MRb, LOW);         // Stop                
    digitalWrite(MRa, LOW);
    digitalWrite(MLb, LOW);                                 
    digitalWrite(MLa, LOW);  
    delay(100);  
    digitalWrite(MRb, HIGH);        // Move Left     
    digitalWrite(MRa, LOW);   
    digitalWrite(MLa, LOW);                                 
    digitalWrite(MLb, LOW);  
    delay(500);
  }
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the servo and motor pins, and in the main loop, it checks the distance measured by the ultrasonic sensor. If no obstacle is detected within a certain range, the motors move forward. If an obstacle is detected, the motors stop, and the servo is actuated to scan the area. Afterward, the robot moves backward and then turns left before stopping and continuing the loop.