Obstacle Detector With Arduino

by | Mar 27, 2023 | Arduino, Basic Coding, Coding

Home » Coding » Obstacle Detector With Arduino

Introduction of the Project

Building an obstacle detector with Arduino is a fantastic way to enhance your understanding of electronics, programming, and sensor technologies. This Arduino project will provide you with an opportunity to learn about the basics of ultrasonic sensors and how they can be used to detect objects in your surroundings.

In this project guide, we will show you how to build an obstacle detector using an Arduino board, ultrasonic distance sensor, and a few other components. You’ll learn how to wire the components together, write code to read the sensor data and create a warning signal to alert you when an obstacle is detected.

Whether you’re an experienced hobbyist or a complete beginner, this project is a great way to improve your skills and have some fun in the process. You can use your obstacle detector to help with a wide range of projects, from robotics and automation to security systems and more.

So, roll up your sleeves, and let’s dive in! By the end of this guide, you’ll have a fully functional obstacle detector that you can use in a variety of applications. Get ready to learn and create something amazing with Arduino!



In order to make an Obstacle Detector with Arduino, we will require an Ultrasonic distance sensor. This sensor work on the principle of sound waves and measures the distance using ultrasonic sound waves. When any obstacle is near the sensor, the piezo buzzer will produce sound.

Other components required are as follows:


Circuit Diagram

Steps To Build An Obstacle Detector With Arduino

Step 1: First thing to do is arrange all the necessary components on the Digital Board or Physical Table.

Step 2: Plug the Ultrasonic Distance Sensor into the Breadboard.


Step 3: Connect the -ve terminal of Piezo to the Ground (GND) pin of the Arduino using a black-colored wire.

Step 4: Connect the +ve terminal of Piezo to the 3-number pin of the Arduino through a resistor.

Ultrasonic Distance Sensor:

Step 5: Connect the Power terminal of the Ultrasonic Distance Sensor to the 5V pin of the Arduino at its Analog Side.

Step 6: Connect the Trigger Terminal of the Ultrasonic Distance Sensor to the 7-number pin of the Arduino.

Step 7: Connect the Echo Terminal of the Ultrasonic Distance Sensor to the 6-number pin of the Arduino.

Step 8: Connect the Ground Terminal of the Ultrasonic Distance Sensor to the GND pin of the Arduino.

Source Code

int distance = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);

digitalWrite(triggerPin, HIGH);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);

return pulseIn(echoPin, HIGH);
void setup()
pinMode(3, OUTPUT);
void loop()
distance = 0.01723 * readUltrasonicDistance(7, 6);
digitalWrite(3, 0);
if(distance < 30)
digitalWrite(3, 1);

if(distance >= 30)
digitalWrite(3, 0);

Explanation of the Code

1. Initially, we initialized a variable ‘distance’ to 0.

2. After that, we use a function ‘readUltrasonicDistance’ that takes two values from the sensor and produces the Ultrasonic distance.

3. In the second function, i.e., the setup function, we set the serial connection speed to 9600 bits per second. And then, we configure pin number 3 for Output purposes.

4. In the loop function, we calculate the distance of the obstacle from the sensor & printing the same in the serial monitor, and then we use an if statement to produce the sound according to its distance from the obstacle or, say, the range of the sensor.


Obstacle Detector With Arduino

If the obstacle is less than 30cm from the sensor, the buzzer will produce a buzzing sound.


More Arduino Projects>>>

You May Also Like To Create…