Guide to Building 4WD Obstacle Avoiding Robotic Car using Arduino UNO and l293D Motor Driver Shield
Building a 4WD Obstacle‑Avoiding Robotic Car
with Arduino UNO & L293D Motor Driver Shield
Learn how to create a smart, four‑wheel drive robot that detects and dodges obstacles automatically. This step‑by‑step guide covers every component, wiring diagram, and line of code you need—no prior robotics experience required.
What You’ll Build
- Four independently controlled DC motors for true 4WD motion.
- Ultrasonic sensor (HC‑SR04) for real‑time distance measurement.
- Arduino UNO programmed to steer away from objects within 15 cm.
- Compact chassis with battery pack, ready for indoor exploration.
Required Components
| Item | Quantity | Notes |
|---|---|---|
| Arduino UNO R3 | 1 | Standard development board |
| L293D Motor Driver Shield | 1 | Handles up to 4 DC motors |
| DC Gear Motors (12 V) | 4 | Matched with wheels |
| HC‑SR04 Ultrasonic Sensor | 1 | Front‑facing distance measurement |
| Chassis & Wheels | 1 set | Prefer metal for durability |
| Battery Pack (6 V – 9 V) | 1 | Li‑Po or NiMH, with barrel jack |
| Jumper Wires & Screws | Assorted | Male‑to‑male, male‑to‑female |
Wiring Diagram
Step 1 – Prepare the Arduino IDE
Download the latest Arduino IDE and install the Arduino UNO board package. No extra libraries are required for the L293D shield, but we’ll use the NewPing library for precise ultrasonic readings.
// Install NewPing via Library Manager
// Sketch → Include Library → Manage Libraries… → Search “NewPing” → Install
Step 2 – Mount the L293D Shield & Wire the Sensors
- Place the L293D shield directly onto the Arduino UNO pins. Ensure all pins align.
- Secure the four DC motors to the chassis and connect each motor’s two leads to the shield terminals M1, M2, M3, and M4.
- Connect the HC‑SR04:
- VCC → 5 V on the shield
- GND → GND
- Trig → Digital Pin 7
- Echo → Digital Pin 8
- Attach the external battery to the shield’s VIN (7‑12 V) and GND terminals. Do not power the robot via the USB port while the motor supply is connected.
Step 3 – Write the Motor Control Functions
Declare motor pins using the shield’s predefined constants (A1, A2, B1, B2) and create helper functions for forward, backward, left turn, right turn, and stop.
// Motor pins (L293D shield defaults)
#define ENA 5 // PWM for left motors
#define ENB 6 // PWM for right motors
#define IN1 2 // Left motor forward
#define IN2 3 // Left motor backward
#define IN3 4 // Right motor forward
#define IN4 7 // Right motor backward
void setupMotors() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
// Move forward at given speed (0‑255)
void forward(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
// Move backward
void backward(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, speed);
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
// Turn left (right wheels forward, left wheels stop)
void turnLeft(uint8_t speed) {
analogWrite(ENA, 0);
analogWrite(ENB, speed);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
// Turn right (left wheels forward, right wheels stop)
void turnRight(uint8_t speed) {
analogWrite(ENA, speed);
analogWrite(ENB, 0);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
}
// Immediate stop
void stopCar() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
Step 4 – Add Ultrasonic Obstacle Avoidance
Using NewPing, we fetch distance readings in centimeters. When an object is detected within 15 cm, the robot stops, backs up, and makes a random turn.
#include <NewPing.h>
#define TRIG_PIN 7
#define ECHO_PIN 8
#define MAX_DISTANCE 200 // cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
const uint8_t baseSpeed = 180; // 0‑255
const unsigned long turnDelay = 500; // ms
void setup() {
Serial.begin(115200);
setupMotors();
}
void loop() {
delay(30); // small debounce
unsigned int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance == 0 || distance > 15) {
// Path is clear – go forward
forward(baseSpeed);
} else {
// Obstacle detected – react
stopCar();
delay(200);
backward(baseSpeed);
delay(400);
stopCar();
// Randomly choose left or right turn
if (random(2) == 0) {
turnLeft(baseSpeed);
} else {
turnRight(baseSpeed);
}
delay(turnDelay);
stopCar();
}
}
Complete Arduino Sketch
Copy the full code below into the Arduino IDE and upload it to your UNO.
#include <NewPing.h>
//-------- Motor Pin Definitions --------
#define ENA 5 // PWM left motors
#define ENB 6 // PWM right motors
#define IN1 2 // Left forward
#define IN2 3 // Left backward
#define IN3 4 // Right forward
#define IN4 7 // Right backward
//-------- Ultrasonic Sensor --------
#define TRIG_PIN 7
#define ECHO_PIN 8
#define MAX_DISTANCE 200 // cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
//-------- Settings --------
const uint8_t baseSpeed = 180; // motor speed (0‑255)
const unsigned long turnDelay = 500; // ms
const unsigned int safeDist = 15; // cm
//-------- Motor Functions --------
void setupMotors() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void forward(uint8_t speed){
analogWrite(ENA, speed); analogWrite(ENB, speed);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void backward(uint8_t speed){
analogWrite(ENA, speed); analogWrite(ENB, speed);
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
void turnLeft(uint8_t speed){
analogWrite(ENA, 0);
analogWrite(ENB, speed);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void turnRight(uint8_t speed){
analogWrite(ENA, speed);
analogWrite(ENB, 0);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
}
void stopCar(){
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
//-------- Arduino Core --------
void setup() {
Serial.begin(115200);
randomSeed(analogRead(A0));
setupMotors();
}
void loop() {
delay(30);
unsigned int dist = sonar.ping_cm();
Serial.print(F("Dist: ")); Serial.print(dist); Serial.println(F(" cm"));
if (dist == 0 || dist > safeDist) {
forward(baseSpeed);
} else {
stopCar(); delay(200);
backward(baseSpeed); delay(400);
stopCar(); delay(100);
if (random(2) == 0) turnLeft(baseSpeed);
else turnRight(baseSpeed);
delay(turnDelay);
stopCar();
}
}
Testing & Troubleshooting
| Issue | Possible Cause | Solution |
|---|---|---|
| Car does not move | Motor supply not connected or battery voltage too low | Provide 7‑12 V to VIN, verify polarity and secure connections. |
|
|
Comments
Post a Comment