Arduino Ultrasonic Sensor : Arduino 15th Lesson of Arduino Tutorial


This is the 15th lesson of Arduino tutorial. In this post we are going to talk about how to measure distance using Ultra Sonic Sensor. Let’s get started.

What is Ultrasonic Sensor?

This is a project contain ultrasonic sensor with arduino

Ultrasonic Sensor is a sensor which used for measuring distance between the sensor and the object.

You have to learn about frequency types before learning this. There are three main sound frequency types. They are listed below.

  • Low Sound-Frequency Waves – Lower than 20Hz
  • Medium Sound-Frequency Waves – 20Hz to 20,000Hz
  • High Sound-Frequency Waves – 20,000Hz and above

“Hz” means hertz. Hertz is the unit that measure frequency. Simply, sound frequency is number of shocks per second. So, this ultrasonic sensor frequency is between 25,000Hz to 50,000Hz.

This sensor is also known as OWL sensor because owl is also finding distance in this way. Own doesn’t have any eyes. So, he transmits ultrasonic waves. Human ear’s frequency limit is about 20Hz to 20000Hz. So, we can’t hear that ultrasonic sound properly. But sometimes you can hear it.


As shown as above diagram, you can understand how owl is finding his direction. First, owl transmits ultrasound waves. Then he waits until it collapses with an obstacle. If it collapses with an obstacle, reflected ultrasound wave come back to owl and owl can notice that there is some obstacle here. Not only that, he can find the distance between him and the obstacle by measuring the time difference between the time that owl transmits the ultrasound and the time that he received the ultrasound wave. That’s what ultrasonic sensor is doing. Now you have good sense about how this ultrasonic sensor is working.

Pinout of Ultrasonic sensor

Below is the pinout of Ultrasonic sensor.


This sensor has GND and VCC as other. And it has two other pins named trigger and echo. Trigger transmits the ultrasonic waves and echo receive it. Let’s work with it.

Circuit diagram is below.


In this code, we have defined echo and trigger pins as 4th and 2nd. You have learned about what is define. So, in the void setup, we have initialized trigger as an output and echo as an input. And the next line of void setup, we have set the serial monitor baud rate to 9600. Then inside the void loop, we have made the trigger pin low to make our first echo. Then after 2 microseconds, it transmits its first ultrasonic wave. Then after 10 microsecond delay, we have made the trigger pin low. Then the ultrasonic wave transmit part is over. Then we have to get the input. To do that, we have created a variable names “time”. And that variable’s type is “long”. Long is 4-byte variable and it’s same as integer type variables. Then inside the time variable, we have put “pulseIn” statement. pulseIn is a type of function to get some pulse. That input is a pulse. So, it’s measuring the time of the pulse. Then there are four other variables below. They are constant equations. That converts the time in to distance. They are very advanced and you don’t have to learn about it much more. In the last few lines, we have put the outputs to serial monitor.

Post a Comment

If you have any doubt, Let us know

Previous Post Next Post