Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Obstacle Avoiding Robot with Ultrasonic Sensor and Servo

Circuit Documentation

Summary

This circuit is designed to control a servo and a pair of DC motors using an Arduino UNO microcontroller. It includes an HC-SR04 Ultrasonic Sensor for distance measurement, which informs the microcontroller how to actuate the motors and the servo based on the proximity of obstacles. The L298N DC motor driver is used to control the direction and speed of the motors. The Tower Pro SG90 servo is controlled directly by the Arduino UNO.

Component List

Arduino UNO

Hobby Gearmotor with 48:1 Gearbox (x2)

Tower Pro SG90 Servo

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

Wiring Details

Arduino UNO

Tower Pro SG90 Servo

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

Hobby Gearmotor with 48:1 Gearbox

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 sets up the pins for the motors and ultrasonic sensor. In the main loop, it measures the distance using the ultrasonic sensor and controls the motors and servo based on the distance to obstacles. If no obstacle is detected within 15 cm, the motors move forward. If an obstacle is detected within 10 cm, the motors stop, and the servo sweeps from 0 to 180 degrees before the motors move backward and then turn left.