Source: https://mokxi.com/learn/arduino-obstacle-avoiding-robot
Updated: 2026-10-05

Learn

# An Arduino obstacle avoiding robot car

Intermediate

Written by the Mokxi team, updated October 5, 2026

Start building free Open the editor

Or see every lesson.

- 261 parts on the bench
- 31 boards running now
- 1.00x real time, on every board

Click to open it in the editor

The Obstacle-avoiding car example on a simulated ATmega328P, with an L298N, a 2WD chassis and an HC-SR04.

The obstacle avoiding robot is one of the most popular Arduino projects, and for good reason. It is the first project where your code makes its own choices. The car drives forward, looks ahead, and turns away when something gets close.

The circuit above is a real build: an Arduino Uno, an L298N motor driver, a 2WD robot car chassis and an HC-SR04 ultrasonic sensor on the front. Press Run and the car drives across the floor drawn inside the chassis. Drag the sensor’s slider to put something in front of it, and watch it stop, back up, turn and carry on. It all runs in your browser on the sketch below.

## What you need

- An Arduino Uno
- A 2WD robot car chassis with two TT gear motors
- An L298N motor driver module
- An HC-SR04 ultrasonic sensor
- A battery pack of 7 V or more, such as a two-cell 7.4 V pack
- Jumper wires

## Wiring

Part and pin | Goes to | Note

HC-SR04 TRIG | Pin 12 | Starts a ping

HC-SR04 ECHO | Pin 11 | HIGH for the time the sound takes to come back

HC-SR04 VCC | L298N 5V | The driver’s regulator makes 5 V from the pack

HC-SR04 GND | GND | Shared ground

L298N ENA, IN1, IN2 | Pins 5, 6, 7 | Left motor; ENA jumper off

L298N ENB, IN3, IN4 | Pins 10, 8, 9 | Right motor; ENB jumper off

L298N 12V | Battery + | 7.4 V pack, regulator jumper on

L298N GND | Battery - and Uno GND | All grounds joined

OUT1, OUT2 | Left motor | Swap if the wheel runs backward

OUT3, OUT4 | Right motor | Swap if the wheel runs backward

## Look, then decide

The whole robot is one short loop. First it reads the distance. If nothing is closer than 25 cm (about 10 inches), it drives forward. If something is, it stops, backs up for 400 ms, spins right for 450 ms and looks again.

The loop waits 60 ms between pings. The HC-SR04’s datasheet asks for that much quiet time, so the last echo has died away before the next ping goes out.

The loop, from the Obstacle-avoiding car example

## Reading the distance

The HC-SR04 is a stopwatch for sound. The Uno holds TRIG HIGH for 10 microseconds. The sensor sends a short burst of ultrasound, then holds ECHO HIGH for as long as the sound takes to bounce back. At room temperature, sound covers a centimeter and back in about 58 microseconds.

The example uses a small helper, mokxi_hcsr04.h, that does the timing and returns millimeters. You can also do it yourself with pulseIn(), which works here the same way it does on a real Uno. Our ultrasonic sensor tutorial shows that version.

A reading of 0 means no echo came back at all. The sketch treats that as "nothing there", not "right against the sensor". That one check stops the car from panicking in an empty room.

## Driving the motors

The motors use the same drive() helper as our L298N tutorial. Each side takes a speed from -255 to 255. Both sides forward is straight ahead. One forward and one back is a spin on the spot.

The car cruises at 200 out of 255, not full speed. That gives it a little more time to react. The L298N also keeps about 2.5 V of the battery for itself, so the motors see less than the pack’s 7.4 V.

## Try it in the editor

Change STOP_MM from 250 to 400 and the car turns away sooner. Change the 450 ms turn to a random time between 300 and 700 ms with random(), so it does not get stuck in the same corner.

Print the distance on every loop and open the Serial Plotter. Drag the slider back and forth and watch the line cross the 250 mm stop point.

## Common mistakes

One wheel runs backward. The motors sit mirror image on the chassis. Swap that motor’s two wires at the driver.

The car spins in circles. Check that both enable pins are wired and both jumpers are off, or one side will always run flat out.

The Uno resets when the motors start. On a real car, the motors pull the battery down for a moment. A fresh pack, or a separate supply for the Uno, fixes it.

Pinging too fast. Leave at least 60 ms between pings, or an old echo can come back as a new reading.

## Questions

What parts do I need for an obstacle avoiding robot?

An Arduino Uno, a 2WD chassis with two gear motors, an L298N motor driver, an HC-SR04 ultrasonic sensor, a battery pack of 7 V or more, and jumper wires.

Where do I get the code for an obstacle avoiding robot?

The full sketch is on this page’s project page, and it opens in the editor with one click. You can change it and run it on the simulated car before you build the real one.

Can I use an IR sensor instead of an ultrasonic sensor?

Yes. An IR obstacle module such as the FC-51 gives a plain HIGH or LOW when something is close, so the check becomes one digitalRead. It has a shorter range and cannot tell you how far away the object is. The FC-51 is in the Mokxi parts bin too.

Does the simulated car hit real walls?

No. The floor inside the chassis is flat and has no walls. The HC-SR04’s slider is the obstacle, so you decide how far away it is. The car also drives perfectly straight, where a real one drifts a little.

Related

## Keep going

Arduino Ultrasonic Sensor (HC-SR04) Tutorial Arduino L298N Motor Driver: Wiring, Jumpers and Code Arduino Servo Motor with a Potentiometer Arduino Projects for Beginners, Each One Running The HC-SR04 ultrasonic sensor The 2WD robot car chassis The Arduino Uno simulator The project page, with the full sketch Intermediate

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
