Arduino Interfacing with Ultrasonic Sensor

Are you looking to interface an ultrasonic sensor with an Arduino Uno? Look no further! In this informative blog, we explore the ins and outs of ultrasonic sensors, including how they work and their key features. We'll also provide you with a step-by-step guide on how to connect your ultrasonic sensor to your Arduino Uno, along with the necessary code. Whether you're a seasoned Arduino user or just getting started, this guide has everything you need to get started with ultrasonic sensors.

What is Ultrasonic Sensor

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves.

An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity.  

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

What is Ultrasonic Sensor

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 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 interface the Ultrasonic sensor with Arduino UNO

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.

 

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 6

#define trigPin 7

 

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

In this blog post, we've learned that ultrasonic sensors are an essential component in distance measurement and obstacle detection. By using sound waves beyond the range of human hearing, these sensors can accurately detect distances and provide data for a wide range of applications. Thanks to their reliability, accuracy, and affordability, ultrasonic sensors are becoming increasingly popular among hobbyists and professionals alike. By following the steps we've outlined in this article and using the provided code, you'll be able to easily interface an ultrasonic sensor with an Arduino Uno and start building your own projects. So, what are you waiting for? Get started with ultrasonic sensors today and unlock the potential of your Arduino projects!

 

 

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!)

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

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

Leave a comment

Please note, comments must be approved before they are published