Creating a 4-wheel drive obstacle avoidance robot car involves connecting and programming a microcontroller (commonly an Arduino Uno) with sensors and motor drivers. Here’s a complete wiring guide and example Arduino code to get your project up and running.
Arduino Uno
4 DC Motors (with 4WD chassis)
L298N Motor Driver (or 2x L298N if needed)
Ultrasonic Sensor (HC-SR04)
Power supply (Li-ion battery pack or 12V battery)
Jumper wires
Breadboard (optional)
can be purchased from our store
HC-SR04 Pin
Connect To arduino
VCC
5V on Arduino
GND
GND on Arduino
TRIG
Digital pin 9
ECHO
Digital pin 10
If you're using 1 L298N module and connecting two motors per channel (left pair & right pair):
L298N Pin
Connect To
IN1
Digital pin 2 (left motors)
IN2
Digital pin 3 (left motors)
IN3
Digital pin 4 (right motors)
IN4
Digital pin 5 (right motors)
ENA
Jumper cap (or PWM pin 6)
ENB
Jumper cap (or PWM pin 7)
+12V
Battery Positive
GND
Arduino GND & Battery GND
5V (optional)
Leave unconnected or jumper to Arduino 5V if needed (CAUTION)
// Motor Pins
int IN1 = 2;
int IN2 = 3;
int IN3 = 4;
int IN4 = 5;
// Ultrasonic Sensor Pins
int trigPin = 9;
int echoPin = 10;
long duration;
int distance;
void setup() {
// Motor pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Get distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 20) {
moveForward();
} else {
stopMotors();
delay(500);
moveBackward();
delay(500);
turnRight(); // Try turning to avoid
delay(500);
}
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
Hey students! 👋
Thanks for stopping by! This page is getting a fresh update and will soon be filled with amazing new science projects, DIY kits, and learning tools designed just for YOU.
We’re working hard to bring you:
🤖 Cool Arduino projects you can build yourself
💡 Fun and simple experiments to try at home or school
🛠️ DIY kits to learn electronics, coding, and sensors
🎓 Tips, tutorials, and guides to boost your STEM skills
🏆 Challenges and quizzes to test your knowledge
Whether you love robotics, want to learn coding, or just enjoy building things with your hands, there’s something exciting coming your way.
✨ Stay tuned and keep checking back—you won’t want to miss what’s next!
In the meantime, check out our YouTube channel for awesome tutorials, or visit other parts of the Kunal Science Project website for inspiration.
Keep creating. Keep learning. Keep exploring. 🔬🌟