0:00
Did you know that accurate distance measurement can revolutionize your robotics and automation projects
0:08
In this video, we're exploring the HCSR-04 Ultrasonic Sensor and its versatile applications
0:14
with Arduino. If you're building a robot that avoids obstacles or automating systems in your home, accurate
0:19
distance measurement is key, and today, we'll show you exactly how to master it with the help
0:24
of an I-2c LCD display. The HCSR-04 Ultrasonic Sensor consists of two main components, a transmitter that sends out
0:32
ultrasonic waves and a receiver that listens for the echo when those waves bounce back
0:36
from an object. It measures the time taken for the sound to travel to the object and back, using that to calculate
0:41
the distance. Before we dive into the coding, let's start by setting up the hardware
0:46
Here's a list of what you'll need. An Arduino board. We're using the Arduino Uno, but other models will work too
0:51
The HCSR-04 Ultrasonic sensor, an I2CLCD, which is a 162,000. by two character display. And of course, some jumper wires to connect everything. Now let's get to the wiring
1:02
Start by connecting the HCSR-0-4 ultrasonic sensor. It has four pins. VCC connects to the Arduino's 5V pin
1:10
GND goes to the GND pin. Trigger connects to digital pin 6 on the Arduino
1:14
Echo goes to digital pin 7. Next, we connect the I2C LCD. This display only uses two communication pins
1:21
which makes it super easy to connect. The SDA pin of the LCD goes to the A4 pin on the Arduino
1:26
the SCL pin of the LCD goes to the A5 pin Lastly connect the VCC and GND pins of the LCD to the corresponding 5 V and GND pins on the Arduino
1:58
Now that the wiring is complete, we're ready to power up the Arduino and move on to the coding
2:21
We start by including two important libraries at the top. Wire.h and Liquid Crystal underscore I2c.H. These are essential
2:28
for controlling the I2C LCD. To install them, you need to download it first
2:33
I'll be posting the link to these libraries and how to install them in the description. Next, we create an object called LCD for the I2C LCD
2:41
Here, we define the LCD address and the pins used for communication. The keyword positive refers to the backlight configuration
2:47
for the LCD. Moving on to the ultrasonic sensor, we declare two pins, trig pin for the trigger
2:52
and echo pin for the echo. The trig pin will be used to send out ultrasonic pulses
2:56
while the echo pin will receive the signal that bounces back, allowing us to measure the distance
3:01
We also declare two floating point variables distance and dist underscore inches These will store the measured distance in both centimeters and inches respectively Now in the setup function we initialize the LCD display with 16 columns and two rows using the LCD
3:16
This ensures the display is ready to show text in two rows
3:20
Next, we define the trig pin as an output since it will send out the ultrasonic pulses
3:24
and the echo pin as an input since it will receive the bounce signal
3:28
Then we print the word distance on the first line of the LCD using LCD
3:32
print distance. This will act as a label for our distance measurement. We also move the cursor to the
3:36
second line with LCD. Set cursor 0 1 and print inches to show that we'll be displaying the
3:41
distance in inches. Now that we've set up the pins and initialize the LCD, let's take a closer
3:46
look at the loop function. This is where the real action happens. First, we need to trigger the
3:51
ultrasonic sensor. The trig pin sends out ultrasonic pulses to detect objects in front of it. To ensure
3:57
the sensor is ready, we start by setting the trig pin to low for a brief moment. This creates a
4:01
a stable starting point for the signal. After that, we set the trig pin to high for just 10
4:05
microseconds to send out a pulse. This pulse travels through the air, bounces off any object
4:10
it hits, and then returns to the sensor. After the pulse is sent, we set the trig pin back
4:15
to low to stop sending the signal. Once the pulse is sent, the sensor waits to hear the echo
4:20
of that sound bouncing back. The time it takes for the sound to travel to the object and back
4:24
is measured by the echo pin. This is done using the pulse and function, which measures the duration
4:29
of the echo in microseconds. Next, we convert that time into a distance
4:33
The sensor gives us the time in microseconds and to convert that into a distance we multiply it by 0 This constant converts the time to a distance in centimeters If we want the distance in inches we further multiply the result by 39
4:49
since there are 39.37 inches in a meter. Now that we have the distance in inches, it's time to display it on the LCD
4:58
Finally, we add a delay of 500 milliseconds to give the sensor enough time between readings
5:02
ensuring stable and accurate measurements. Now that we've walked through the code, it's time for the fun part, demonstrating how this setup works in real time
5:10
Load the code and see how it detects objects at various distances
5:14
First, let's upload the code to the Arduino. As you can see, the LCD immediately displays distance inches, which sets the stage for our real-time measurements
5:28
Let's start by placing this object in front of the sensor. As I move it closer, notice how the distance on the LCD changes dynamically
5:34
dynamically. But watch what happens when I move it further away. Pretty neat, right? The sensor is sending
5:42
out ultrasonic pulses, measuring the time it takes for the echo to bounce back, and calculating the
5:46
distance in real time. As the object moves away, the distance displayed on the LCD gets larger
5:52
showing how the system adjusts to changes in the environment. This system is incredibly useful
5:56
for robotics projects, where accurate distance detection is crucial. If you found this guide helpful
6:01
be sure to give us a thumbs up. Comment below and share your own distance measurement projects. We'd love to see what you're building. And don't forget to
6:08
subscribe to the channel for more hands-on tutorials on Arduino and sensors. See you in the next video