When you are reversing, you will see the distance between the car and the surrounding obstacles to avoid collision. The device for detecting the distance is an ultrasonic sensor. In this experiment, you will learn how the ultrasonic wave detects the distance.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:




Elite Explorer Kit


Elite Explorer Kit

You can also buy them separately from the links below.



Arduino Uno R4 WiFi




Jumper Wires


Ultrasonic Module


I2C LCD1602




Schematic Diagram




  • You can open the file 06-ultrasonic.ino under the path of elite-explorer-kit-main\basic_project\06-ultrasonic directly.

  • Or copy this code into Arduino IDE.

Code Analysis

1. Initialize the ultrasonic sensor and LCD1602

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);  // initialize the Liquid Crystal Display object with the I2C address 0x27, 16 columns and 2 rows

// Define the pin numbers for the ultrasonic sensor
const int echoPin = 3;
const int trigPin = 4;

void setup() {
  pinMode(echoPin, INPUT);               // Set echo pin as input
  pinMode(trigPin, OUTPUT);              // Set trig pin as output

  lcd.init();       // initialize the LCD
  lcd.clear();      // clear the LCD display
  lcd.backlight();  // Make sure backlight is on


2. Display the distance on the LCD1602

void loop() {
  float distance = readDistance();  // Call the function to read the sensor data and get the distance

  lcd.setCursor(0, 0);         //Place the cursor at Line 1, Column 1. From here the characters are to be displayed
  lcd.print("Distance:");      ////Print Distance: on the LCD
  lcd.setCursor(0, 1);         //Set the cursor at Line 1, Column 0
  lcd.print("               ");  //Here is to leave some spaces after the characters so as to clear the previous characters that may still remain.
  lcd.setCursor(7, 1);         //Set the cursor at Line 1, Column 7.
  lcd.print(distance);         // print on the LCD the value of the distance converted from the time between ping sending and receiving.
  lcd.setCursor(14, 1);        //Set the cursor at Line 1, Column 14.
  lcd.print("cm");             //print the unit "cm"

  delay(800);                       // Delay for 800 milliseconds before repeating the loop

3. Convert the time to distance

float readDistance(){// ...}

Here, “PING” refers to the process where the ultrasonic sensor sends out an ultrasonic pulse (or “ping”) and then waits for its echo.

PING is triggered by a HIGH pulse of 2 or more microseconds. (Give a short LOW pulse beforehand to ensure a clean HIGH pulse.)

digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

The echo pin is used to read signal from PING, a HIGH pulse whose duration is the time (in microseconds) from the sending of the ping to the reception of echo of the object. We use the following function to obtain the duration.

pulseIn(echoPin, HIGH);

The speed of sound is 340 m/s or 29 microseconds per centimeter.

This gives the distance travelled by the ping, outbound and return, so we divide by 2 to get the distance of the obstacle.

float distance = pulseIn(echoPin, HIGH) / 29.00 / 2;     // Formula: (340m/s * 1us) / 2