Ultrasonic Distance Measurement with Arduino
Author : Dinesh Kumar Wickramasinghe
Introduction and Images
We can use ultrasonic sensors to measure distance with Arduino.
There are several types of ultrasonic sensors available in the electronic market.
In this example, I am using the famous HC-SR04 type ultrasonic sensor.
This sensor is cheap, easy to connect to Arduino and highly used in robotic projects to identify obstacles.
I will also use a LCD screen to display the distance in both Inches and Centimeters.
Before we start, Here are some images of the Completed Project
How Ultrasonic Works
Active ultrasonic sensors generate high-frequency sound waves and evaluate the echo which is received back by the sensor, measuring the time interval between sending the signal and receiving the echo to determine the distance to an object.
Here is a very good article to read more information
Since we are using LCD screen in our project. you need to import the LiquidCrystal library first. You can also use the serial monitor window to display the distance without using LCD. I will give you both code examples. To read the distance, our code sends a pulse to the sensor to initiate a reading, then it listens for a pulse to return. The length of the returning pulse is proportional to the distance of the of the object from the sensor.
The HC-SR04 sensor has four pins. First and last pins for +5V and Ground. You can connect them to power pins in your Arduino board (Please refer the schematic). Other pins are trigger and echo pins which we use to get data from the sensor.
Arduino Connectivity
You can follow the below schematic to connect your ultrasonic sensor to the Arduino board.
You can also run this project without the LCD Screen.
In that case you can use the serial monitor to view the measurements.
I will give you two source codes for LCD and Serial Monitor.
Source code With LCD Screen
Below code example will show you the distance on the LCD screen
#include <LiquidCrystal.h>
LiquidCrystal lcd (7, 8, 9, 10, 11, 12);
int pingPin = 4;
int inPin = 5;
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147;
int cmconv = 59;
String s1, s2;
void setup() {
lcd.begin(16, 2);
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
inches = microsecondsToInches(duration);
indec = (duration - inches * inchconv) * 10 / inchconv;
cm = microsecondsToCentimeters(duration);
cmdec = (duration - cm * cmconv) * 10 / cmconv;
s1 = "Distance: " + String(inches) + "." + String(indec) + "in";
s2 = "Distance: " + String(cm) + "." + String(cmdec) + "cm";
lcd.setCursor(0, 0);
lcd.print(s1);
lcd.setCursor(0,1);
lcd.print(s2);
delay(1000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / inchconv;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / cmconv;
}
Source code to run with Serial Monitor
Below code example will show you the distance on the Arduino Serial Monitor
int pingPin = 4;
int inPin = 5;
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147;
int cmconv = 59;
String s1, s2;
void setup() {
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
inches = microsecondsToInches(duration);
indec = (duration - inches * inchconv) * 10 / inchconv;
cm = microsecondsToCentimeters(duration);
cmdec = (duration - cm * cmconv) * 10 / cmconv;
s1 = "Distance: " + String(inches) + "." + String(indec) + "in";
s2 = "Distance: " + String(cm) + "." + String(cmdec) + "cm";
Serial.print(s1);
Serial.println();
Serial.print(s2);
delay(1000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / inchconv;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / cmconv;
}
Hope you enjoyed the tutorial. If you found any issue during the project or if you have any feedback, please comment.