Obstacle-avoiding rover

A 2WD robot car that drives around a room and steers away from walls with an ultrasonic sensor. Wired, coded and tested in Faber's simulator before a single part was bought.

By the Faber team · Featured build · 2026-11-17

The prompt

“Build a robot that drives around and doesn't hit walls.”

How it was built

  1. Describe it. One sentence to Faber: "Build a robot that drives around and doesn't hit walls." It picked an Uno, an L298N driver, two TT motors and an HC-SR04, and explained why each one is there.
  2. Wire it. Follow the wiring table. The two PWM pins (D5, D6) set each motor's speed; the IN pins set its direction. The battery powers the motors through the L298N, and the L298N GND must be tied to the Uno GND.
  3. Upload the sketch. It drives forward at about two-thirds speed while the sensor reads 28 cm or more. When something is closer it stops, backs up for 220 ms, then turns right in short steps until it sees at least 55 cm of open space. Every decision is printed on Serial at 115200 baud.
  4. Test it in the simulator first. The same firmware ran on an emulated Uno in a 3 x 2 m arena with three obstacles: 30 virtual seconds, zero collisions, about 13 m driven, closest gap 7.5 cm.
  5. Build the real one. Mount the motors and caster on the chassis, the sensor at the front facing forward, and the Uno and driver on top. Lift the wheels off the table for the first power-up.

Wiring

FromToNote
Uno D5 (PWM)L298N ENAleft motor speed
Uno D7, D8L298N IN1, IN2left motor direction
Uno D6 (PWM)L298N ENBright motor speed
Uno D4, D2L298N IN3, IN4right motor direction
L298N OUT1/OUT2left motor
L298N OUT3/OUT4right motor
Battery +/-L298N 12V / GNDshare GND with the Uno
Uno 5V, GNDHC-SR04 VCC, GND
Uno D9HC-SR04 TRIG
HC-SR04 ECHOUno D10

The code

The core of rover.ino, as it ran in the simulator:

const int ENA = 5, IN1 = 7, IN2 = 8;   // left motor
const int ENB = 6, IN3 = 4, IN4 = 2;   // right motor
const int TRIG = 9, ECHO = 10;
const int CRUISE = 170, TURN = 150;    // PWM 0..255
const long STOP_CM = 28, CLEAR_CM = 55;

long distanceCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);
  return us == 0 ? 999 : us / 58;
}

void drive(int left, int right) {
  digitalWrite(IN1, left >= 0 ? HIGH : LOW);  digitalWrite(IN2, left >= 0 ? LOW : HIGH);  analogWrite(ENA, abs(left));
  digitalWrite(IN3, right >= 0 ? HIGH : LOW); digitalWrite(IN4, right >= 0 ? LOW : HIGH); analogWrite(ENB, abs(right));
}

void setup() {
  pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  Serial.begin(115200); Serial.println("rover ready");
  drive(0, 0); delay(200);
}

void loop() {
  if (distanceCm() >= STOP_CM) { drive(CRUISE, CRUISE); delay(30); return; }
  drive(0, 0); delay(80);
  drive(-CRUISE, -CRUISE); delay(220);       // back up
  drive(0, 0); delay(40);
  int steps = 0;
  do { drive(TURN, -TURN); delay(90); drive(0, 0); delay(40); steps++; }
  while (distanceCm() < CLEAR_CM && steps < 40);  // turn right until clear
}

What the simulator tested

  • Drives for 20 s without touching anything: 0 collisions, more than 3 m travelled, never closer than 3 cm.
  • The sensor sees the far wall at start (reads more than 50 cm within 0.5 s).
  • Deterministic: the same run gives the same path every time.

What it doesn't prove

  • Real TT motors differ by 10 to 20 % in speed, so a real rover drifts to one side; the simulated motors are identical.
  • The simulated sensor sees everything in a 30 degree cone. Soft, round or angled objects may not echo on a real HC-SR04.
  • Battery drain is not simulated.
Build something like this in Faber →
Describe it in one sentence. Faber picks the parts, wires them, writes and tests the code, and designs a case.
Faber · Latin for maker