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

Arduino UNO Obstacle Avoiding Car with Ultrasonic Sensor and Servo Motor

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

Circuit Documentation

Summary

This document provides a detailed overview of an Arduino-based obstacle-avoiding car circuit. The circuit includes an Arduino UNO, an L293D motor driver shield, an HC-SR04 ultrasonic sensor, two DC motors, and a servo motor. The Arduino UNO is programmed to control the motors and the ultrasonic sensor to navigate and avoid obstacles.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. L293D Motor Driver Shield

    • Description: A motor driver shield for Arduino that can control DC motors and servos.
    • Pins: 5v, GND, A0, A1, A2, A3, A4, A5, M+, M2, M1, M3, M4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, gnd, Aref, ser1, Servo2, +, -, 9v
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor used for obstacle detection.
    • Pins: VCC, TRIG, ECHO, GND
  4. DC Motor

    • Description: A simple DC motor.
    • Pins: pin 1, pin 2
  5. Servo Motor

    • Description: A servo motor used for precise control of angular position.
    • Pins: gnd, vcc, pulse

Wiring Details

Arduino UNO

L293D Motor Driver Shield

HC-SR04 Ultrasonic Sensor

DC Motor 1

DC Motor 2

Servo Motor

Code Documentation

//SCIENCE N MUSIC STUDIO//
//ARDUINO OBSTACLE AVOIDING CAR//
//  Before uploading the code you have to install the necessary library//
//AFMotor  Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
//NewPing  Library https://github.com/livetronic/Arduino-NewPing// 
//Servo Library https://github.com/arduino-libraries/Servo.git  //
// To Install the libraries go to sketch >> Include Library >> Add .ZIP File  >> Select the Downloaded ZIP files From the Above links //

#include <AFMotor.h>  
#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A0 
#define  ECHO_PIN A1 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed  of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN,  MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2,  MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo  myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet  = 0;

void setup() {

  myservo.attach(10);  
  myservo.write(115);  
  delay(2000);
  distance = readPing();
  delay(100);
  distance =  readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance  = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
  int distanceL =  0;
 delay(40);
 
 if(distance<=15)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
  }
 distance = readPing();
}

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

int lookLeft()
{
    myservo.write(170);  
    delay(500);
    int distance = read