HC-SR04 Ultrasonic Distance Sensor and Arduino (Lesson #9)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn how to use the HC sro4 ultrasonic distance sensor with an Arduino note that the hcsro4 is very similar to the Ping another popular ultrasonic distance sensor that is used with the Arduino the main difference is that the ping only has three pins it uses a single pin for the trigger and return pulses the hcs-ro4 has four pins and uses separate pins for trigger and the return pulse so we have a separate video covering how to use the ping you can find our Arduino tutorial playlist Linked In the description of this video here we will focus on the hcsro4 you can see that it has four pins VCC for power trigger Echo and ground to connect the hcs-ro4 to your Arduino you will need male to female jumper wires if you were just testing the sensor and don't have any other parts in your circuit you can connect it directly to the Arduino instead of using a breadboard I have the VCC pin going to 5 volts the ground pin going to ground the trigger pin going to pin 7 and the echo pin go into Arduino pin 8. let's switch over to the computer and take a look at the sensor's data sheet to briefly talk about how it works but again if you have already watched our video about the ping sensor the concept for operation here is very similar the sensor works by emitting bursts of ultrasonic sound and measuring how long they take to reflect back to the sensor this is the same principle used by animals like bats for echolocation if we look at the timing diagram we see that you need to send a trigger pulse to the sensor which then tells it to emit that burst of ultrasonic sound it listens for the sound to return and then it emits an echo or return pulse the length of which is proportional to how long it took this sound to reflect back to the sensor so using the speed of sound in air and knowing that there is a factor of two because the sound had to go out and then return to the sensor you can use that information to calculate the distance to the object to program the sensor where you're actually going to start with the built-in Arduino example program for the Ping sensor and just make a few simple modifications you can find this program by going to file examples sensors ping once you've opened it we can take a quick look through and see that again the Ping sensor uses a single pin as both an output for the trigger pulse and then it switches that PIN to an input to measure the return pulse the hcsro4 uses two different pins so we are going to declare separate variables for those pins so I'm going to write const int Echo pin equals eight because I'm using pin eight and I'm going to change the name of this to trig pin just so it matches the labels on the sensor then in my setup function I'm going to declare those pins as inputs and outputs using the pin mode command so pin mode trig pin comma output because that is my output pin for the trigger pulse and then pin mode Echo pin input because that is my input pin for the return pulse down here in our Loop function I can delete these two pin mode commands because they are no longer needed again the Ping sensor is switching the pin back and forth between output and input with each measurement the hdsr04 has two dedicated pins that we only need to set up once in the setup function so I'm going to delete this pin mode command delete this pin mode command but then make sure I change the variable names to match what I used before so I'm going to use the trig pin variable to send the trigger pulse and then we are going to use the pulse in function to measure the length of the return pulse on what I am calling Echo pin finally if you remember you might have seen this in the data sheet the hcsr04 expects a 10 microsecond trigger pulse so I'm going to change this variable value from 5 to 10 for the trigger pulse and now I should be all set so you see just from changing a few lines of code we have modified the Ping program to use two pins instead of one everything else can remain the same like the conversion from that pulse duration in microseconds to a distance in either inches or centimeters if you upload the code and open the serial Monitor and move your hand back and forth in front of the sensor you should see the distance readings change now if you are trying to decide between the Ping and the hcsro4 note that the hcs-ro4 is much cheaper but does not always seem to work as well for smaller objects for example as I move this large flat piece of cardboard back and forth in front of the sensor you can see that this circuit works pretty well and the LEDs light up depending on how far away the cardboard is however if I move my hand back and forth in front of the sensor sometimes it seems a little more glitchy and some of the LEDs are lighting up when they shouldn't if you compare this to the performance of the Ping sensor in our other video you will see that the performance of the LEDs is more consistent so whether the more expensive sensor is worth it is really up to you your budget and what you need to measure in your project
Info
Channel: Science Buddies
Views: 79,150
Rating: undefined out of 5
Keywords: ARDUINO, HC-SR04, HCSR04, ultrasonic, sonar
Id: n-gJ00GTsNg
Channel Id: undefined
Length: 5min 28sec (328 seconds)
Published: Fri Nov 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.