Hello friends! Welcome back to ElectroDuino. This tutorial is base on the Interfacing Ultrasonic sensor with Arduino. After understanding the “Ultrasonic Sensor Module-HC-SR04 | How it’s Works” in the previous blog. Here we will discuss Introduction to Interfacing Ultrasonic Sensor with Arduino, how the ultrasonic sensor connects with the Arduino circuit diagram and the ultrasonic sensor Arduino code. Also, we will learn how to measure the distance using this Sensor.
In this project, we are going to make a distance measuring device using the ultrasonic sensor and Arduino, which can measure distance in the range of 2 cm to 400 cm. Using this project we can easily understand how the ultrasonic sensor works. When any object or obstacle comes in front of the sensor then the sensor measure distance between the object or obstacle and it. Then print the measuring distance on the Serial Monitor window of the Arduino IDE.
|Arduino UNO ((you can use other types of Arduino like Arduino NANO, MEGA, pro mini, etc)||1|
|HC-SR04 Ultrasonic Sensor Module||1|
|Connecting wires||As required in the circuit diagram|
Ultrasonic Sensor Pin Diagram
|For More Details: Ultrasonic Sensor Module-HC-SR04 | How it’s Works|
How to Connect Ultrasonic Sensor with Arduino
First of all, we need to connect the Vcc pin to the Arduino 5v pin and the GND pin is connected to the Arduino ground (GND) pin, to activate the sensor. Then connect the Trig pin to a digital pin of Arduino and set this pin as an output to generate ultrasonic waves from the transmitter. The Echo pin is connected to another digital pin of Arduino and sets this pin as an Input to read the output value from the Ultrasonic sensor.
Ultrasonic Sensor with Arduino Circuit Diagram
The following table represents the pin connection between the Ultrasonic sensor and Arduino.
|HC-SR04 Ultrasonic Sensor pin||Arduino pin|
|Vcc pin||+5 v pin|
|Trig||Digital pin D3|
|Echo||Digital pin D4|
How Arduino Measure Distance using Ultrasonic Sensor
At first, We need to trigger the ultrasonic Sensor for 10 microseconds to generate ultrasonic wave from the transmitter part. If any object comes in front of the sensor detection range. Then the ultrasonic wave reflects back from the object surface to the receiver part. When the receiver receives the wave then the Eco pin provides high output. Now the Arduino read the time duration between triggering and Receiving the wave by the sensor.
Then Arduino calculate the distance between the sensor and object by a simple formula
Distance= (Ultrasonic wave travel time x Speed of sound)/2
Where the speed of sound in Air is around 340 meters/second or 0.034cm/µs.
In this way, Arduino measures distance using the Ultrasonic sensor.
Ultrasonic Sensor Arduino Code
/* interfacing ultrasonic sensor with Arduino
int trigpin = 3;
int echopin = 4;
duration = pulseIn(echopin,HIGH);
distance = duration*0.034/2;
Here we will describe the code line by line for your better understanding.
it is declared your Ultrasonic sensor trig pin connected to Arduino pin “D11” and named as
define two variables, these are duration and distance
initialize Arduino digital pin 11, as an OUTPUT. That will provide input voltage to the trig pin for generating ultrasonic sound waves from Transmitter.
initialize Arduino digital pin 10, as an INPUT. That will read the output from your Sensor,
this function starts serial communication, at 9600 bits of data per second, between your board and your computer with the line.
At the starting of the loop, set the trig pin low for Clears the trig Pin for 2 microseconds.
Then, set the trig pin High for 10 microseconds and again set the trig pin Low. This is used to generate ultrasonic wave for 10 microseconds from the sensor transmitter.
The function pulseIn is read the ultrasonic wave receive by the sensor receiver and store this value in the variable duration.
this is the formula for Distance Calculation.
using this command you can print the distance value on your serial monitor window.
the delay is used to get clear output on the serial monitor.
using this project u can calculate the distance between the sensor and an object and print the distance value on the serial monitor.