How to Configure Ultrasonic Sensor With Arduino Uno

Step-by-step instructions on wiring and configuring an ultrasonic sensor with Arduino Uno to calculate distances accurately.

How to Configure Ultrasonic Sensor With Arduino Uno

An ultrasonic sensor, also known as an ultrasound sensor, is a device that uses sound waves to measure distances or detect the presence of objects. It works based on the principle of echolocation, similar to how bats navigate and locate objects in their surroundings.

Ultrasound sensors typically consist of a transmitter and a receiver. The transmitter emits ultrasonic sound waves at a frequency higher than the upper limit of human hearing (usually above 20 kHz). These sound waves propagate through the air and bounce off objects in their path.

The receiver of the ultrasound sensor detects the echoes of the sound waves reflected back from the objects. The sensor calculates the distance between itself and the object by measuring the time it takes for the sound waves to travel to the object and back.

Ultrasound sensors can provide distance measurements with high accuracy and are widely used in various applications, including robotics, automation, parking assist systems, object detection, and industrial sensing. They are commonly employed in both indoor and outdoor environments.

When using an ultrasound sensor with an Arduino, you can interface the sensor with the Arduino’s digital or analog pins. The Arduino can then process the received signals and calculate distances or trigger actions based on the presence of objects.

It’s important to note that different ultrasound sensors may have different specifications and features, such as operating range, beam angle, and communication protocols. Therefore, it is recommended to refer to the specific datasheet and documentation of the ultrasound sensor you are using for accurate usage instructions and specifications.

What will you learn from this article?

Here, in this article, I will explain two basic examples that will help you to understand all the basics of ultrasonic sensors (HC-SR04). You will learn, How to use an ultrasound sensor with Arduino Uno and develop a high-level application.

First, I will explain a fundamental example, in which, you will learn how to calculate a distance in centimeters and inches (using a serial monitor). In the second example, I will explain how to calculate the distance in centimeters and inches using an ultrasonic sensor and how to display them on an LCD screen.

How ultrasonic sensor (HC-SR04) works

ultrasonic sensor

The HC-SR04 is a popular and widely used ultrasonic sensor module for distance measurement and object detection. It is a cost-effective sensor that offers reliable performance and ease of use. The HC-SR04 has a typical range of about 2 centimeters to 4 meters (2cm to 400 cm, or an inch to 13 feet).

Ultrasonic sensors emit high-frequency sound waves beyond the range of human hearing (In the case of the HC-SR04 ultrasonic sensor, it typically emits sound waves at a frequency of around 40 kHz). The emitted sound waves travel in a spherical pattern away from the sensor.

When the sound waves encounter an object or obstacle in their path, they are reflected back toward the sensor as echoes. The sensor’s receiver detects and measures the time it takes for the echoes to travel back. Using the measured time, the sensor calculates the distance to the object based on the speed of sound in the medium (typically air).

The calculation for distance traveled by ultrasonic wave

Travel time=pulse duration

Speed of ultrasonic wave=speed of sound

[note: 1 meter= 100 centimeters, 1 secound=1*106 microseconds]

Speed of sound=340 m/sec =340*100 cm/sec=34000 cm/1*106 microsecond=0.034 cm/microsecond

So, the speed of sound=0.034 cm/microsecond

Total traveled distance= speed of sound * travel time
=speed of sound * pulse duration
=0.034 * pulse duration

Therefore, the total traveled distance=0.034 * pulse duration

After the sound wave reflects off the object, it travels back to the ultrasound sensor. This distance is also half the total distance traveled by the sound wave and represents the distance from the object back to the sensor.

However, since the ultrasound sensor measures the time for the round trip (to the object and back), you need to divide the result by 2 to obtain the distance from the sensor to the object or vice versa.

Distance between the object and sensor(cm)=traveled distance /2=(0.034 * pulse duration)/2=0.017*pulse duration

Hence, the Distance between the object and sensor(cm)=0.017*pulse duration

