How to connect HC SRO4 Ultrasonic sensor to Arduino


In this tutorial, you will learn how to connect the HC SR04 Ultrasonic sensor to the arduino. The tutorial will entail displaying the values on a serial monitor and cause it to put on/off an LED on the microcontroller.


Category:  Arduino

Difficulty:  Beginner

Submitted by: Gideon

Date: 28th January, 2018


The HC-SR04 ultrasonic sensor is a great sensor for taking distance measurements. The distance can be converted to units like centimeters/inches/feet. They are using used in robotics for obstacle avoidance and also used in automation to check the height of a fluid in a tank. Aside these applications, the ultrasonic can be used innovatively in other projects. After this tutorial, you will discover how to use the HC-SR04 Ultrasonic sensor with Arduino.


The sensor has two drums. One drum serves as a sound trigger and the other as an echo receiver.The time between sound sent and time received is used to compute distance. Using the speed of sound in air(343m/s) and the formular of velocity(velocity=distance/time). The duration given by the sensor is used to get the distance travelled.

Here is a complete list of components for this circuit


Below is a fritzing image of how the components are connected in the circuit with the components mentioned above. The values generated from this setup will be displayed on the serial monitor. For this reason, the serial monitor will do communication at 9600 baud rate. Trig pin is connected to pin 5, echo is connected to pin 4 on the arduino nano board. GND and VCC are connected to GND and 5v on the microcontroller respectively. Values distance are displayed on the serial monitor.

There are two conditions declared using if and elseif. At a distance below 30 centimeters, the LED on the arduino nano turns on, else if the distance is greater than 100 centimeters, the LED goes off.

fritzing diagram of circuit

The code requires a forrmular generated from the explaination done above in the details sub-heading.

This is the set up of the whole circuitary.


I hope you enjoyed this tutorial and you now know how to use the HC-SR04 Ultrasonic sensor with Arduino. If  you have any issues, please leave a comment below and we will answer them. You can check out all the tutorials in our sensors section.


HC-05 bluetooth module is a wireless module at connect with bluetooth devices like andriod and with an interface, allows controls. This module is used in robotics, home automation and other interesting projects. There are other types of bluetooth module in the market  but HC-05 bluetooth module is easy to use and widely used. In this tutorial, we are going to control two dc motors connected to a motor driver wirelessly from an android up.


A bluetooth device has the ability to connect to a device at a time. There is always a master and slave. The slave connects to the master and receives data. The vice-versa happens with the HC-05 bluetooth module. The HC-05 bluetooth module has six pins. The pins are the ends of the module are not considered in this tutorial. The four pins in the middle are the GND(ground), VCC(+5v), TXD(transmit) and RXD(receive). The module uses AT commands for configuration. The transmit pin is used to send data to another device in the connection. The receive pin is the point where the module gets data from other devices.   These are the components that are used in this tutorial. This tutorial is linked with the motor driver tutorial

The module uses serial for communication. The arduino has a serial pins at pins 0 and 1. Instead of using these pins which usually prevents uploading during code editting, you use software serial. This library allows you to have a new set of serial pins using the normal GPIO pins. In this tutorial, we are using pin 7 as RXD and pin 8 as TXD. The motors  are connected to four digital and PWM pins( pin 3,5,6 and 9). In this setup an andriod app call bluetooth controller is used to send characters to the HC-05 bluetooth module, the arduino receives it and executes the appropriate action. This is a fritzing picture of the set up   This is the setup of the tutorial This is the arduino code that will run the setup


Thank you for going through this tutorial and we are highly excited because we have been able to help you breakthrough in this area. Visit our website to buy all the components that you need and we wish you a steady study.


Leave a Comment

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Adu-Ampomah Kwaku Effa Recent comment authors
newest oldest most voted
Notify of
Adu-Ampomah Kwaku Effa
Adu-Ampomah Kwaku Effa

Great piece. But I guess you have to explain further on the calculation part. Someone might get lost if he is not into this.

You should also talk about the fact that the pulse at the trigpin has a double travel time, therefore the value read at (pulseIn) can be decided by 2 to get the actual time taken to cover the obstacle distance.

And also that time recorded is in microseconds, therefore it is advisable to convert it also into seconds when using speed of sound in meters per *second*.

Any way this is a great piece, well-done.💪

This is the tutorials website for Invent Electronics. We are a top online retailer of electronics components, microcontrollers and sensors in Ghana. Besides providing customers with good quality electronic components we want to help them build great projects with the components they buy for us so we’ve put together a ton electronics resources, projects and tutorials on this site just for you!

Looking for electronic components to buy in Ghana?

Get all your sensors, electronic components, Arduinos, Raspberry Pi and other parts for your project at great prices from our online store.

Join Our Community

We’re more than just an electronics store. We are also a community of makers, engineers, enthusiasts and more. Join our community today, meet and learn from other people like you.

%d bloggers like this: