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

Arduino UNO Controlled Smart Car with Ultrasonic Obstacle Avoidance

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Smart Car with Ultrasonic Obstacle Avoidance

Smart Car with Ultrasonic Sensor Circuit Documentation

Summary

This circuit is designed to control a smart car equipped with two motors and an ultrasonic sensor (HC-SR04). The Arduino UNO microcontroller serves as the central processing unit, interfacing with the L293D motor drivers to control the motors' movements. The ultrasonic sensor is used to detect obstacles, and the car's motion is adjusted accordingly: it moves forward until an obstacle is detected within a certain distance, at which point it stops.

Component List

Wiring Details

Arduino UNO

L293D Motor Driver (x2)

18650 Li-ion Battery x 2

7805 Voltage Regulator

Diode

Electrolytic Capacitor (1000 Farads)

Motor and Wheels (x4)

HC-SR04 Ultrasonic Sensor

Documented Code

/*
 * Smart Car with Ultrasonic Sensor
 * This Arduino sketch controls a smart car with two motors and an ultrasonic
 * sensor. The car moves forward until an obstacle is detected within a
 * certain distance, at which point it stops.
 */

// Pin definitions
const int motor1Enable = 9;
const int motor1Input1 = 10;
const int motor1Input2 = 2;
const int motor2Enable = 11;
const int motor2Input1 = 13;
const int motor2Input2 = 12;
const int trigPin = A0;
const int echoPin = A1;

void setup() {
  // Initialize motor control pins
  pinMode(motor1Enable, OUTPUT);
  pinMode(motor1Input1, OUTPUT);
  pinMode(motor1Input2, OUTPUT);
  pinMode(motor2Enable, OUTPUT);
  pinMode(motor2Input1, OUTPUT);
  pinMode(motor2Input2, OUTPUT);
  // Initialize ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Measure distance
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  // Print distance to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  // Control motors based on distance
  if (distance < 20) {
    // Stop motors
    digitalWrite(motor1Enable, LOW);
    digitalWrite(motor2Enable, LOW);
  } else {
    // Move forward
    digitalWrite(motor1Enable, HIGH);
    digitalWrite(motor1Input1, HIGH);
    digitalWrite(motor1Input2, LOW);
    digitalWrite(motor2Enable, HIGH);
    digitalWrite(motor2Input1, HIGH);
    digitalWrite(motor2Input2, LOW);
  }
  delay(100);
}

This code is responsible for controlling the smart car's movements and processing the data from the ultrasonic sensor. It initializes the motor control and sensor pins, then enters a loop where it measures the distance to any obstacle and controls the motors accordingly. If an obstacle is detected within 20 cm, the car stops; otherwise, it moves forward.