ultrasonic distance meter with lcd display on arduino uno

by:UMeasure     2020-06-17
In this tutorial, we will see HC-
Ultrasonic module SR04.
This module is usually used on robots and toy cars to locate obstacles, but it can also be used to measure distance.
Distance measured by HC
The SR04 will be displayed on our beloved LCD 16x2 display.
If you would like to know how to weld pin heads to this display, please take a look at our tutorial.
Maximum range of HC-
The SR04 is 4 m mm, the shortest distance that can be measured is 2 cm, and the accuracy is 3mm.
When the distance is not enough, we make a piezoelectric speaker ring.
If you want your distance meter to do more accurate measurements, two more things are needed: Unfortunately, when testing, we find that due to lack of energy, the device cannot be powered by a 9v battery (
In terms of the supplied amps).
Therefore, you must use a computer or an AC adapter (
Yes, it\'s not convenient at all, but it\'s just an experiment! )
Shopping list: How does it work?
The system emits a burst of ultrasound to the object under test, and then receives the corresponding echo.
Arduino calculates the distance according to the reflection time according to the following formula: Test distance = (
High level Time × sound speed *)
/2 * the sound speed in the air is 340 m/sHC-
SR04 of HC-
No additional libraries are required for the SR04 module.
All you need to do is connect it to the Arduino after the attached Fritzing diagram or the scheme below: 5 v----
> VccGND----> GNDpin 7 ----> Trigpin 8 ----
> Echo Upload codeNow, download the file ultrasound.
Then double click it.
The Arduino IDE will ask you to save the new sketch to a folder with the name used. ino file.
Save the file to your preferred location.
At the beginning of the sketch, we defined two pins for triggering and Echo: in order to make HC-
SR04 launches 8 ultrasonic waves (40kHz)
Arduino sends a pulse (10us)to the pin 7.
In the setup function, we correctly set them to output and input: the loop function.
SR04 emits microwave to the object: Arduino sets pin 7 to high by sending a 10us pulse to triggerPort.
After this instruction, the sketch waits for 10us before setting pin 7 to low again.
In the first line of the following instructions, Arduino from HR-SR04.
Use the formula distance = (this time is required to calculate the distance
High level Time × sound speed *)
/2 is displayed on the second line.
The last line determines whether the object is too far away and checks if the reflection time is greater than 38 ms.
You can see the output of this sketch on the screen-
Attached to the photo
Of course, we only want our distance meter to be measured when we need it.
We can simply implement this feature by adding a push-button.
So press a bush
Buttons, Arduino and HC-
Make a measurement of SR04.
Check out the attached Fritzing scheme for how to connect push-button.
Keep in mind that this wiring requires a 10k ohm resistor.
The first step is to declare the pin used to connect the button (
We chose pin 10)
: Then, in the setup function, we set this pin to input: the last instruction must be put into the loop function: What does the previous instruction do? When the push-
The button is not pressed, and the logical condition in the loop is true, so the sketch stays in it endlessly and does not execute the next line.
On the contrary, when pushing-
Press the button, the logical condition becomes FALSE, the sketch comes out of the loop, and the following rows are performed for measurement.
As usual, you can download the sketch ready to use.
As you can see, if you test this step, the serial monitor will only press-
Press the button.
Now is the time to connect the LCD display to the Arduino.
As you can see in the introduction, you also need something else to make this circuit: 2N3904 transistor (
Alternatively, you can also choose 2N2222)
, 10k ohm resistance, 10k Ohm linear potentiometer for adjusting contrast.
These components are required to turn on the LCD display when push-is pressed
Button when measuring.
Also, if you want to be told in a timely manner that you are not far from the meter, adding a piezoelectric speaker can be a wise choice.
Note: We will not explain how to connect the LCD display to the Arduino and how to control its back
Because these programs have been explained in step 5 and step 6 of our previous tutorial, respectively.
However, if you are only interested in the connection, please see the Fritzing plan attached.
Note: since the display is now using pin 7, we changed HC-
SR04 moves it to pin 9.
SketchLet adjusts the sketch based on the changes we want to make.
In the first part of the sketch, we have to declare two constants and one variable.
The first constant is the PWM pin used to turn the display on and off-
The light and the second is the frequency of the note that the piezoelectric speaker is not able to play in time from the meter.
Variables are needed to set the following-
The brightness reaches the maximum.
We then include the LiquidCrystal library and initialize the LCD display Library: initialize the setup function to the setup function. We need to declare pin 11 as output and initialize the LCD display (
16 columns, 2 rows)
: We made some changes in the loop function.
First, we modified the loop as shown below: the logical condition is TRUE when the button is not pressed, so the sketch will wait for this loop.
In this case, the first line in the block is back-
Turn off the lights, clear the display on the second line, and turn off the tone generator on the third line. When the push-
Press the button and the logical condition becomes FALSE, and Arduino skips this loop and continues to execute the following line. . . . . .
Turn on the backlight.
Since we no longer use the serial monitor, we have to change the serial function. print to lcd. print.
On the first line, it prints the reflection time and distance in the second line.
If the reflection time is greater than 38 MS, the obstacle is unreachable, the piezoelectric buzzer will play the sound, and the display will display a \"unreachable\" message. Otherwise, the sketch will print the measured distance.
As usual, the sketch is ready for download.
In order to make the equipment easier to use, we put the components on the board of 24x8 cm.
We also use two different breadboards: a miniature breadboard for ultrasonic modules and buttons and a medium-sized breadboard for other components.
In addition, we installed two basic things for more accurate measurement: a simple laser indicator and a tubular mental level.
The last useful thing is a few paper tubes (ours are 7. 5 cm long)
Must be placed in front of the two speakers of the ultrasonic module.
In order to focus the ultrasonic pulses from the ultrasonic module speakers, they are required, otherwise they will be excessively divergent.
In fact, the angle of measurement is 15 degrees according to the data sheet.
During the measurement, we found that pipes are required for \"long distances (
More than 1 m)
And misleading short.
The longest distance we do is 418 cm.
Custom message


APP Umeasure---the must-have home decorating apps for iOS And Android which can connect with Mobile and Laser Distance Meter

Chat Online 编辑模式下无法使用
Chat Online inputting...
Umeasure here! Just in case you leave or we reply later,please leave your email,mobile or Skype. Will get back to you later. Contact us email:measure@measure.hk, skype:measure.zh@outlook.com, mobile/whatsApp/WeChat: 0086 166 7561 7862