Connections as per below

Arduino Nano + L293D Motor Driver Wiring (Obstacle Avoidance)

1️⃣ Motors (DC motors with 48:1 gearbox)

Left Motor → L293D OUT1 and OUT2

Right Motor → L293D OUT3 and OUT4

2️⃣ Motor Control Pins (matching the Arduino code)

L293D IN1 → Arduino D2 (LEFT_MOTOR_DIR)

L293D IN2 → Arduino D6 (LEFT_MOTOR_PWM)

L293D IN3 → Arduino D4 (RIGHT_MOTOR_DIR)

L293D IN4 → Arduino D5 (RIGHT_MOTOR_PWM)

3️⃣ L293D Enable Pins

EN1 (Enable for Left Motor) → Arduino 5V (or use PWM pin if speed control is needed)

EN2 (Enable for Right Motor) → Arduino 5V (or use PWM pin if speed control is needed)

4️⃣ Ultrasonic Sensor (HC-SR04)

VCC → Arduino 5V

GND → Arduino GND

TRIG → Arduino D8

ECHO → Arduino D9

5️⃣ Buzzer

+ → Arduino D13

→ Arduino GND

 Power Supply

L293D Vcc1 (logic) → Arduino 5V

L293D Vcc2 (motor power) → Battery (3–6V)

Common GND between Arduino, L293D, and battery

Arduino Code

// ==========================================================
// Obstacle Avoidance Robot (OTTO-style with wheels, no LED matrix)
// ==========================================================

// ———- Pin Configuration ———-
// Motor control pins
#define LEFT_MOTOR_PWM 6                        // PWM pin for Left Motor
#define LEFT_MOTOR_DIR 2                       // Direction pin for Left Motor
#define RIGHT_MOTOR_PWM 5                      // PWM pin for Right Motor
#define RIGHT_MOTOR_DIR 4                      // Direction pin for Right Motor

// Ultrasonic Sensor pins
#define TRIG_PIN 8                       // Trigger pin for HC-SR04
#define ECHO_PIN 9                      // Echo pin for HC-SR04

// Buzzer pin
#define BUZZER_PIN 13                  // Buzzer to indicate obstacle detection

// Distance threshold for obstacle detection (in cm)
#define OBSTACLE_DISTANCE 15

// ==========================================================
// Setup function
// ==========================================================
void setup() {
// Set motor pins as OUTPUT
pinMode(LEFT_MOTOR_PWM, OUTPUT);
pinMode(LEFT_MOTOR_DIR, OUTPUT);
pinMode(RIGHT_MOTOR_PWM, OUTPUT);
pinMode(RIGHT_MOTOR_DIR, OUTPUT);

// Buzzer
pinMode(BUZZER_PIN, OUTPUT);

// Ultrasonic Sensor
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

// Start Serial Monitor for debugging
Serial.begin(9600);
}

// ==========================================================
// Main Loop
// ==========================================================
void loop() {
int distance = getDistance();

// Print distance to Serial Monitor
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm“);

// If obstacle detected, stop and turn
if (distance > 0 && distance < OBSTACLE_DISTANCE) {
stopMotors();
tone(BUZZER_PIN, 1000, 200); // Beep to indicate obstacle
delay(200);

turnRight();                     // Turn to avoid obstacle
delay(500);

stopMotors();               // Brief stop before moving forward again
delay(200);
} else {
moveForward();            // No obstacle → keep moving forward
}
}

// ==========================================================
// Function: getDistance
// Description: Measures distance using HC-SR04 ultrasonic sensor
// ==========================================================
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH, 20000);          // Timeout 20ms
if (duration == 0) return -1;                           // No signal received

int distance = duration * 0.034 / 2;                   // Convert to cm
return distance;
}

// ==========================================================
// Motor Control Functions
// ==========================================================

// Move Forward
void moveForward() {
digitalWrite(LEFT_MOTOR_DIR, HIGH);
digitalWrite(RIGHT_MOTOR_DIR, HIGH);
analogWrite(LEFT_MOTOR_PWM, 150);                     // Adjust speed (0-255)
analogWrite(RIGHT_MOTOR_PWM, 150);                  // Adjust speed (0-255)
}

// Turn Right
void turnRight() {
digitalWrite(LEFT_MOTOR_DIR, HIGH);
digitalWrite(RIGHT_MOTOR_DIR, LOW);
analogWrite(LEFT_MOTOR_PWM, 150);
analogWrite(RIGHT_MOTOR_PWM, 150);
}

// Stop Motors
void stopMotors() {
analogWrite(LEFT_MOTOR_PWM, 0);
analogWrite(RIGHT_MOTOR_PWM, 0);
}

Video