Measuring distance using the ultrasonic sensor 📏
In many situations, you may need to measure how far an object is away from you, but what if that object is constantly changing its position?
Using the ultrasonic sensor to measure this distance might be an ideal solution in this situation.
In this tutorial we will learn how to use the ultrasonic sensor to measure distance and print the values on your computer!
Overview
The ultrasonic sensor is a device that can measure distance by sending out sound waves and calculating how long it takes for them to bounce back.
It can be used to measure the distance between two objects or to detect whether an object is in the way.
That is why the ultrasonic sensor is often used in robots, as it can help the robot avoid obstacles.
Let's find out more!

Getting the items
For this project you will need the following components, you can buy them from our store.
Wiring it up
To set up the ultrasonic sensor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the ultrasonic sensor and the Arduino. Once the ultrasonic sensor and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.

Connections from the Arduino to the ultrasonic sensor:
• Arduino VCC pin → ultrasonic sensor VCC pin (+ pin)
• Arduino pin 11 → ultrasonic sensor Trig pin
• Arduino pin 12 → ultrasonic sensor Echo pin
• Arduino GND pin → ultrasonic sensor GND pin (- pin)
Coding
The function of this sketch is to calculate distance using the ultrasonic sensor. It sends a wave and waits for the echo. If there's an object in front of the sensor, it will reflect the wave, and we can measure the time it takes to come back. With that information, we can calculate the distance using an equation based on the speed of sound in air.
The code is basic and does not require any libraries; it will teach you about the sensor and how it works, allowing you to work on more complicated projects.
/*
Voltaat learn (https://www.voltaat.com)
Link for full tutorial: https://bit.ly/3MEPgnu
Tutorial: Measuring distance using the ultrasonic sensor
This is an Arduino sketch that uses an ultrasonic sensor to compute the distance from an object
Connections from the Arduino to the ultrasonic sensor:
• Arduino VCC pin → ultrasonic sensor VCC pin (+ pin)
• Arduino pin 11 → ultrasonic sensor Trig pin
• Arduino pin 11 → ultrasonic sensor Echo pin
• Arduino GND pin → ultrasonic sensor GND pin (- pin)
*/
//Define pin numbers
#define trig 11
#define echo 12
//Defines variables and initialize their values
int distance =0, t =0;
//Commands inside void setup run once
void setup(){
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//Sets the trig Pin as an output
pinMode(trig,OUTPUT);
//Sets the echo Pin as an intput
pinMode(echo,INPUT);
}
//Commands inside void loop run forever
void loop(){
//Clears the trig pin
digitalWrite(trig,LOW);
//delay for 5 micro seconds
delayMicroseconds(5);
//Sets the trig Pin HIGH for 10 micro seconds
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
//Reads the echoPin, returns the sound wave travel time in microseconds
t =pulseIn(echo,HIGH);
//Calculating the distance
distance =(t /57);
//Print to serial monitor
Serial.print("Distance= ");
Serial.print(distance);
Serial.println(" Cm");
delay(1000);
}
Testing it out

Now you must have correctly wired the ultrasonic sensor to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.
You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It can allow us to send and receive different commands as well as view information directly from the Arduino.
As we see in the following image, the serial monitor displays the distance between the sensor and the object. The value is updated and printed every one second because of the delay we determined in our code.
You should also make sure you have chosen the right baud rate (9600) as specified in the code.
You can ensure that the sensor is calibrated and that the value it provides is completely correct by comparing the output with one of the measuring tools, such as a ruler. For example, the speed of sound varies slightly depending on temperature and pressure, so the accuracy of the sensor may differ from the value in the equation that we used.

Related Tutorials
In this tutorial, we will use the PIR sensor to send a message to your computer when someone enters your house.
A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.
LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.