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.

Taken time between trigger and echo received =20 milliseconds=0.00002seconds 
The time taken is half of the actual distance because the sound travelled to and fro.

Taken speed of sound =343m/s=34300cm/s




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.

int trig = 5; //trig on the module is connected to arduino's pin 5
int echo=4;  //echo on the module is connected to arduino's pin 4

int distance; // variable to store distance reading 
int duration; // variable to store duration reading

void setup()                    // run once, when the sketch starts
  Serial.begin(9600); //opens the serial port to communicate at 9600 bits per second
  pinMode(trig,OUTPUT);// trig is declared as OUTPUT
  pinMode(echo,INPUT); //echo is declared as INPUT

void loop()
 digitalWrite(trig,LOW); //put trig off
 delay(2); //delay before trig goes on
 digitalWrite(trig,HIGH); //puts trig on
 delay(20);//waits for 20 milliseconds
digitalWrite(trig,LOW); //puts trig off
//this setup creates a frequency
 duration=pulseIn(echo,HIGH);//reads the frequency and measures the time taken to send and receive a sound signal sent by trig
 distance=duration/57.8; // the duration is converted to centimeter
 Serial.print("Distance =="); //prints Distance == on a row in the serial monitor
 Serial.println(distance); //prints the values stored in distance
if (distance<=30){
Serial.println(" Obstacle close");
else if(distance>=100){
Serial.println(" Obstacle Far ");


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

#include <SoftwareSerial.h>
SoftwareSerial myserial(7, 8);     
//download"bluetooth controller" form google play 
//insert the variables(a,b,c,d) in the code in the app at the Set key tap

int motor1a = 3; // pin number controlling motor one anticlockwise
int motor1c = 5;  //pin number controlling motor one clockwise
int motor2a =6  //pin number controlling motor two anticlockwise
int motor2c =9; //pin number controlling motor two clockwise

void setup()                    // run once, when the sketch starts
Serial.begin(57600);// opens the serial port
  myserial.begin(9600); //opens the l
  pinMode(motor1a, OUTPUT);
  pinMode(motor1c, OUTPUT);
  pinMode(motor2c, OUTPUT);
  pinMode(motor2a, OUTPUT);


void loop()
  myserial.println(" ON"); // test the bluetooth's connecton
  if (myserial.available()>0) {
    char b =; //read the input
    if (b == 'a') {       //in case of 'a' motor one moves anticlockwise
    if (b == 'b') { //in case of 'b' motor one moves clockwise
    digitalWrite(motor1c,LOW); // put motor one anticlockwise off
      digitalWrite(motor1a,HIGH);//in case of 'a' motor one moves clockwise
    if (b == 'c') { //
      digitalWrite(motor2a,HIGH); //in case of 'c' motor two moves anticlockwise
      dwigitalWrite(motor1a,LOW);// put motor one clockwise off
    if (b == 'd') {  
      digitalWrite(motor2a,LOW); // put motor two anticlockwise off
      digitalWrite(motor1a,HIGH);//in case of 'd' motor two moves clockwise

    if (b == 'e') { //incase of 'e' puts all motor rotations off



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.


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.

0 0 vote
Article Rating
Would love your thoughts, please comment.x
%d bloggers like this: