Obstacle-Avoiding Car
A robot car that drives on its own and steers away from walls and furniture.
What You'll Build
Here you build a real robot. Two motors drive the wheels, an ultrasonic sensor on a servo looks ahead, and the Arduino decides where to go. Put it on the floor and it will explore your room by itself, backing up and turning whenever something blocks the way.
What You Need
- ✓Arduino Uno
- ✓L298N motor driver
- ✓2 × DC gear motor
- ✓HC-SR04
- ✓SG90 servo
- ✓6×AA rechargeable battery pack
- ✓Chassis
How It Works
The Arduino cannot power motors directly — its pins are far too weak. The L298N driver sits in between: the Arduino sends small control signals, and the driver switches the big battery current to the motors. The robot repeats a simple think-loop: measure the distance ahead; if the path is clear, drive forward; if not, stop, look left and right, and turn toward whichever side has more space.
Before You Wire It
Ask a parent, teacher, or trusted adult before building, powering a circuit, using tools or batteries, or buying parts. Purchases are optional and at your own cost. If anything gets hot, smells burnt, leaks, swells, sparks, or moves unexpectedly, switch off or disconnect power without touching the damaged or hot part, then tell an adult. Read safety rules
Wiring
- 1Power the motors through the L298N, not the Arduino. Use a battery voltage suitable for your specific motors and driver — do not assume a battery labelled 12V is suitable. Motor battery + → L298N '12V' terminal, − → L298N GND.
- 2Join L298N GND to Arduino GND. Without this shared ground nothing works reliably.
- 3Power the Arduino separately by USB or a suitable USB power bank. Do not connect the L298N 5V output to the Arduino for this build. L298N boards vary, so ask an adult to check the exact board label and datasheet before setting its 5V jumper.
- 4L298N IN1–IN4 → Arduino pins 5, 6, 7, 8. ENA/ENB → PWM pins 9 and 10.
- 5Motors screw into OUT1/OUT2 and OUT3/OUT4.
- 6HC-SR04 Trig → pin 12, Echo → pin 13. Servo signal → pin 11. For the servo, use an appropriate regulated 5V supply and connect its GND to Arduino GND.
- 7Disconnect power before changing wiring. Keep fingers, hair and clothing away from moving parts.
Step-by-Step Instructions
- 1
Choose the right battery
Use 6 rechargeable AA cells (about 7.2 V) as the safer first choice, and ask an adult to confirm they suit your exact motors and driver. Do not use a small rectangular 9V PP3 battery: it cannot reliably supply the high startup current these motors need. LiPo packs can catch fire if damaged, shorted, over-discharged, or charged incorrectly, so only an adult experienced with LiPo safety may select, charge, store, and connect one.
- 2
Build the chassis
Bolt the motors and wheels on, then add the caster wheel at the back. Keep the battery low and central so the car does not tip while turning.
- 3
Wire power in the right order
Battery to the L298N first, then the shared ground, then the signal pins last. Add an on/off switch in the battery's + wire so you can stop a runaway robot instantly, and unplug the battery whenever you rewire.
- 4
Wire the driver
Double-check the shared ground. A robot with two separate power supplies and no common ground behaves randomly, and it is the single most common robot bug.
- 5
Test each motor alone
Raise the wheels off the floor, keep fingers, hair and clothing clear, and have an adult nearby. Run each motor for two seconds. Disconnect power before swapping a motor's two screw-terminal wires.
- 6
Add the scanner
Mount the servo at the front with the ultrasonic sensor on top. Centre the servo at 90° before you glue anything.
- 7
Upload the brain
Now put it together: sense, decide, drive. Start with a generous 30 cm safety distance and lower it once you trust the robot.
- 8
Tune the behaviour
Change the turn duration to make the robot more or less curious, and adjust speed so it does not crash before it can react.
Arduino Code
#include <Servo.h>
Servo look;
const int TRIG = 12, ECHO = 13;
const int IN1 = 5, IN2 = 6, IN3 = 7, IN4 = 8;
const int ENA = 9, ENB = 10;
const int SPEED = 150;
const int SAFE_CM = 30;
int distance() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
return pulseIn(ECHO, HIGH) * 0.034 / 2;
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a); digitalWrite(IN2, b);
digitalWrite(IN3, c); digitalWrite(IN4, d);
analogWrite(ENA, SPEED); analogWrite(ENB, SPEED);
}
void forward() { drive(HIGH, LOW, HIGH, LOW); }
void back() { drive(LOW, HIGH, LOW, HIGH); }
void left() { drive(LOW, HIGH, HIGH, LOW); }
void right() { drive(HIGH, LOW, LOW, HIGH); }
void stopCar() { analogWrite(ENA, 0); analogWrite(ENB, 0); }
void setup() {
for (int p = 5; p <= 10; p++) pinMode(p, OUTPUT);
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
look.attach(11);
look.write(90);
}
void loop() {
if (distance() > SAFE_CM) {
forward();
} else {
stopCar(); delay(200);
back(); delay(400);
stopCar();
look.write(160); delay(400);
int leftSpace = distance();
look.write(20); delay(600);
int rightSpace = distance();
look.write(90); delay(400);
if (leftSpace > rightSpace) left(); else right();
delay(450);
stopCar();
}
}Understanding the Code
void drive(int a, ...)A helper function. Instead of writing four digitalWrite lines everywhere, you describe the motion once and reuse it.
analogWrite(ENA, SPEED)PWM controls how fast the motors spin. 0 is stopped, 255 is full speed.
if (distance() > SAFE_CM)The whole robot brain in one line: clear ahead means keep going.
look.write(160)Turns the sensor to peek left before deciding which way to escape.
Challenges & Upgrades
Troubleshooting
😖 Arduino resets when motors start
Fix: Disconnect power. The motor supply may be dropping or creating electrical noise. Ask an adult to check the driver wiring and use a suitable separate motor supply; connect its ground to Arduino GND, but do not join the positive rails unless the exact design requires it.
😖 Car spins in circles
Fix: One motor is wired backwards — swap its two output wires.
Helpful Resources
Want extra help? Build a virtual circuit in Tinkercad or watch a video guide for this project. These are optional external resources.
Try it in Tinkercad
Open Tinkercad Circuits to recreate and test the circuit online before using real parts.
Open TinkercadWatch video guides
Browse video tutorials for this exact project and compare how each part is connected.
Find videosOutside examples may use different pins or parts. Follow this page’s wiring and safety instructions if they do not match. Ask a trusted adult before following an outside guide or buying anything; external parts, advice, and costs are your responsibility.
Up next
🦾 Servo-Controlled Robot Arm