Episode 7 – Ultrasonic Rangefinder

Estimated read time 3 min read

This article is also now available as a full video, click the link below to open the video in a new tab:

For today’s presentation we’ll be taking you through an electronics lab demo. If you’re interested in learning electronics there is enough detail in the video for you to follow along. Otherwise this is a nice demo of some electronics concepts including some of the behavior of ultrasound systems.

The circuit has three main components, an Arduino Uno microcontroller, a part with an ultrasound transmitter and an ultrasound receiver, and a small LCD screen for the output. The completed project is able to detect the range in centimeters to an object in front of the sensor and display the distance on the LCD screen.

Here is the parts list for the project: (This would total around $50 for all parts at the time of this article)

Small Breadboard
Jumper Cables
HC-SR04 Ultrasonic Sensor
Arduino Uno
3pcs I2C IIC 1602 LCD Display Module 16×02 LCD Screen Module for Arduino Raspberry Pi

Here are the connections:

Ultrasonic sensor GND pin → Common Ground
Ultrasonic sensor VCC pin → Common Power
Ultrasonic sensor Trig pin → Arduino Uno A1 pin
Ultrasonic sensor ECHO pin → Arduino Uno A2 pin
Arduino Uno 5V pin → Common Power
Arduino Uno Ground pin → Common Ground
LCD GND pin → Common Ground
LCD VCC pin → Common Power
LCD SDA pin → Arduino Uno A4 pin
LCD SCL pin → Arduino Uno A5 pin

And the full source code is below:

//MIT License

//Copyright (c) 2025 TargetedTechTalk.com
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


#include <Wire.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);
int TRIGGER_PIN = 15;
int ECHO_PIN = 16;
int THRESHOLD_DISTANCE = 10;

void setup() {
  lcd.init();          // Initialize the LCD
  lcd.backlight();     // Turn on backlight
  lcd.setCursor(0, 0); // First column, first row
  lcd.print("Distance Sensor");
  lcd.setCursor(0,1);
  lcd.print("Range: ");
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

}

void loop() {
  float distance;
  int pulseLength;
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(20);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(100);
  digitalWrite(TRIGGER_PIN, LOW);
  pulseLength = pulseIn(ECHO_PIN, HIGH);
  distance = pulseLength * 0.017;

  lcd.setCursor(7,1);
  lcd.print(distance);
  lcd.print("     ");

}

targetedtechtalk@protonmail.com

More From Author