# Arduino Beginner: Ultrasonic Sensor

## Ultrasonic Sensor:

Hello everyone. You are now going to learn how to use a ultrasonic with arduino to get distances measured. In the previous video, we made a blinking light, and multiple blinking light. Now we just need the following to make this project-measuring distances from an object to the sensor. With no more delay, we just enter into making of the experiment.

## Procedure:

• Take an arduino, and program it with the given code. Previous I mentioned how to download Arduino Software.
The code is here,

/*
* Using Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by SS Jayanth
* www.vigyan-ihub.in
*
*/
// defines pins numbers
const int trigPin = 3;
const int echoPin = 4;
// defines variables
long duration;
float distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2000);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
• Now, make the connections as follows,
• Trigpin of sensor to digital 3 and echopin of sensor to digital 4 of the arduino.
• Connect VCC of sensor to 5v arduino and gnd of sensor to gnd(ground) in arduino.
• Check the connections again and then open serial port in arduino software.  It is in the top left of software, in tools.
• You will clearly see the distance measured by the sensor(in integers) on the serial monitor.
• If you want accurate distance upto two decimals you just need to replace int in the code to float. Here is the code again for you.

/*
* Using Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by SS Jayanth
* www.vigyan-ihub.in
*
*/
// defines pins numbers
const int trigPin = 3;
const int echoPin = 4;
// defines variables
long duration;
float distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2000);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

watch the video for better understanding;

DIY: Make this possible with different delays, (1000) is 1 second. And also do this with different digital pins on the arduino board.

In this two(this one and (blinking light) arduino beginner videos and articles, we blindly made it work. But what is an arduino? why it is made? and more, will be posted soon, stay updated, subscribe for getting instant notifications.