Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Voltaat Arduino Ultimate Kit
40 min
Share

Measuring distance using the ultrasonic sensor

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.

Project Video

Overview

In this tutorial we will learn how to use the ultrasonic sensor to measure distance and print the values on your computer.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Ultrasonic Sensor (HC-SR04)
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the ultrasonic sensor and the Arduino, as shown in the image below.


 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)

Coding


/*
 Voltaat learn (https://www.voltaat.com)
 Link for full tutorial:

 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

know when your plant needs watering

Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

know when your plant needs watering

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 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.

know when your plant needs watering

Resources

No items found.