Arduino Interfacing with Ultrasonic Sensor

Arduino Interfacing with Ultrasonic Sensor

Summary

Are you wanting to connect an ultrasonic sensor to an Arduino Uno? Look no further! In this educational essay, we'll look at how ultrasonic sensors function and what their essential features are.

We will also give you with a step-by-step tutorial for connecting your ultrasonic sensor to your Arduino Uno, as well as the necessary code.

Whether you're an experienced Arduino user or just starting out, this article contains everything you need to get started with ultrasonic sensors.

What is Ultrasonic Sensor

An ultrasonic sensor uses ultrasonic sound waves to detect the distance to an object.

An ultrasonic sensor uses a transducer to emit and receive ultrasonic pulses, which provide information about an object's proximity.

High-frequency sound waves reflect from boundaries to produce distinct echo patterns.

Take a look at our straightforward blog on connecting ultrasonic sensor with Arduino. Whether you're new to this or already have some experience, this guide will make things clear for you to understand ultrasonic sensor interfacing with Arduino.

What is Ultrasonic Sensor

Also, read our blog on Ultrasonic Sensor Working Principle detailing the principles and workings of these sensors, their applications, and how are ultrasonic sensors used.

How does an Ultrasonic Distance Sensor work?

The ultrasonic sensor (or transducer) works on the same principles as a radar system. An ultrasonic sensor can convert electrical energy into acoustic waves and vice versa.

The acoustic wave signal is an ultrasonic wave traveling at a frequency above 18kHz. The HC-SR04 ultrasonic sensor generates ultrasonic waves at 40kHz frequency, which is much higher than the audible range of human hearing.

The generated ultrasonic waves travel in the air to the target and reflect back to the ultrasonic sensor.

Typically, a microcontroller like Arduino is used for communication with an ultrasonic sensor. To measure the distance to an object, the microcontroller sends a trigger signal to the ultrasonic sensor.

The duty cycle of this trigger signal is 10µS for the HC-SR04 ultrasonic sensor. When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave bursts and initiates a time counter. The transmitted sound wave travels in the air and reaches the target object.

The sound waves are reflected back to the ultrasonic sensor’s receiver part. As soon as the reflected (echo) signal is received, the timer stops. The output of the ultrasonic sensor is a high pulse with the same duration as the time difference between transmitted ultrasonic bursts and the received echo signal.

 

Read our blog smart dustbin project using Arduino detailing Arduino code for making a smart dustbin, the required components, and the circuit diagram for making an automatic dustbin project.

read more : Arduino Sensor types and Applications

 

How does an Ultrasonic Distance Sensor work

Hardware required:

  1. Arduino - 1 (You can use any Arduino board for this project)
  2. HC-SR04 Ultrasonic sensor -  1
  3. Jumper cables - As required

 

Connection Diagram to interfacing ultrasonic sensor with Arduino

Below are the connection diagram and the code for interfacing the Ultrasonic sensor with Arduino and measuring the distance.

 

 

circuit diagram for  interface the Ultrasonic sensor with Arduino UNO

Connections:

Arduino side  Ultrasonic sensor side
5V pin VCC pin
GND pin GND pin
GPIO 6 ECHO pin
GPIO 7 TRIG pin

 

Make all the connections as per the diagram above to interfacing ultrasonic sensor with Arduino.

 

read more : Top 10 Arduino Projects to Get Started With

 

Code


#define echoPin 6

#define trigPin 7

 

long duration;

int distance;

 

void setup()

{

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  Serial.begin(9600);      

}

void loop()

{

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");

}

The above code is used to measure distance using Ultrasoinc sensor and Arduino. This code prints the distance on the serial monitor.

Steps to Interface the Ultrasonic Sensor with Arduino UNO

Step 1: Make connections as per the circuit diagram given above.

Step 2: Open Arduino IDE and type the above code.

Step 3: Using a USB cable, connect your Arduino board to the computer.

Step 4: Select the correct port and the board you are using.

For selecting a Board: Go to Tools >> Boards >> Arduino AVR Boards >> Arduino UNO

For selecting Port: Go to Tools >> Ports >> port to which the Arduino is connected. You can make sure by checking in the device manager.

read more : Arduino Uno Pin Diagram: A Complete Guide

Step 5: Once, the board and the Port are selected, click on the upload icon. Once the code gets uploaded successfully, you can see the distance measured by the ultrasonic sensor on the serial monitor.

 

steps to interface ultrasonic sensore with arduino

Code explanation

#define echoPin

#define trigPin

long duration;

int distance;

First, we used 2 preprocessor components for assigning the pins of Arduino to connect the Ultrasonic sensor.

GPIO pin 6 is assigned to read the ECHO signal from the ultrasonic sensor and the GPIO 7 pin is assigned to give a triggering signal to the ultrasonic sensor.

And we have made 2 variables called “duration” and the “distance” of long and int datatype respectively, which hold the duration of the pulse and the distance calculated.

void setup()

{

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(9600);

}

Next, we have a “void setup()” function, this function executes only once in our program.

In this function, we are configuring the TRIG and ECHO pin as OUTPUT and INPUT respectively using a “pinMode();” function.

void loop()

{

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2;

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

}

Next, we have the “void loop()” function. This function executes infinite times, which means executes forever unless purposefully interrupted.

