How Can an Arduino Be Interfaced with an Ultrasonic Sensor?

https://updatehub.xyz/

We’ll walk you through connecting an Arduino to an ultrasonic sensor in this tutorial so you can measure distance and see it on the serial monitor. First, we will go over how to interface the ultrasonic sensor; at the end of the project, we will add the I2C LCD display.

Check out our Arduino Projects and Tutorials if you are new to working with Arduino. For everyone to construct and learn on their own, we offer a collection of around 500+ Arduino projects with code, circuit diagrams, and thorough explanations available for free.

Components Required

  1. Arduino board (e.g., Arduino Uno)
  2. HC-SR04 Ultrasonic Sensor
  3. I2C LCD Display (e.g., 16×2 characters)
  4. Jumper wires

In our previous attempts, we have used this HC-SR04 with an Arduino, Raspberry Pi, and PIC microcontroller to build many interesting projects, some of which are linked below.

Pinout for HCSR04 Ultrasonic Sensor

The four pins on the ultrasonic sensor are VCC, Trig, Echo, and GND. The explanation of the pinout is provided below.

Operation of the Ultrasonic Sensor

A number of crucial parts come together to form an ultrasonic sensor, which uses high-frequency sound waves to detect things or measure distances. The transducer, which is essentially a piezoelectric crystal that emits and receives ultrasonic waves, is at the center of it all.

The ultrasonic transmitter initiates the sensor’s activity by releasing short bursts of high-frequency sound waves into the surrounding air.

The MAX232 sensor, located on the right side of the ultrasonic sensor’s back, transforms the electrical signal that comes from the trig pin. transforms it into an ultrasonic pulse and transmits it via the transmitter.

The ultrasonic receiver picks up these vibrations when they bounce off nearby objects. This ultrasonic pulse is received by the two LM324 ICs on the left, which transform it into an electrical signal and transmit it to the echo pin.

The reflected waves are transformed into electrical impulses by the receiver.

The sensor’s signal processing circuitry precisely measures the time it takes for sound waves to reach the item and back. For the purpose of computing distances and detecting the presence of objects nearby the sensor, this time measurement is essential. Now, we’ll use the following formula to get the distance.

We now know that sound travels at 343.2 m/s. We must convert the sound speed to cm/us, or 0.03432 cm/uS, in order to determine the distance. We also have the time value that was obtained from the ultrasonic sensor, which is 500 uS.

Distance= (0.03432 cm/us x 700uS) / 2 = 8.58 cm

As a result, we are aware of the object’s 8.58 cm distance from the sensor.

Frequently Posed Queries Concerning Ultrasonic Sensors

What is an ultrasonic sensor’s maximum range?
Although an ultrasonic sensor’s maximum range can vary based on its particular brand and design, basic sensors typically have a range of 2 centimeters (0.79 inches) to several meters (tens of feet). For certain uses, specialized sensors may have extended ranges.

Do ambient factors affect ultrasonic sensors?
Indeed, environmental elements like temperature, humidity, and air pressure can have an impact on ultrasonic sensors. Variations in these parameters have the ability to change the speed of sound, which could have an impact on how accurate distance measurements are.

Is it possible for ultrasonic sensors to detect more than one thing at once?
Indeed, several objects can be detected by ultrasonic sensors as

What is an ultrasonic sensor’s maximum range? Although an ultrasonic sensor’s maximum range can vary based on its particular brand and design, basic sensors typically have a range of 2 centimeters (0.79 inches) to several meters (tens of feet). For certain uses, specialized sensors may have extended ranges.

Do ambient factors affect ultrasonic sensors? Indeed, environmental elements like temperature, humidity, and air pressure can have an impact on ultrasonic sensors. Variations in these parameters have the ability to change the speed of sound, which could have an impact on how accurate distance measurements are.

Is it possible for ultrasonic sensors to detect more than one thing at once? Indeed, several objects can be detected by ultrasonic sensors as as long as they are in the detection range of the sensor. If their echoes overlap, they could find it difficult to tell apart things that are near together.

Arduino Interface Circuit Diagram Using Ultrasonic Sensor

to connect the Arduino to the ultrasonic sensor. The TRIG (trigger) pin of the sensor must be connected to the Arduino pin. 4. The Arduino pin to the sensor’s ECHO pin 5. Connect the ground (GND) pin of the sensor to the Arduino’s GND and the VCC (power) pin to the Arduino’s 5V output..

Arduino Code for Ultrasonic Sensor Interface with Arduino

This is the whole description of how to interface an Arduino with an ultrasonic sensor, line by line. The entire code is located at the very bottom of the blog.

const int trigPin = 4;
const int echoPin = 5;

These lines define two constants, trigPin and echoPin, to specify the Arduino pins to which the trigger and echo pins of the ultrasonic sensor are connected, respectively.

long duration;
int distance;