Example 1: How to Configure Ultrasonic Sensor With Arduino Uno

This example helps you to understand:

  • How to connect the ultrasonic sensor with Arduino uno.
  • How to calculate the distance between the object and an ultrasonic sensor in centimeters and inches.
  • How to display calculated distance on Serial monitor.

Required components

Circuit Diagram

The HC-SR04 module usually has four pins

  • VCC: Connect to the positive supply voltage (5V to 12V).
  • GND: Connect to the ground (0V) of the power supply.
  • Trig (Trigger): An input pin is used to initiate the emission of the sound waves.
  • Echo: An output pin that generates a pulse when it detects the echo of the emitted sound waves. The duration of this pulse corresponds to the time it took for the waves to travel to the object and back.

Connectivity with Arduino UNo

HC-SR04 ultrasonic sensor has four pins such as VCC, Trig, Echo, and GND. According to the connection diagram, the Trig Pin is connected to the analog pin 6 and the Echo pin is connected to the digital pin 7 of the Arduino UNo board. The VCC of the ultrasonic sensor is connected to the 5-volt power supply, and the GND pin is connected to one of the GND pins of the Arduino UNo board respectively.

ultrasonic sensor basic pin How to Configure Ultrasonic Sensor With Arduino Uno

Code

//How to use ultrasonic sensor using Arduino uno

//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>

//For more details please visit:https://arduinounomagic.com/ultrasonic-sensor-with-arduino-uno/

//For more projects please visit://www.arduinounomagic.com


//define pins
const int trig_pin=6;
const int echo_pin=7;

//define variables
long pulse_duration;
int distance_cm, distance_inch;

void setup() {

Serial.begin(9600); // Start serial communication

   pinMode(trig_pin, OUTPUT); //Configure trig_pin 6 as output pin
   pinMode(echo_pin, INPUT); //Configure  echo_pin 7 as input pin
  
}

void loop() {

digitalWrite(trig_pin,LOW); //Initally clear the trig_pin for 5 micro sec

delayMicroseconds(5);//pause for 5 microseconds
   
digitalWrite(trig_pin,HIGH); //Configure the trig_pin 6 on HIGH state for 10 microsec

delayMicroseconds(10);//pause for 10 microseconds

digitalWrite(trig_pin,LOW); 
  
pulse_duration=pulseIn(echo_pin, HIGH);// Read the value at echo_pin 7 and measure the travel time of sound wave in microsec

distance_cm=(pulse_duration*0.034)/2;// Calculate the distance in cm
distance_inch=(pulse_duration*0.0133)/2;// Calculate the distance in inch

Serial.print("Distance in cm: ");
Serial.print(distance_cm);
Serial.println(" cm");

Serial.print("Distance in inch: ");
Serial.print(distance_inch);
Serial.println(" inch");
Serial.println();


delay(1000);

}

Output

Check out the output video, it will help you understand the workings of the ultrasonic sensor in both the simulator and the real world respectively.

Arduino Uno with ultrasonic sensor

Example 2: How to Configure Ultrasonic Sensor With Arduino Uno using LCD screen

This example helps you to understand:

  • How to connect the ultrasonic sensor with Arduino uno.
  • How to connect LCD screen with Arduino Uno.
  • How to show calculated distance in centimeters and inches as well on LCD screen.

in this example, I will show you how to display output on an LCD screen instead of a serial monitor. if you need to know more about How to use LCD with Arduino Uno, then you can check my other article to understand it briefly.

Required components

Circuit Diagram

ultrasonic sensor with LCD How to Configure Ultrasonic Sensor With Arduino Uno

