Arduino

Ultrasonic Obstacle-Avoiding Robot

Ultrasonic Obstacle-Avoiding Robot

The Ultrasonic Obstacle-Avoiding Robot is a smart robot that can move by itself and avoid hitting objects. It uses an ultrasonic sensor to measure the distance in front of it. If it detects something too close, the robot will stop, move backward, and turn to find another path. This helps the robot keep moving without bumping into anything.

The robot is controlled by an Arduino Uno, which acts like its brain. The sensor gives information to the Arduino, and the Arduino decides what the motors should do—go forward, stop, reverse, or turn. The motor driver helps the Arduino send power and direction to the motors. This setup shows how simple parts can work together to make the robot respond to its surroundings.

This project is great for beginners who want to learn about electronics, sensors, and programming. It’s fun to build, easy to test, and helps you understand how robots can “see” and “think” in a simple way. You can also improve it by adding more sensors or making it follow a specific path.

Components

// Live from circuit.rocks shop

Connection:

See also  Programming the Nano CH340
  • Motors:
    • Left Motor → Connect to M1 terminal on the AFMotor shield
    • Right Motor → Connect to M2 terminal on the AFMotor shield
  • HC-SR04:
    • VCC5V Arduino Uno
    • GNDGND Arduino Uno
    • TRIGA0 Arduino Uno
    • ECHOA1 Arduino Uno

Code:

The code controls a two-wheeled robot that moves forward by default and uses an ultrasonic sensor to detect obstacles. If the sensor measures a distance less than 5 cm, the robot stops, reverses, and tries turning left and right until the path is clear. The motors are controlled through an Adafruit Motor Shield using the AFMotor library, while the Arduino reads distance data from the sensor to decide when to stop, reverse, turn, or move forward again.

#include <AFMotor.h> // Define motors on M1 and M2
AF_DCMotor motorLeft(1); // Left motor on M1
AF_DCMotor motorRight(2); // Right motor on M2 // HC-SR04 connected to analog pins
const int trigPin = A0;
const int echoPin = A1; long duration;
int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // Set motor speed (0 to 255) motorLeft.setSpeed(150); motorRight.setSpeed(150);
} void loop() { distance = getDistance(); Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); if (distance < 5) { stopMotors(); delay(300); // Reverse a bit motorLeft.run(BACKWARD); motorRight.run(BACKWARD); delay(400); stopMotors(); delay(200); // Try turning until path is clear while (getDistance() < 5) { // Alternate left and right turns motorLeft.run(BACKWARD); motorRight.run(FORWARD); // Try turning left delay(400); stopMotors(); delay(200); if (getDistance() >= 5) break; motorLeft.run(FORWARD); motorRight.run(BACKWARD); // Try turning right delay(400); stopMotors(); delay(200); } } else { // Go forward motorLeft.run(FORWARD); motorRight.run(FORWARD); } delay(100);
} int getDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; return distance;
} void stopMotors() { motorLeft.run(RELEASE); motorRight.run(RELEASE);
}
See also  Smart Necklace For Blind

Frequently Asked Questions

What does this Ultrasonic Obstacle tutorial cover?

The Ultrasonic Obstacle-Avoiding Robot is a smart robot that can move by itself and avoid hitting objects.

Can I use an Arduino Nano or Mega instead of UNO for the Ultrasonic Obstacle build?

Yes. Nano shares the same ATmega328P and pinout. Mega has more I/O if you outgrow UNO. The code stays the same — just match the pin numbers used in the Sample Code section.

Why does my Ultrasonic Obstacle sketch fail to upload?

Usually wrong COM port, missing CH340/CH341 driver for clone boards, or another program holding the serial port. Disconnect the board, install the driver, reselect the port, retry.

// written by jomar

Jomar Zabala builds robots, line-followers, and microcontroller projects at Circuitrocks. He writes hands-on guides covering sensors, motor control, and embedded systems — the kind of bench-tested walkthroughs he wishes existed when he started with Arduino and ESP32.