TL;DR:
An ultrasonic sensor sends a chirp and times the echo. Distance in centimeters is the echo time in microseconds × 0.0343 ÷ 2.
The decision loop is short: if the distance is under 20 cm, stop, back up, turn, then carry on. Otherwise drive forward.
Test with a wall, a box and a thin chair leg. Soft or angled objects reflect sound badly, and that is where cars fail.
A robot that drives until it nearly hits something, then chooses to go somewhere else, feels alive for a very simple reason: it senses and then acts, over and over. This build gives you that loop with one cheap sensor and a two-motor car.
What you will build
You will mount an ultrasonic distance sensor on the front of a two-wheel car. The program measures the distance ahead many times a second. When something is closer than 20 cm, the car stops, backs up a little, turns, and tries again. When the way is clear, it drives forward.
Parts and tools
A two-wheel chassis with two DC gear motors and a caster wheel.
A microcontroller board (an Arduino-style board is used here) and a dual H-bridge motor driver.
A four-pin ultrasonic module with trigger and echo pins.
A battery pack suited to your motors and driver, with a switch.
Jumper wires, double-sided tape or a bracket for the sensor, and a small screwdriver.
How sound becomes a distance
The module emits a short burst of ultrasound, which is sound too high for people to hear. It then holds its echo pin high for as long as it takes the echo to come back. Sound travels at about 343 m/s in room-temperature air, which is 0.0343 cm per microsecond. The sound goes out and back, so you halve the path.
distance (cm) = echo time (µs) × 0.0343 / 2Worked example: the echo pin stays high for 1166 µs.
Round trip:
1166 × 0.0343 ≈ 40.0 cm.One way:
40.0 / 2 = 20.0 cm.Check backward:
20 cm × 2 = 40 cm, and40 / 0.0343 ≈ 1166 µs. It matches.
So the 20 cm trigger corresponds to an echo of about 1166 µs. The speed of sound shifts a little with temperature, but over a few meters indoors the error is too small to matter for this car.
Wiring and assembly
Mount the sensor: Fix it at the front, pointing straight ahead and level, a few centimeters above the floor. A tilted sensor hears the floor and reports obstacles that are not there.
Wire the sensor: Connect power and ground, then the trigger pin to one digital output and the echo pin to one digital input. Many modules run on 5 V, so check that your board's input pins accept that.
Wire the motors: Run the motor driver from the battery pack, join all grounds, and connect two direction pins and one speed pin per motor to your board.
Test with the wheels in the air: Lift the car on a box and confirm both wheels spin forward, then backward. Swap a motor's two wires if one is reversed.
The code: a decision loop
const int TRIG = 9;
const int ECHO = 10;
const int LIMIT_CM = 20;
void drive(int left, int right) {
// positive = forward, negative = backward, range -255 to 255
}
long readCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // give up after 30 ms
if (us == 0) return 999; // no echo: treat as clear
return us * 0.0343 / 2;
}
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
if (readCm() < LIMIT_CM) {
drive(0, 0); delay(200); // stop
drive(-120, -120); delay(400); // back up
drive(120, -120); delay(450); // turn in place
} else {
drive(150, 150); // forward
}
delay(40);
}The 30 ms timeout in pulseIn limits the longest measurable distance to 30000 × 0.0343 / 2 ≈ 514 cm, more than this module can reliably see anyway, and it stops the program from freezing when no echo returns.
Test, measure and fix
First, test the sensor alone. Print readCm() to the serial monitor and hold a book at 10, 20 and 40 cm with a ruler. If the numbers are off by more than a centimeter or two, check the wiring and the formula before touching the driving code.
Then check your stopping distance. At a speed of 0.3 m/s the car covers 0.3 × 0.04 = 0.012 m, about 1.2 cm, during the 40 ms delay, plus whatever coasting the motors do. If it still bumps the wall, raise LIMIT_CM or lower the drive speed.
Drives into a sofa or curtain: soft surfaces soak up sound. Add a bumper or a second sensor.
Misses a thin chair leg: the beam is a cone, and a thin object returns little. Test with different shapes.
Spins forever near a corner: the turn is too short. Lengthen the turn delay in steps of 100 ms.
Make it better
Mount the sensor on a small servo, look left and right when blocked, and turn toward the side with more room. Pick a random turn direction if both sides are equal so the car does not get stuck in a loop. When you want smoother driving, replace the hard turns with proportional steering, as in the PID controller entry.
Stay safe
Low voltage only: Power the car from a battery pack designed for it, never from mains power. Switch off before changing wires, keep loose wires away from the wheels, and test on a table with a lip or the car lifted until you trust the stop. Do not run the car near stairs.
Frequently asked questions
Why does the reading jump around?
Echoes can bounce off several surfaces, and the module also reads noise. Take three readings and use the middle one, or average a few.
Can two cars use ultrasonic sensors in the same room?
They can hear each other's chirps and give false readings. Test one at a time while you debug.
