Measure distances with an ultrasound sensor and Arduino


Following the previous post where I explained how to control one LCD display with Arduino. Today I’m going to use that display to display the measurement of the distance from an ultrasonic sensor to a target as it can be shown in the first picture.

1. Arduino with an USB connection and the Arduino IDE installed.
2. An ultrasound sensor, I’m using an Ultrasonic Ranging Module HC-SR04.
3. One LCD display: I’m using the same LCD I’ve used in this previous post.
4. A 2K Ohm linear potentiometer to control the contrast of the screen.
5. A breadboard and some wires to connect your LCD display to the Arduino board and to the potentiometer.

Circuit diagram:
Again, because we are using a LCD display, the most complicated thing of this circuit is to connect all the wires to each pin due to the high number of wires, the image below shows how everything is wired up, it is quite similar to the previous post, adding the ultrasonic sensor:

All conections between the LCD, the Arduino, the sensor and the potentiometer
All conections between the LCD, the Arduino, the sensor and the potentiometer

We are using the LCD in 4-bits configuration, to summarize the connections the following table shows where the LCD display is connected and the function of each pin:

LCD Display Pin
VSS (ground for the display) GND
VDD (Voltage for the display) 5V (it should also work with 3.3V)
VE (Contrast voltage for the screen) Potentiometer output (central pin)
RS (Register select) Pin 10 of Arduino board
RW (Read/Write pin) GND (sets the pin to write)
E (Enable pin) Pin 9 of Arduino board
D0 to D3 Not used, only required when 8 bit operation
D4 Pin 5 of Arduino bard
D5 Pin 4 of Arduino bard
D6 Pin 3 of Arduino bard
D7 Pin 2 of Arduino bard
A (Anode for backlight) 5V (lower voltage can be used for less light)
K (Cathode for backlight) GND






And now I show the connections for the ultrasound sensor, it has only four pins, so connecting this sensor is very straightforward:

LCD Display Pin
VCC (Voltage for the sensor) 5V
Echo Pin 13 of Arduino board
Trig (trigger pin) Pin 11 of Arduino board
GND (Ground GND




Arduino code:
The code is very similar than the previous post, we just need to include the pin configuration and the calculations for the distance:

The fist lines set up the LCD and from line 5 we set up the ultrasound sensor. An ultrasonic ranging module as this one works sending a small sound pulse (a small vibration) and it listens for the echo, that is, for the returning wave. In other words, a small pulse is sent to the target, part of the pulse is reflected and the sensor collects the sound and it gives us the time between the wave sent and the wave received. If we use the speed of the sound (which is 340.3 m/s or 1,126 ft/s at sea level, dry air and 20C -68F-) we can use the time to calculate the distance of the full travel (to the target and back). If we divided this result by 2, we have the distance from the sensor to the target.

So we need to define the pin for the trigger and for the echo in this case 13 and 11 respectively. We also define the variable pingTime to measure the time of the wave travelling from the sensor to the target and back, the speedOfSound constant (I’m using SI units) and finally the variable to store the distance to the target targetDistance.

Inside the void setup from line 13, first I start the serial port, this is not required but I find it useful for debugging: if something don’t go well, you can always check what the Arduino is receiving. The trigger pin is an output and the echo an input, for the LCD display, we start the display and we go to position (0,0) to write the string: ‘Target distance:’

Now we go to the void loop on line 24: the trigger pin is set low for 2miliseconds (ms) (2000 microseconds (us)) so no signal is emitted and we then set the pin high to emit a wave for 15us and back to low. In the next chunk of code we measure the pingTime with the echo pin, that is, the echo pin is high until it detects the incoming wave, once it is detected, it gives you the time of the journey to and from the target. This time is in microseconds so we divided between 1 million to get the time in seconds in the next line. To get the distance to the target, first we multiply the pingTime for the speed of sound, which gives the distance of the full travel, in this case in meters. Last instructions converts the targetDistance from meters to centimeters (cm) multiplying by 100 and to get the correct distance (sensor to the target) we have to divide the between 2, to make things easier we can multiply by 50 as in the code to get the desired value.

For debugging, I print these values on the serial code, but you don’t need to do that. However, we still have to print the result on the LCD screen, to do that in line 43 we place the cursor on the first position of the second row and we print a line of blanks to remove any previous digits, we then go back to the first position of the second row and we print the value of the targetDistance variable and then the units we are using, in my case is cm. We end the code with a delay of a quarter of second between measurements.

As usual, you can find the codes in my GitHub account.