Two variables are specified here: distance (of type int) and duration (of type long). The length of the echo pulse and the estimated distance will be stored in these variables, accordingly.

void setup() {
  Serial.begin(9600);

The Arduino initiates serial communication at a baud rate of 9600 bits per second in the setup() function. The purpose of this is to connect to your computer’s Serial Monitor, which is where the distance measurements are shown.

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

The echoPin is configured as an INPUT and the trigPin as an OUTPUT in these lines. To control the ultrasonic sensor, this configuration is required. A signal is sent via the trigger pin (TRIG) and returned by the echo pin (ECHO).

void loop() {
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

These lines start the ultrasonic sensor’s measurement inside the loop() function. The trigger pin (TRIG) is first set to LOW, then it uses delayMicroseconds() to wait a short while to make sure the trigger signal is clean, and finally it sets it to HIGH for 10 microseconds before returning it to LOW. The ultrasonic sensor emits an ultrasonic pulse in response to this sequence.

  duration = pulseIn(echoPin, HIGH);

The length (in microseconds) of the pulse received on the echo pin (ECHO) is measured on this line using the pulseIn() function. It measures the interval between those events by waiting until the ECHO pin gets HIGH and then waiting again until it goes LOW.

 distance = duration * 0.034 / 2;

In this case, the code uses the echo pulse’s duration to calculate the distance. The speed of sound in air, which is roughly 0.034 millimeters per microsecond, plus the fact that the pulse travels to and from the object are the basis for this formula. To account for the two-way travel, divide by two.

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

These lines output the computed distance in centimeters (“cm”), the actual distance value, and a descriptive label (“Distance:”) to the Serial Monitor.

Ultimately, a 1000 millisecond (1 second) delay is introduced to regulate the pace at which distance readings are obtained. To alter the measurement frequency to suit your needs, you can modify this delay.

Circuit Diagram for Arduino Interface Using 16×2 LCD and Ultrasonic Sensor

Connect the ultrasonic sensor to the Arduino as mentioned earlier (TRIG to pin 4, ECHO to pin 5).

Connect the I2C LCD to the Arduino:

SDA to A4 (for Arduino Uno)

SCL to A5 (for Arduino Uno)

VCC to 5V

GND to GND

Code to Connect Arduino to 16×2 LCD and Ultrasonic Sensor

Make that the libraries required for the I2C LCD are installed. For this, you can make use of the “LiquidCrystal_I2C” library. It can also be installed via the Arduino Library Manager.

You can copy, paste, and upload the entire Arduino, Ultrasonic Sensor, and LCD interface code to your Arduino board.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int trig=4;
const int echo=5;
void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
  lcd.print("Distance : ");
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trig,LOW);
  delay(2);
  digitalWrite(trig,HIGH);
  delay(10);
  digitalWrite(trig,LOW);
  long duration = pulseIn(echo,HIGH);
  long distance=duration*0.034/2; //speed of sound=340 m/s =0.034 cm/microsecond.
  lcd.setCursor(10,0);
  lcd.print("    ");
  lcd.setCursor(10,0);
  lcd.print(distance);
  Serial.print("Distance : ");
  Serial.println(distance);
  Serial.print(" cm")
  delay(10);
}

Using an Arduino LCD and an ultrasonic sensor, measure distance

This is an example of an Arduino and ultrasonic interface in action, showing the distance value in real time on the screen.

Code for Arduino and Ultrasonic Sensor

const int trigPin = 4; // Trigger pin of the ultrasonic sensor (connected to Arduino digital pin 2)
const int echoPin = 5; // Echo pin of the ultrasonic sensor (connected to Arduino digital pin 3)
void setup() {
  Serial.begin(9600); // Initialize serial communication for debugging
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  // Trigger the ultrasonic sensor by sending a 10us pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Measure the time it takes for the echo to return
  long duration = pulseIn(echoPin, HIGH);
  // Calculate the distance in centimeters
  // Speed of sound in air at room temperature is approximately 343 meters/second or 0.0343 cm/microsecond
  // Divide the duration by 2 to account for the time it takes for the sound pulse to travel to the object and back
  int distance = duration * 0.0343 / 2;
  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000); // Delay for readability (adjust as needed)
}

Code for Arduino, Ultrasonic Sensor, and LCD Display

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int trig=4;
const int echo=5;
void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
  lcd.print("Distance : ");
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trig,LOW);
  delay(2);
  digitalWrite(trig,HIGH);
  delay(10);
  digitalWrite(trig,LOW);
  long duration = pulseIn(echo,HIGH);
  long distance=duration*0.034/2; //speed of sound=340 m/s =0.034 cm/microsecond.
  lcd.setCursor(10,0);
  lcd.print("    ");
  lcd.setCursor(10,0);
  lcd.print(distance);
  Serial.print("Distance : ");
  Serial.println(distance);
  Serial.print(" cm")
  delay(10);
}

Leave a Reply

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