Welcome to the fourth blog in our Arduino series! This time we will use an ultrasonic sensor and a character LCD display to create a device that measures distance and displays it.
We will use an HC-SR04 ultrasonic sensor, which sends ultrasonic signals and measures the time taken for them to reflect back from an object. This time lets us calculate the distance between sensor and object.
To make the project more interactive and user-friendly, we will add a character LCD display with 20x4 characters. It will show the measured distance in an easily readable format.
We will build the project using a breadboard and jumper wires to connect the components to the Arduino. This provides a flexible, temporary circuit that can easily be changed or reused in other projects.
In the coming posts, we will explore the setup and coding in detail. You will learn to connect the ultrasonic sensor and LCD, adjust contrast and program Arduino to read and display the distance.
Required Components
| Arduino UNO |
| Ultrasonic Sensor HC-SR04 |
| LCD 20x4 |
| Jumper wires |
| or |
| Ultrasonic distance meter kit — current range; check compatibility |
HC-SR04 Ultrasonic Sensor

The HC-SR04 ultrasonic sensor used in this project has 4 pins: VCC and GND connect to the 5V and GND pins on the Arduino, while Trig and Echo connect to chosen digital pins on the Arduino.
Using Trig, we transmit ultrasound at a frequency of 40,000 Hz into the air. If an object or obstacle is in its path, the sound reflects back to the sensor. Measuring the travel time and knowing the speed of sound lets us calculate the distance.

It is fascinating to think about the ultrasonic waves travelling through the air. They reflect off an object and return to the sensor, enabling accurate distance measurements.
To generate ultrasound, set Trig HIGH for 10 μs. This sends a short burst travelling at the speed of sound. After the burst, the Echo pin goes HIGH and the sensor starts listening for the reflected signal from an object.
Without a valid echo, timeout behaviour can vary between modules; 38 ms is not a universal guarantee. The examples below use an explicit 30000 µs timeout and show “No echo” when pulseIn() returns 0.
When a valid echo is detected, Echo falls LOW. Its pulse width represents the sound’s round-trip travel time and is used to calculate distance.

Follow the module datasheet for trigger spacing. The cited HC-SR04 datasheet recommends a measurement cycle longer than 60 ms, so the code waits at least 65 ms before a new measurement.
For this, we use the basic distance formula:
Distance = Speed x Time
We know both speed and time. Time is how long Echo remained HIGH, and speed is the speed of sound, approximately 340 m/s here. We must also divide by 2 because the measured duration covers the journey to the object and back.

Suppose Echo was HIGH for 2 ms. To obtain centimetres, convert 340 m/s to 34 cm/ms.
Distance = (Speed x Time) / 2 = (34 cm/ms x 2 ms) / 2 = 34 cm.
Thus an Echo pulse of 2 ms, measured with pulseIn(), corresponds to a sensor-to-object distance of 34 cm.
Great! Let’s now use our ultrasonic sensor with the Arduino UNO. We will add the display later.
HC-SR04 Ultrasonic Sensor with Arduino
The circuit setup is straightforward. Once it is ready, we can review and upload the code, then use the Serial Monitor to check whether the ultrasonic sensor gives the correct distance.

