Ultrasonic Sensors are widely used in Robo projects for Distance measurement as well as Obstacle detector.They are available as modules in the market.

There are 3 pin modules , 4 pin as well as 5pin modules . 3 pin is generally not available in Indian market.

Generally available ones are 4 pin or 5pin modules.


       image1                    ULTRA3PIN  




          HC  – SR 04                    HC – SR 05



The Ultrasonic module has two Transducers ,one for  Transmit & the other for Receive.Both are fixed on a single PCB with control circuit,so that it can be easily used in your Robo projects.

Ultrasonic distance sensor provides precise, non-contact distance measurements from about 2 cm  to 400cm.

Ultrasound is a high frequency sound of  frequency 40 KHz (we can’t hear this frequency,but animals can).

The principle of ultrasonic distance measurement is the same as that of RADAR.

Principle of Operation

From Arduino generate a  short 10uS pulse to the Trigger input to start the ranging. The Ultrasonic Module  will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high.

It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters . If nothing is detected then the module will lower its echo line anyway after about 30mS.
The module provides an echo pulse proportional to distance. If the width of the pulse is measured in uS, then dividing by 58 will give you the distance in cm, or dividing by 148 will give the distance in inches.

uS/58=cm    or   uS/148=inches.

The module can be triggered as fast as every 50ms, or 20 times each second. You should wait 50ms before the next trigger, even if the SRF05 detects a close object and the echo pulse is shorter. This is to ensure the ultrasonic "beep" has faded away and will not cause a false echo on the next ranging.

The sensor can detect objects within 3cm to 3m range.

Image 4



Specifications :

> The module includes ultrasonic transmitter, receiver and control circuit .

> Its stable performance and high ranging accuracy make it a popular module in Robotics.

> There are 5 pins out of the module, VCC, Trig, Echo, OUT  & GND.

> The  name of 4th pin “OUT “ is misguiding.As such no output is available at this pin.This is a Mode    Select pin.Generally it is left unused.

> It  performs best in 30 degrees angle .

> Open source Arduino library is readily available to use.

> It operates at 5V  DC & consumes 15mA current.Range of sensing is from 3cm up to 400cm with a resolution of 1cm.

> Trigger pulse width is 10us.


5 pin module can be used as a 4 pin or 3 pin module .

If you leave out the 4th pin  (OUT)  in a  5 Pin Module , it behaves as a 4 pin Module.

If you Ground the 4th pin then it behaves as a 3 pin PING module.

If 4th pin is connected to GND ,pin 2 is left out.

Now the  Trigger Input & Echo Output appears on single pin 3 (behaves as a 3 pin module).

So 4th pin of a 5pin module acts as a Mode Select pin.

The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the pulseIn() function. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this time can be converted to distance.

            5PIN Module being used as a  3 pin PING.



Below is the Timing diagram of a 5 pin module with 4th pin GND & 2nd pin left out

(works as a 3pin PING module)


The SRF05 will  raise the echo line only after  700uS of end of the trigger signal. You have that long to turn the trigger pin around and make it an input and to have your pulse measuring code ready. The pulseIn() command of Arduino controllers does this automatically.


Connecting Sensor Module to Arduino

Following is the connection diagram between Ultrasonic module and an Arduino UNO board.

The connection involves only 4 wires.

          PIN 2 of Arduino to TRIG of Sensor module

          PIN 3 of Arduino to ECHO of Sensor module

          5V of Arduino to Vcc of Sensor module

          GND to GND


     ULTRA_5PIN   ard_con

If you leave out pin 4 in a 5 pin module then connections are same for 4 pin as well as 5 pin module.


Image 2


Now connect the USB cable of Arduino to your PC & Upload the following Code.


//code for testing Ultrasonic Module

#define ECHOPIN 3        // Pin to receive echo pulse
#define TRIGPIN 2        // Pin to send trigger pulse

void setup()
  pinMode(ECHOPIN, INPUT);

void loop()
  // Start Ranging -Generating a trigger of 10us burst
  digitalWrite(TRIGPIN, LOW);
  digitalWrite(TRIGPIN, HIGH);
  digitalWrite(TRIGPIN, LOW);

  // Distance Calculation

  float distance = pulseIn(ECHOPIN, HIGH);
  distance= distance/58; 

/* The speed of sound is 340 m/s or 29 us per cm.The Ultrasonic burst travels out & back.So to find the distance of object we divide by 58  */

  Serial.println(" cm");



After Uploading is over the TX LED on Arduino starts blinking , indicating that it has started sending Trigger burst to the Sensor.

Open up the Serial Monitor of Arduino IDE & ensure that baud rate is 9600 ,same as that we used in code .

If you move your Hand or any object in front of the Sensor you can see the distance measurement reading on the Serial monitor.




NewPing Library for Ultrasonic Sensors

Libraries are a collection of code that makes it easy for you to connect  a sensor, display, module, etc. For example, the built-in LiquidCrystal library makes it easy to talk to character LCD displays. There are hundreds of additional libraries available on the Internet for download.

The NewPing library  adds many new features and breathes new life into these Ultrasonic distance sensors,whether it is a 3 pin, 4 or 5 pin module.



Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be a .cpp file, a .h file and  a keywords .txt file, examples folder, and other files required by the library.

To install the library, first quit the Arduino application.

You can download New Ping from


Then uncompress the ZIP file containing the library.

Drag the NewPing folder into your Libraries folder of Arduino IDE. Generally it is Under  "My Documents\Arduino\libraries"

Inside the NewPing folder are some example codes.Just copy them & paste it undee Examples folder of your Arduino IDE. 

Restart the Arduino application. Make sure the new library appears in the Sketch->Import Library menu item of the software.

That’s it! You’ve installed a library!

Now you can select & load an example code or

Download Sketch from


& you can test 4 pin or 5 pin Ultrasonic using Arduino.


// Code using NEWPING Library

#include <NewPing.h>

#define TRIGGER_PIN  12                // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN  11                   // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins .

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.

void loop() {
  delay(50);       // Wait 50ms between pings ( 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)



Watch this video  :


Testing ULTRASONIC Sensors using Arduino UNO


For availability of Ultrasonic Sensors contact :

cooltext753793315        cooltext753790696