In this function we make the TRIG pin HIGH for 10 microseconds and LOW for 2 microseconds by using the function “digitalWrite();” function and the “delayMicroseconds();” function.

The duration is measured by using a “pulseIn();” function which measures the duration of the ECHO pulse. Next, the distance is measured using the below equation and stored in a “distance’ variable.

distance = duration * 0.034 / 2;

Then the distance measured in centimeters is printed on the serial monitor. By using a “Serial.print();” function.

 

read more : IR Sensor Interfacing with Arduino

Conclusion

This blog post shows how to connect an ultrasonic sensor to an Arduino and use it to identify obstacles and compute distance.

These sensors employ inaudible sound waves to accurately measure distances and provide data for a range of applications.

Ultrasonic sensors are gaining popularity among professionals and amateurs because of their low cost, precision, and durability.

By following the directions in this article, you may quickly connect an Arduino Uno to an ultrasonic sensor using the provided code and begin building your own projects.

So why do you wait? Take immediate advantage of ultrasonic sensors to maximize the potential of your Arduino creations!

 

If you appreciate our work don't forget to share this post and leave your opinion in the comment box.

 

Please do check out other blog posts about Interfacing ACS712 with Arduino , LED Interfacing with Arduino , Interfacing GSM Module with Arduino , Interfacing MAX30100 Pulse Oximeter with Arduino , IR Sensor Interfacing with Arduino , How to connect ZMPT101B to Arduino and  How to use Buzzer with Arduino.

 

Make sure you check out our wide range of products and collections (we offer some exciting deals!)

You may also like to read

  • What is a Bluetooth beacon?

    What is a Bluetooth beacon?

    - Robocraze -

    Discovering the power of Bluetooth beacons unveils a world of possibilities in the realm of connectivity. In our latest blog,...

    What is a Bluetooth beacon?

    - Robocraze -

    Discovering the power of Bluetooth beacons unveils a world of...

  • What is a t nut

    What is a t nut

    - Robocraze -

    Ever wondered what exactly a T-nut is and how it can revolutionize your projects? Our latest blog delves deep into...

    What is a t nut

    - Robocraze -

    Ever wondered what exactly a T-nut is and how it...

  • What is outdoor positioning system

    What is outdoor positioning system

    - Robocraze -

    Discover the magic behind Outdoor Positioning Systems (OPS) in our latest blog. Delve into the basics with an insightful Introduction,...

    What is outdoor positioning system

    - Robocraze -

    Discover the magic behind Outdoor Positioning Systems (OPS) in our...

  • What is data visualization in IoT

    What is data visualization in IoT

    - Robocraze -

    Dive into the dynamic realm of IoT with our latest blog! We kickstart with an insightful Introduction, delving into the...

    What is data visualization in IoT

    - Robocraze -

    Dive into the dynamic realm of IoT with our latest...

Frequently Asked Questions

1. How does the ultrasonic sensor communicate with Arduino?

The Ultrasonic sensor sends out a pulse to Arduino. Using the duration of this pulse, the distance is calculated.

read more : Interfacing MAX30100 Pulse Oximeter with Arduino

2. How does Arduino code connect to the ultrasonic sensor?

In Arduino code, we give instructions to Arduino to perform some tasks. The Arduino code for the Ultrasonics sensor is used to tell Arduino to read the ultrasonic sensor output and calculate the distance.

3. Does the ultrasonic sensor use PWM?

No, the ultrasonic sensor does not use PWM. It just gives a pulse with a particular time duration.

read more :  How 433MHz RF Module Works & Interfacing With Arduino

4. What is the maximum range of the ultrasonic sensor?

The ultrasonic sensor measures the distance anywhere between 2 cm to 400 cm.

5. Do ultrasonic sensors interfere with each other?

It depends on how the ultrasonic sensor is placed. If the ultrasonic sensors are placed opposite to each other, then they may interfere.

6. What can ultrasonic sensors be used for?

The ultrasonic sensors are used to measure the distance to an object ranging from 2 cm to 400 cms.

read more : Interfacing Proximity Sensors with Arduino

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.

You may also like to read

  • What is a Bluetooth beacon?

    What is a Bluetooth beacon?

    - Robocraze -

    Discovering the power of Bluetooth beacons unveils a world of possibilities in the realm of connectivity. In our latest blog,...

    What is a Bluetooth beacon?

    - Robocraze -

    Discovering the power of Bluetooth beacons unveils a world of...

  • What is a t nut

    What is a t nut

    - Robocraze -

    Ever wondered what exactly a T-nut is and how it can revolutionize your projects? Our latest blog delves deep into...

    What is a t nut

    - Robocraze -

    Ever wondered what exactly a T-nut is and how it...

  • What is outdoor positioning system

    What is outdoor positioning system

    - Robocraze -

    Discover the magic behind Outdoor Positioning Systems (OPS) in our latest blog. Delve into the basics with an insightful Introduction,...

    What is outdoor positioning system

    - Robocraze -

    Discover the magic behind Outdoor Positioning Systems (OPS) in our...

  • What is data visualization in IoT

    What is data visualization in IoT

    - Robocraze -

    Dive into the dynamic realm of IoT with our latest blog! We kickstart with an insightful Introduction, delving into the...

    What is data visualization in IoT

    - Robocraze -

    Dive into the dynamic realm of IoT with our latest...