Connect the module’s GND and VCC to Arduino GND and 5V respectively, and connect Trig and Echo to digital I/O pins that match the code.
Arduino Code (Serial Monitor)
Here is code for measuring distance with the HC-SR04 ultrasonic sensor and Arduino.
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH, 30000UL);
if (duration == 0) {
Serial.println("No echo");
delay(65);
return;
}
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(65);
}
Code Explanation
First define the Trig and Echo pins: Arduino pins 9 and 10, named trigPin and echoPin. We also need a long variable, duration, for the measured travel time and an integer variable for distance.
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
In void setup(), define trigPin as an output and echoPin as an input, and start serial communication to show results in Serial Monitor.
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
In void loop(), first clear trigPin by setting it LOW for 2 μs. Then set it HIGH for 10 μs to generate the ultrasonic burst.
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pulseIn(echoPin, HIGH, 30000UL) measures the HIGH pulse width in microseconds and stores it in duration. The third argument sets a 30000 µs timeout. If 0 is returned, the full examples show “No echo” and skip the distance calculation.
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH, 30000UL);
Here we use HIGH, because the HC-SR04 sets Echo HIGH after transmitting an 8-cycle ultrasonic burst. This starts timing; receiving the reflected sound makes Echo LOW and ends it. The function returns pulse length in microseconds.
To obtain distance, multiply duration by 0.034 and divide by 2, as explained earlier.
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Finally, display distance in Serial Monitor, or “No echo” if the pulse is missing. Upload, open Serial Monitor and compare with a flat object at a known distance. Wait at least 65 ms after each measurement. Then add the display.
Arduino Ultrasonic Sensor and LCD Display
Here is another example using the ultrasonic sensor with Arduino and showing results on an LCD.
Connect the ultrasonic sensor and LCD as shown in the next figure. We explain this setup further below. The ultrasonic sensor wiring does not need to change.
When connecting the display to Arduino UNO over I2C, choose cables to match the connectors. Typically use female-to-male jumper wires directly from the display’s male pins to UNO’s female headers, or wiring through a breadboard. Check your actual connectors before wiring.
Direct Connection: Typically use four female-to-male wires for GND, VCC, SDA and SCL between the display’s male pins and UNO’s female headers.
Breadboard Connection: If the module is fitted to a breadboard, use male-to-male jumper wires from the breadboard to UNO. Connect SDA to A4 and SCL to A5, along with GND and power.
Connect the GND pin on the display module to Arduino GND. This establishes a shared ground.
Connect the VCC pin on the display module to Arduino’s 5V pin. This supplies power to the display.
Connect the SDA pin on the display module to Arduino pin A4. This is the data connection.
Connect the SCL pin on the display module to Arduino pin A5. This is the clock connection.
With the display correctly connected, you can work on the code to control it. We use the libraries Wire.h, LCD.h and LiquidCrystal_I2C.h to simplify communication between Arduino and the display.
The distance-measurement code is essentially the same as the basic example. Here we print results on the LCD instead of Serial Monitor.
Arduino Code (20x4 Display)
The display version adds libraries (Wire.h, LCD.h, LiquidCrystal_I2C.h) and initialises the LCD display using lcd.begin(). It also adds code to control the LCD display and print distance measurements using lcd.print().
Here are the specific changes:
- Added libraries: The code now includes Wire.h, LCD.h and LiquidCrystal_I2C.h to control the LCD display.
- LCD object initialisation: An object of type LiquidCrystal_I2C with address 0x27 and its pin configuration is created using the LiquidCrystal_I2C class.
- Display dimensions: In lcd.begin(), the LCD width and height are set to 20 and 4, meaning 20 characters across and 4 rows.
- Printing on the display: Instead of printing distance in Serial Monitor, lcd.print() prints text and distance on the LCD. Centimetres and inches each appear on a separate line.
- Measurement interval: The code uses delay(65) when updating the display and when no echo is detected. This keeps triggers more than 60 ms apart, as recommended by the HC-SR04 datasheet. A missing pulse is shown as “No echo”, not 0 cm.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // Opretter et LCD-objekt. Parametre: (rs, enable, d4, d5, d6, d7)
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(20, 4); // Initialiserer forbindelsen til LCD-skærmen og specificerer dimensionerne (bredde og højde) af displayet
lcd.setBacklightPin(3, POSITIVE); // Sætter bagbelysningspin til positiv forsyningsspænding
lcd.setBacklight(HIGH); // Tænder bagbelysningen
pinMode(trigPin, OUTPUT); // Sætter trigPin til OUTPUT-mode
pinMode(echoPin, INPUT); // Sætter echoPin til INPUT-mode
}
void loop() {
digitalWrite(trigPin, LOW); // Sætter trigPin til lav logisk værdi (0V)
delayMicroseconds(2); // Venter i 2 mikrosekunder
digitalWrite(trigPin, HIGH); // Sætter trigPin til høj logisk værdi (5V)
delayMicroseconds(10); // Venter i 10 mikrosekunder
digitalWrite(trigPin, LOW); // Sætter trigPin til lav logisk værdi (0V)
duration = pulseIn(echoPin, HIGH, 30000UL); // Måler tiden for echo-signalet at være højt (HIGH) og gemmer det i duration
if (duration == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No echo");
delay(65);
return;
}
distanceCm = duration * 0.034 / 2; // Beregner afstanden i centimeter baseret på tiden
distanceInch = duration * 0.0133 / 2; // Beregner afstanden i tommer baseret på tiden
lcd.setCursor(0, 0); // Sætter positionen, hvor efterfølgende tekst skrevet til LCD'en vil blive vist
lcd.print("Distance: "); // Udskriver strengen "Afstand" på LCD'en
lcd.print(distanceCm, DEC); // Udskriver afstandsværdien fra sensoren i centimeter
lcd.print(" cm "); // Udskriver "cm" på LCD'en
delay(65); // Venter i 65 millisekunder
lcd.setCursor(0, 1); // Sætter positionen til næste linje
lcd.print("Distance: "); // Udskriver strengen "Afstand" på LCD'en
lcd.print(distanceInch, DEC); // Udskriver afstandsværdien fra sensoren i tommer
lcd.print(" inch "); // Udskriver "tommer" på LCD'en
delay(65); // Venter i 65 millisekunder
}
Adjusting Contrast on the LCD Display
The display module, where the four connecting pins are located, also has a potentiometer. Turning it adjusts contrast on the LCD display. The potentiometer adjusts character contrast, not normally the brightness of the backlight.
With the display connected correctly and its contrast adjusted using the potentiometer, you have completed another project!
You have successfully connected Arduino, the ultrasonic sensor and the LCD display, and can visualise distance measurements on the LCD display. This opens up many possibilities for using these components in other projects.
We have covered connecting an ultrasonic sensor to Arduino and showing distance measurements on an LCD display.
We hope this blog has helped and inspired you to explore Arduino and electronic components. Remember to enjoy your projects, keep learning and challenge yourself. Have fun with your future Arduino adventures!
You have successfully connected Arduino, the ultrasonic sensor and the LCD display, and can visualise distance measurements on the LCD display. This opens up many possibilities for using these components in other projects.
We have covered connecting an ultrasonic sensor to Arduino and showing distance measurements on an LCD display.
We hope this blog has helped and inspired you to explore Arduino and electronic components. Remember to enjoy your projects, keep learning and challenge yourself. Have fun with your future Arduino adventures!