Built-in project · Arduino Uno R3

Obstacle-avoiding car on an Arduino Uno

A 2WD robot car on an L298N with an HC-SR04 on the front. It drives ahead, and when something comes within 25 cm it backs up, turns and carries on. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

IntermediateRuns in your browser. Free, and no account needed.

Obstacle-avoiding car · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Obstacle-avoiding car: a 2WD chassis, an L298N and an HC-SR04.
//
//   L298N    ENA pin 5, IN1 pin 6, IN2 pin 7    left motor (ENA jumper off)
//            ENB pin 10, IN3 pin 8, IN4 pin 9   right motor (ENB jumper off)
//            12V from the 7.4 V pack, GND to the Uno's GND, regulator jumper on
//   HC-SR04  TRIG pin 12, ECHO pin 11, VCC to 5V, GND
//
// The whole robot is one loop: look, then decide. It pings every 60 ms,
// because the datasheet asks for that much quiet between pings so the last
// echo has died away. Clear ahead, it drives. Something closer than STOP_MM,
// it stops, backs off, turns right on the spot and looks again.
//
// On the canvas, drag the HC-SR04's slider to put something in front of the
// car, and watch it back away and turn on the floor drawn in the chassis.
// A reading of 0 means no echo at all, which is "nothing there", not "right
// against the sensor".

#include "mokxi_hcsr04.h"

const int ENA = 5;
const int IN1 = 6;
const int IN2 = 7;
const int ENB = 10;
const int IN3 = 8;
const int IN4 = 9;

const int TRIG = 12;
const int ECHO = 11;

const uint16_t STOP_MM = 250;
const int CRUISE = 200;

Sonar sonar(TRIG, ECHO);

// One side of the car, as a signed speed from -255 to 255. Zero brakes.
void side(int en, int a, int b, int speed) {
  digitalWrite(a, speed > 0 ? HIGH : LOW);
  digitalWrite(b, speed < 0 ? HIGH : LOW);
  if (speed < 0) speed = -speed;
  analogWrite(en, speed == 0 ? 255 : (speed > 255 ? 255 : speed));
}

void drive(int left, int right) {
  side(ENA, IN1, IN2, left);
  side(ENB, IN3, IN4, right);
}

void setup() {
  const int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
  for (int i = 0; i < 6; i++) {
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], LOW);
  }
  sonar.begin();
  Serial.begin(115200);
  Serial.println("obstacle-avoiding car: ready");
}

void loop() {
  uint16_t mm = sonar.read();
  if (mm != 0 && mm < STOP_MM) {
    Serial.print("obstacle at ");
    Serial.print(mm);
    Serial.println(" mm: back up and turn");
    drive(0, 0);
    delay(200);
    drive(-CRUISE, -CRUISE);
    delay(400);
    drive(CRUISE, -CRUISE);
    delay(450);
    drive(0, 0);
    delay(100);
  } else {
    drive(CRUISE, CRUISE);
  }
  delay(60);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

17 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND
  • Arduino Uno R3 pin 12; Ultrasonic ranger pin TRIG
  • Arduino Uno R3 pin 11; Ultrasonic ranger pin ECHO
  • Arduino Uno R3 pin 10; Motor driver module, L298N pin ENB
  • Arduino Uno R3 pin 9; Motor driver module, L298N pin IN4
  • Arduino Uno R3 pin 8; Motor driver module, L298N pin IN3
  • Arduino Uno R3 pin 7; Motor driver module, L298N pin IN2
  • Arduino Uno R3 pin 6; Motor driver module, L298N pin IN1
  • Arduino Uno R3 pin 5; Motor driver module, L298N pin ENA
  • Motor driver module, L298N pin OUT1; Robot car chassis, 2WD pin L+
  • Motor driver module, L298N pin OUT2; Robot car chassis, 2WD pin L-
  • 7.4 V: Motor driver module, L298N pin 12V
  • Ground: Motor driver module, L298N pin GND
  • Motor driver module, L298N pin 5V; Ultrasonic ranger pin VCC
  • Motor driver module, L298N pin OUT3; Robot car chassis, 2WD pin R+
  • Motor driver module, L298N pin OUT4; Robot car chassis, 2WD pin R-
  • Ground: Ultrasonic ranger pin GND

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.