Translate

DIY Arduino Smart 4wd Obstacle Avoider with Dynamic Speed Control

 1. INTRODUCTION

In modern robotics, smooth movement is just as important as logic. Most basic obstacle-avoiding robots move at a fixed, jerky speed. This project upgrades the classic design by using Pulse Width Modulation (PWM). Our robot will now act intelligently: sprinting when the path is clear and slowing down cautiously as it approaches an object.

2. COMPONENTS

  • Microcontroller: Arduino Uno R3

  • Motor Driver: L298N Dual H-Bridge Module

  • Chassis: 4WD Robot Car Kit (4 DC Motors)

  • Sensor: HC-SR04 Ultrasonic Sensor

  • Actuator: SG90 Servo Motor

  • Power: 12V Li-ion Battery Pack

  • Jumpers: Male-to-Male and Male-to-Female wires

3. CIRCUIT AND CONNECTIONS

Pin Diagram Arduino Uno R3

Pin Diagram L298N

Pin Diagram Ultrasonic Sensor HC SR04

Servo Motor Pinout Diagram

Power Distribution Connections

From (Source)

To (Destination)

Purpose

Battery (+) 12V

L298N 12V Terminal

Main Power

Battery (+) 12V

Arduino VIN Pin

Power Arduino

Battery (–) GND

L298N GND Terminal

Common Ground

L298N GND

Arduino GND Pin

Common Ground

Arduino 5V

Ultrasonic & Servo VCC

Logic Power


L298N Connections

L298N Terminal

Arduino Pin

Function

ENA

Pin 10 (PWM)

Left Motors Speed

IN1

Pin 9

Left Motors Forward

IN2

Pin 8

Left Motors Backward

IN3

Pin 7

Right Motors Forward

IN4

Pin 4

Right Motors Backward

ENB

Pin 5 (PWM)

Right Motors Speed

L298N SIDE TERMINALMOTOR WIRES (PAIRING)
OUT1 & OUT2Connect both Left Front and Left Rear motor wires here.
OUT3 & OUT4Connect both Right Front and Right Rear motor wires here.
Ultrasonic Sensor Pin Connections

HC SR 04 Terminal

Arduino Pin

Trigger pin

Pin 11

Echo pin

Pin 12

VCC

To 5V

GND

To GND


Servo Motor  Pin Connections

Sero Motor Terminal

Arduino Pin

VCC 

To 5V VCC

GND 

To GND

Control 

Pin 3


4. CIRCUIT WORKING

The L298N acts as the brain's "muscle," allowing the Arduino to drive high-current motors. By connecting ENA and ENB to PWM pins (10 and 5), we can vary the voltage. The HC-SR04 sends sound waves; the time they take to return tells the Arduino the distance. The Servo allows the sensor to "scan" 180 degrees to find the best path when forward movement is blocked.

5. CODE


#include <Servo.h>

int enA = 10; int in1 = 9; int in2 = 8;
int enB = 5;  int in3 = 7; int in4 = 4;
int trig = 11; int echo = 12;
Servo myServo;

void setup() {
  pinMode(enA, OUTPUT); pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);
  myServo.attach(3);
  myServo.write(90);
}

void loop() {
  int distance = getDistance();
  if (distance > 50 || distance == 0) {
    moveForward(255); // Full Speed
  } else if (distance <= 50 && distance > 20) {
    moveForward(150); // Slowing Down
  } else {
    stopRobot();
    scanAndTurn();
  }
}

int getDistance() {
  digitalWrite(trig, LOW); delayMicroseconds(2);
  digitalWrite(trig, HIGH); delayMicroseconds(10);
  digitalWrite(trig, LOW);
  return pulseIn(echo, HIGH) * 0.034 / 2;
}

void moveForward(int s) {
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
  analogWrite(enA, s); analogWrite(enB, s);
}

void stopRobot() {
  analogWrite(enA, 0); analogWrite(enB, 0);
  delay(200);
}

void scanAndTurn() {
  myServo.write(20); delay(500);
  int dRight = getDistance();
  myServo.write(160); delay(500);
  int dLeft = getDistance();
  myServo.write(90);
  if (dLeft > dRight) {
    digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
  } else {
    digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
    digitalWrite(in3, LOW); digitalWrite(in4, HIGH);
  }
  analogWrite(enA, 200); analogWrite(enB, 200);
  delay(500);
}

6. CODE WORKING

The code uses a conditional if-else structure. It constantly checks the getDistance() function. If the distance is large, analogWrite(255) is sent to the Enable pins. As the distance drops below 50cm, it switches to analogWrite(150). If an object is too close (< 20cm), the robot stops, triggers the scanAndTurn() function to compare left vs. right distances, and chooses the clearer path.

7. TIPS

  • Common Ground: Ensure the Battery GND and Arduino GND are connected.

  • Power Supply: Use a 12V source for 4WD chassis; 9V batteries often drain too fast.

  • PWM Pins: Only pins with the ~ symbol (3, 5, 6, 9, 10, 11) support speed control.

8. USES

  • Warehouse Automation: Small-scale version of AGVs (Automated Guided Vehicles).

  • Exploration: Educational base for rovers in unknown environments.

  • Service Robots: Foundations for vacuum or delivery robots.

9. CONCLUSION

By implementing dynamic speed, we have transformed a simple hobby project into a sophisticated robotic platform. This setup reduces motor wear and makes the robot's navigation appear much more natural and "human-like."

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.