How to Measure Distance on the Arduino - Ultimate Guide to the Arduino #34

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] sensors you can use with an Arduino is the ultrasonic rangefinder these cool little sensors can measure the distance to an object they're really useful for obstacle avoidance in robotics projects the ultrasonic Rangefinder I'll be using in this video is the HC sr04 these two metal cylinders here are called transducers transducers take a physical force and convert it into an electrical signal the transducer with the T next to it is the transmitting transducer the transducer with the r next to it is the receiving transducer the transmitting transducer generates high frequency pulses of sound the pulses of sound travel through the air until they hit a solid object the sound pulses get reflected off the object and back to the sensor where they're detected by the receiving transducer small metal device here is a crystal oscillator it sets the timing of the sound pulses this chip is a Max 3232 it controls the sound pulses generated by the transmitting transducer this chip is an lm324 it's an op amp that amplifies the signal detected by the receiving transducer there are pins for VCC and ground then there's a trigger pin the trigger pin is where we'll send the signal to start the measurement and an echo pin the echo pin is what we'll read from to get the distance measurement now let's get a closer look at how these sensors actually measure distance the principle is based on one very simple math formula or rate equals distance divided by time so if we know the speed of a vehicle and how long it was moving we can calculate the distance it traveled we just rearranged the formula to distance equals speed multiplied by time ultrasonic rangefinders use sound to measure distance so the speed in the equation is the speed of sound as it travels through the air if we measure the time it takes for a sound pulse to leave the sensor hidden object and bounce back to the sensor we can calculate the distance to the object however we only want to measure the distance to the object and not the distance of the path the sound pulse took so we actually divide the time in half we will of course also need to know the speed of sound if sound and error changes with temperature and humidity at zero degrees Celsius and zero percent humidity the speed of sound is a constant 331.4 meters per second to factor in the effects of temperature and humidity we can use this equation to calculate the speed of sound B and H are the temperature in degrees Celsius and humidity in percent relative humidity so if the temperature is 20 degrees Celsius and the relative humidity is 50 percent the speed of sound would be 344.02 meters per second you'll get more accurate distance measurements if you change the temperature and humidity values to your local conditions you could also connect the dht-11 to the Arduino for real-time humidity and temperature readings to start a distance measurement we need to send a high signal to the trigger pin for 10 microseconds this will make the rangefinder emit eight pulses of ultrasonic sound at a frequency of 40 kilohertz from the transmitting transducer it also starts a timer to see how long it takes the sound pulses to return to the receiving transducer when the pulses hit the receiving transducer the echo pin outputs a high voltage signal the length of this high voltage signal equals the total time it took the eight pulses to travel from the transmitting transducer and back to the receiving transducer let's connect the ultrasonic Rangefinder to the Arduino first then we'll see how to program it the connections are simple connect VCC to 5 volts and ground to ground the trigger pin can be connected to Arduino pin 10. and the echo pin can connect to pin 13. we're going to look at a sketch that outputs the distance measurements to the serial monitor the first thing we do is declare a couple pin variables one for the trigger pin which is connected to Arduino pin 10. and one for the echo pin which is connected to pin 13. then we initialize the serial monitor and set the pin modes of the trigger pin and Echo pin we're going to send a start signal to the trigger pin so that's set as an output then we'll read the signal from the echo pin so that's set as an input in the loop the first thing we do is make sure that the trigger pin is low we do that by writing the trigger pin low followed by a delay of two microseconds so now that we're sure the trigger pin is low we can send the 10 microsecond long start signal so we digital write the trigger pin High then delay for 10 microseconds then we write the trigger pin low that will create our 10 microsecond long high signal that will trigger the rangefinder to emit the 8 Sonic pulses now we need to read the echo pin and see how long the signal stays High to do that we use a built-in function called pulse in the pulse in function is similar to digital read it listens to the PIN but instead of returning the high or low state of the PIN it Returns the length of time the pin was high or low it takes two parameters the first is the PIN it needs to read the second is the type of signal it needs to measure so here we're reading the echo pin and measuring the length of the high pulse the value will be the time in microseconds we store that in a float variable called duration here's where we calculate the distance which we're going to store in another float variable called distance we take the duration measurement from the echo pin and divide it by two since we only want to measure the distance to the object so this is our time variable from the distance formula then we multiply that by the speed of sound which we calculated as 344 meters per second at 20 degrees Celsius and 50 humidity but that speed of sound was in units of meters per second the output of the pulse in function is in microseconds and also I want to display the distance in centimeters not meters so we need to convert the speed of sound from meters per second to centimeters per microsecond to do that we just need to multiply the meters per second value by a couple conversion factors to convert seconds to microseconds we can multiply by 1 over 1 million since there are 1 million microseconds in one second then to convert meters to centimeters we multiply by 100 over one since they are 100 centimeters in one meter now we can eliminate the units we don't want so we're left with 344 times 100 centimeters divided by 1 million which equals 0.0344 centimeters per microsecond and that's what we'll use for the speed variable in the distance equation an object is closer than two centimeters or farther than 400 centimeters the reading from the rangefinder may be inaccurate so let's make the Arduino show an error message if the object is outside that range to do that we can use an if else statement if distance is greater than or equal to 400 or distance is less than or equal to two the sensor is out of range so we serial print distance equals and out of range if the distance measurement is within that range the else block gets executed if that happens we serial print the distance equals text followed by the distance variable we print some text that says cm to indicate the distances in centimeters then we delay for 500 milliseconds that's just to make sure we don't read the sensor too fast which could cause the trigger signal to be sent before the echo signal is returned then we delay for another 500 milliseconds just to slow down the output of the readings and make them easier to read okay now let's see what this looks like so here I have the rangefinder set up and here's a ruler to show you the true distance when I place a notebook at 15 centimeters the rangefinder measures 15.19 15.2 when I put the notebook at 25 centimeters the rangefinder measures 24.96 25.47 so it's pretty accurate you probably get even better accuracy by refining the speed of sound with the correct temperature and humidity in the next video we're going to learn how to detect motion with PIR sensors or infrared motion sensors [Music] the three in one smart car and iot learning kit through sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or fall on the line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I have lots of fun building all the projects in it so click the link in the description below to order the kit from some founder
Info
Channel: Circuit Basics
Views: 2,443
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: hRR_4Yfh5rY
Channel Id: undefined
Length: 12min 37sec (757 seconds)
Published: Wed Jun 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.