Lesson 20 – Ultrasonic Distance Sensor

Introduction

This Arduino program measures the distance to an object using an ultrasonic sensor and displays the result on an LCD screen.

It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back. By recording the elapsed time between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sonar sensor and the object.

This technology is used in crash avoidance systems, parking assistance and proximity detection.

The Circuit

Circuit assembled on a breadboard
Schematic

Program Description

The setup() function, initializes the LCD and sets the pin modes for the ultrasonic sensor.
The loop() function repeatedly measures the distance, updates the display with the measured value in centimeters, and includes a brief pause between readings.
The program continuously refreshes the data, showing real-time distance measurements as the sensor is moved.

See the flowchart below for more information

Code flowchart