Hi guys, welcome to today’s Arduino tutorial. Today I will be showing us how to use the popular HC-SRO4 Ultrasonic sensor with the Arduino to measure distance.

HC SRO4 is a very cheap and effective ranging sensor which uses the principle of echo and ultrasonic sound to measure the distance between two points.

HC-SR04 Ultrasonic Sensor

The module has four pins two of which are for operational purposes. one of those pins is the trigger pin which when set high, instructs the ultrasonic transmitter on the sensor to transmit ultrasonic sound waves. This waves travel till they get obstructed by an object and deflected back towards the sensor. The Ultrasonic receiver on the sensor picks the reflected sound waves and the time taken between when the waves were sent out and when the reflected wave was received is used to calculate the distance between the sensor and the object in its path.

 

Operation Principle of HC-SR04

Some of the features of this sensor are listed below.

  • Operating voltage: +5V
  • Theoretical  Measuring Distance: 2cm to 450cm
  • Practical Measuring Distance: 2cm to 80cm
  • Accuracy: 3mm
  • Measuring angle covered: <15°
  • Operating Current: <15mA
  • Operating Frequency: 40Hz

From experience, some of this features may vary from sensor to sensor due to different desires and design goals of manufacturers.

In addition to the HC-SRO4, for this project, we will also be using one of my favorite displays, the Nokia 5110 LCD to display the distance value after it has been determined using the ultrasonic sensor.

Required Parts and Where to Buy

The following parts are required to build this project and they can be bought through the link in front of them.

1. Ultrasonic Distance Sensor: https://educ8s.tv/part/HCSR04

2. Nokia 5110 LCD: https://educ8s.tv/part/NOKIA5110

3. Arduino Uno: https://educ8s.tv/part/ArduinoUno

4. Small Breadboard: https://educ8s.tv/part/SmallBreadboard

5. Wires: https://educ8s.tv/part/Wires

 

Schematics

The schematics for this project is a very simple one. We have treated how to use the Nokia 5110 LCD display in several tutorials on this website so I won’t be going deep into its connection details, but the ultrasonic sensor, on the other hand, has 4 pins, two of which are used for powering the sensor(VCC and GND) the other pins are the trigger and the echo which are used for operational purposes as mentioned above.

Connect the sensors to the Arduino as shown in the schematics below.

For clarity, the pin connections between the Arduino and the ultrasonic sensor is further described below.

HC-SR04 – Arduino

VCC – 5v

GND – GND

Trig – D7

Echo – D6

pin connections between the LCD and the Arduino are also described below.

Nokia 5110 – Arduin0

Pin 1(RST) – D12

Pin 2(CE) – D11

Pin 3(DC) – D1

Pin 4(DIN) – D9

Pin 5(CLK) – D8

Pin 6(VCC) – VCC

Pin 7(LIGHT) – GND

Pin 8(GND) – GND

 

Code

Just like the schematics, the code for this project is quite easy. I will be making two Arduino sketches available for this tutorial. The first one is for those who do not have the Nokia 5110 LCD display while the second one is for those who have the LCD. The first sketch which does not incorporate the display uses the serial monitor as a means of displaying the measured distance.  These two sketches are similar and the only difference is the code which was included to implement the LCD functionalities.

While a library is not needed for the first sketch, for the second sketch which includes the code to use the Nokia 5110 LCD display, we will be using the LCD graph library which can be downloaded from the link below.

Libraries

Nokia 5110: http://www.rinkydinkelectronics.com/library.php?id=47

since we have covered how to use the Nokia 5110 LCD display in several tutorials here, I will explain the first code which will explain the basics associated with programming the Arduino to measure distance using the Ultrasonic sensor. This will help, should you decide to use a different display with the project in the future.

To briefly explain the code, the first thing we do is declare the pins of the Arduino to which the ultrasonic sensor is connected to after which we specify the maximum and minimum range of the sensor, this is to prevent the sensor from going above its range and returning false readings.

#define echoPin 6 // Echo Pin
#define trigPin 7 // Trigger Pin

int maximumRange = 250; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance

Next, we move to the void setup() function where we set the pin modes of the sensor pins and initialize the serial monitor.

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}

Next, we write the void loop function. The first line of code under the function calls the readdistance() function.

readDistance();

The function takes the trigger pin on a series of high and low with delays in between to generate the ultrasonic sound, after which the Arduino inbuilt pulse in function is used to retrieve the echo and calculate the time elapsed. The duration is then converted to distance using a formula from the datasheet and returned to the void loop function.

int readDistance()
{
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 distance = duration/58.2;
}

we then check if the distance value sent back is within the range of the sensor. If the value is within the range, we take it as genuine and print it, if the value is not within the range we print “out of range” on the serial monitor. We wait for a few seconds before reading the sensor again using the delay() function. The delay after the data is displayed is quite important as it prevents us from reading echoes from the previous sound that was sent out.

if(distance>minimumRange && distance < maximumRange)
{
 Serial.println(distance);
}else
{
  Serial.println("Out of range...");
}
 delay(50);
}

The complete code for this project can be downloaded from the link below. It also contains the sketch that implements the LCD side of this project.

 

[adsense]

——————–
CODE OF THE PROJECT
——————–

 

 

Copy the code into the Arduino IDE, upload to your setup and test, you should see the system display distances to objects in cm as shown in the image below.

Serial Monitor Distance Meter

For those using the sketch with the LCD, the distance will be displayed on the LCD as shown in the image below.

Distance Meter

That’s all for today, thanks for reading and watching. If you have any question(s) about this tutorial, feel free to reach out to me via the comment section. I will also love to hear about other cool things people are building with the ultrasonic sensor. You can subscribe to our youtube channel to get updates on posts by hitting the subscribe button below.

——————–
SUBSCRIBE ON YOUTUBE
——————–

Never miss a video: Subscribe to educ8s.tv