According to the circuit connection diagram:

  • The ultrasonic sensor’s VCC pin and the VDD pin of the LCD screen are both connected to the 5-volt power supply of the Arduino uno board.
  • The RW, GND, and LED- pins of the LCD screen are connected to the GND pin of the Arduino Uno board.
  • The RS and E pins of the LCD screen are connected to the pin numbers 8 and 2 of the Arduino Uno board respectively.
  • The data pins D4, D5, D6, and D7 of the LCD screen are connected to pin numbers 7, 6, 5, and 4 of the Arduino uno board respectively.
  • The 220-ohm resistor’s one terminal is connected to the LED+ pin of the LCD screen and the other terminal is connected to the 5-volt power supply of the Arduino uno board.
  • The preset is a three-terminal device. Its first terminal is connected to the 5volt power supply of the Arduino uno board, the middle terminal is connected to the VEE of the LCD screen, and the third terminal is connected to the GND terminal of the Arduino uno board.

This was all about the connection diagram. Now you must be wondering what is the need to connect the 220-ohm resistor on the LED+ backlight pin of the LCD screen.

LCD screen has two backlight pins, known as LED+ and LED- pins. If you run a LED over its forward voltage, the current increases exponentially. Consequently, it’s mandatory to provide a current limiting circuit (such as a simple resistor in series). If you connect the LED+ pin directly to the 5-volt power supply of the Arduino board, the result of which current through the LED will be so high, it will get burnt. To overcome this problem, I connected a 220-ohm resistor in series with a LED+ pin of the LCD screen (between VCC and LED+of LCD screen).

How to control current through backlight LED

Let’s watch the video to understand how to control current through the backlight LED of the LCD screen using Arduino uno:

How to control current through backlight LED of LCD screen

Code

//How to use ultrasonic sensor with LCD using Arduino uno

//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>

//For more details please visit:https://arduinounomagic.com/ultrasonic-sensor-with-arduino-uno/

//For more projects please visit://www.arduinounomagic.com



#include<LiquidCrystal.h> //Add LCD hedder file

LiquidCrystal ARDUINO_LCD(8, 2, 7, 6, 5, 4);// Set RS pin=8, Enable pin=2, D4=7, D5=6, D6=5, d7=4. 

//define pins
const int trig_pin=3;
const int echo_pin=12;

//define variables
long pulse_duration;
int distance_cm, distance_inch;

void setup() {
  ARDUINO_LCD.begin(16, 2);//set up number of colomn and rows of LCD

   pinMode(trig_pin, OUTPUT); //Configure trig_pin 3 as output pin
   pinMode(echo_pin, INPUT); //Configure  echo_pin 12 as input pin
  
}

void loop() {

digitalWrite(trig_pin,LOW); //Initally clear the trig_pin for 5 micro sec

delayMicroseconds(5);//pause for 5 microseconds
digitalWrite(trig_pin,HIGH); //Configure the trig_pin 3 on HIGH state for 10 microsec

delayMicroseconds(10);//pause for 10 microseconds
digitalWrite(trig_pin,LOW); 

pulse_duration=pulseIn(echo_pin, HIGH);// Read the value at echo_pin 12 and measure the travel time of sound wave in microsec

distance_cm=(pulse_duration*0.034)/2;// Calculate the distance in cm
distance_inch=(pulse_duration*0.0133)/2;// Calculate the distance in inch
 
ARDUINO_LCD.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed 
  
//print distance in cm
ARDUINO_LCD.print("Distance: ");
ARDUINO_LCD.print(distance_cm);
ARDUINO_LCD.print(" cm");
  
ARDUINO_LCD.setCursor(0,1);// Sets the location at which subsequent text written to the LCD will be displayed 

//print distance in inch
ARDUINO_LCD.print("Distance: ");
ARDUINO_LCD.print(distance_inch);
ARDUINO_LCD.print(" inch");

delay(1000);

}

Output

Check out the output video, it will help you to understand the workings of the ultrasonic sensor using an LCD screen in both the simulator and the real world respectively.

Ultrasonic sensor with LCD using Arduino Uno

Hope you learn something new from this article.

If you have any queries, let me know in the comments. I will try to help you as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *