Arduino Ultrasonic Sensor Project Obstacle detection using Arduino- In this Tutorial, you will learn how to use HC-SR04 ultrasonic sensor with Arduino for Obstacle detection and distance measurement. For the demonstration purposes I will use an Led which will be controlled depending on the distance of the object from the HC-SR04 Sensor. This LED will be controlled as per the pre-defined value. The Obstacle detection range can be adjusted in the programming.
The same programming can also be used in the Robot for detecting any Obstacle, or it can be used in a security project, or the same idea can also be used in making the touch-less door bell system. For the Step by Step explanation you can watch video tutorial given at the end of this Article.
This tutorial covers the extreme basics
- Ultrasonic sensor Pinout
- Circuit Diagram
- Ultrasonic Sensor interfacing with Arduino
- Ultrasonic Sensor Arduino Programming and finally
The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:
Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu
Nodemcu esp8266 wifi module: Best price https://amzn.to/2NAvmiB
HC-SR04 Ultrasonic Sensor: https://amzn.to/2GJrjgP
LM7805 Voltage Regulator: https://amzn.to/2IZEl5x
330 ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 npn transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF
Features of the Ultrasonic Sensor:
Power Supply: +5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Ranging Distance : 2cm – 400 cm/1″ – 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse width: 10uS
Dimension: 45mm x 20mm x 15mm
Ultrasonic Sensor HC SR04 Pinout:
As you can see in the Picture above, the HC-SR04 has a total of 4 male headers which are clearly labeled with Vcc, Trig, Echo and GND. As the HC-SR04 needs 5v so this Ultrasonic Sensor can be powered using the Arduino’s 5v.
This schematic is designed in cadsoft eagle 9.1.0 version, if you want to learn how to make a schematic and PCB then watch my tutorial, the video is given below.
Learn how to make a schematic and PCB using Eagle:
As you can see in the circuit diagram above the HC-SR04 Vcc pin is connected with the Arduino’s 5v, the trigger pin is connected with pin number 4.Tthe echo pin is connected with pin number 2, and the ground pin is connected with the Arduino’s ground. A 330 ohm resistor is connected in series with a 2.5v LED. This is a current limiting resistor. I have a very detailed tutorial on led and how to calculate the current limiting resistor value; you can watch the following tutorial.
LED and how to calculate the current limiting resistor:
The resistor is connected with pin number 13, while the cathode side of the LED is connected with the ground.
- Connect the orange wire with the Vcc.
- Connect a blue wire with the trigger pin.
- Connect a green wire with the echo pin.
- Connect a black wire with the ground pin.
After I was done with connecting the male to female type jumper wires with the HC-SR04 then I started connecting these wires with the Arduino Uno.
I simply connected the Orange wire with the Vcc.
I connected the black wire with the ground.
I connected the blue wire with pin number 4.
I connected the green wire with pin number 2. I also connected an LED with pin number 13 through a 330 resistor.
#define trigpin 4 // digital pin 4
#define echopin 2 // digital pin 2
#define buzzer 13
int duration, distance;
duration = pulseIn(echopin,HIGH);
distance = ( duration / 2) / 29.1;
if ( distance < 0 )
Serial.println("out of Range");
if ( (distance >= 0) && (distance <= 30 ))
if (distance > 30 )
After I uploaded this program into the Arduino Uno, I was able to detect objects and was also able to measure the distance.
For the practical Demonstration watch the following video Tutorial.
Watch Video Tutorial:
Other HC-SR04 Sensor Projects:
Safe distance maintaining Car to Avoid accident using Ultrasonic Sensor
Water level monitoring using Ultrasonic Sensor
IOT Water level monitoring using Ultrasonic Sensor