Using the HC-SR04 Ultrasonic Distance Sensor with Arduino - Everything you need to know!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be looking at an essential robotics component the HC sr0 for ultrasonic sensor after we find out how it works we'll hook it up to an Arduino and put it through its paces on the workbench well look at a couple of different ways you can code for this versatile device and we'll even add an additional component to our circuit to make our results far more accurate so there's a lot to cover today so grab your sensors and welcome to the workshop [Music] hey welcome to the workshop today we're going to be working with this little bug-eyed monster it's an H csr zero 4 ultrasonic distance sensor now as its name implies this little device uses ultrasonic sound in order to measure the distance between itself and the nearest solid object and as a result this has become a staple in robotics projects because the last thing we want is for our little robots to be driving into walls and things now we're going to look at a couple of different ways that we can code for this sensor and one of them is using a library called new pings so if you're new to the Arduino and haven't used a library before or you're going to learn something we'll put it on the workbench see how accurate it is we'll actually look at a couple of different ways we can hook it up to an Arduino and we'll even add an extra component to it to make it a lot more accurate than it is out of the box so there's a lot to cover today so let's start by looking at how this device actually works each csr 0/4 consists of two ultrasonic transducers one is used as a transmitter and the other one is used as a receiver now a normal operation the transmitter sends out a series of ultrasonic pulses these are not picked up by the receiver despite its proximity because ultrasonic signals are very directional however if a surface is in front of the device it will reflect the signal back to the receiver the time delay between the transmission and receiving the signal is used to calculate the distance so a longer time delay will be a longer distance and a shorter time delay indicates a shorter distance so how does this device actually hooked up well the HC sr0 4 has 4 pins on it VCC which is the 5 volt power connection trig which is the trigger pin and this is the input to the device echo the echo pin is the output from the device and finally a ground pin now it to work the device the trigger pin is sent the 5 volt 10 micro second pulse the device then transmits 8 ultrasonic pulses either at 40 kilohertz each the echo pin will output a pulse between 100 and 50 microseconds to 25 milliseconds and that pulse width is used to calculate the distance it'll output a pulse of 38 milliseconds if there is no object detected now let's look at the timing note that this is not the scale first we will send the trigger pin attend microsecond pulse and we transmit 8 ultrasonic pulses now in this case there is no object so the echo is 38 milliseconds however if we put an object in front of the device we'll see the difference now again we'll send a trigger pulse of 10 microseconds and send out our ultrasonic pulses only now the signal has been reflected back and comes back with a timing of 500 microseconds we can use this now to determine the distance ultrasonic signals travel at the speed of sound at 20 degrees Celsius the speed of sound is 340 3 meters per second now remember the time we're measuring with the HCS are 0 4 is for a return trip so we'll need to divide this in half to calculate the actual distance take a look at the variables that we'll be using in our equation delta T is the time delay C is the speed of sound and D is the distance we'll be measuring and as a reminder again the speed of sound is 340 3 meters per second now we're going to be measuring in centimeters so let's take the units to make it easier 343 m/s is 0.03 4 3 centimeters per second another way of looking at this is that sound takes twenty nine point one five four microseconds a travel a centimeter and you can consider that to be the pace of sound now if you're not familiar with the metric system 2.54 centimeters equals one inch so you can just convert the result at the end if you need to now because we know both the speed of sound and the pace of sound there are two different ways we can calculate the result our first method will use the speed of sound and so the formula is the distance equals the time divided by two multiplied by the speed of sound now let's put in the values we just got so in our case it's five hundred divided by two times zero point zero three four three which equals eight point five seven five centimeters now the second method involves the pace of sound and in this case the formula is distance is the time divided by two divided by twenty nine point one five four so let's plug in our figures again and we'll come out to the same result the distance is eight point five seven five centimeters for those of you who are metrically impaired 8.57 five centimeters is the same as three point three seven six inches all right now we've seen how the ultrasonic sensor functions let's start to work with it before we do though I want to show you the little test arrangement I've made on my workbench to test the accuracy of the sensor what I've done is I've taken an H CSR 0-4 and mounted it at the end of this long stick I've put it on to an angle bracket and I'm actually holding it on just with Velcro I've aligned it with this meter stick that I've got here this is a one meter stick and it will let me measure the distance between itself and the sensors of the ACS are 0-4 I've lined up the end of the stick with the front of the unit here now I'm going to use this piece of wood simply to reflect the ultrasonic signal back to the device and we can see how accurate it actually is now if you're wondering about all this mousepad business I have on here that's simply so I can place an Arduino and a solderless breadboard beside the device and do my wiring so now that you've seen my test setup let's actually hook this up and start coding with an Arduino and check out how accurate it is hooking up the HC SR 0 4 to the Arduino is actually pretty simple I've connected the 5 volts of the ACS are 0-4 to the Arduino 5 volt output I connected the trigger pin to the Arduino digital pin number 10 I connected the echo pin to Arduino pin 13 I have connected the ground to the arduino z' ground now you can use two different io pins if you wish all you'll have to do is change the sketch so let's take a look at the first sketch that will be running with the HC sr 0 for now we start off by defining the connections that we've made to detroit your pin in the echo pin so I defined trig pin has 10 number 10 the connection to the trigger on the ACS are 0-4 and the echo pin defined is pin number 13 now if you wish you can use different digit io pins on your Arduino has change the 10 and the 13 to match the way that you've wired your sensor then we define a couple of floats now floats or floating point variables these are numbers that can contain decimal points so I've got two of them one for duration that's going to define the duration of the signal we get back from the HC s are 0-4 and the other one for distance and this is going to be our final result in centimeters in this case now we'll start in the setup we're going to be using arduino serial monitor to monitor the results so we'll set up the serial monitor at 9600 baud and then we'll define our two pins our trigger pin is an output because we are sending pulses out from the arduino to the device whereas our echo pin is the input back from the device now that we've defined that let's actually go into the loop now if you recall the first thing we have to do with the HCS are 0 for is send it a 10 microsecond pulse and we're going to do this manually well use a digital write to the trigger pin and send it low and we're sending it low for 2 microseconds now that value is not particularly critical we just want to make sure we start off in a low state then after that we're going to send the trigger pin high for 10 microseconds this is the high part of our 10 microsecond pulse and then we'll send it low again so this in effect creates a 10 microsecond pulse to the trigger pin now we're going to measure the response we get back so our duration is going to use Arduino pulse in command which very conveniently measures the duration of a pulse which is exactly what we want now it has two parameters the pin that we're monitoring which is the echo pin the pin that's connected to the echo out from the HCS are 0 4 and high what that means is that we are measuring the amount of time that the pulse stays high because the pulse in command can equally be used for measuring low pulses but in our case we want to know how long it stays high now that we've got the duration we are going to calculate it using the speed of sound we use 343 meters second as our speed of sound and so we're going to calculate the distance with that now remember the distance that we're measuring is going to be half of the results because we actually have to send the signal out and wait for it to bounce back so we're going to divide the duration by 2 and then multiply it by our speed of sound and then after that we simply send the results to the serial monitor now the sensor has a maximum range of 400 centimeters and a minimum range of 2 centimeters so any readings outside of that range are invalid so we're just going to print out of range otherwise we're just going to print the distance in centimeters we're going to put a couple of delays in there and we're going to go and do the loop over and over again so this should constantly distri back the distance between the sensor and the device that's reflecting the signal so now let's see how it works okay let's put our first sketch of the test I've got my reflecting surface so let's start the serial monitor and see how accurate this actually is so you start the serial monitor just up in Tools serial monitor and it's reading some readings back now these are probably spurious readings from items on my workbench so let's put this down at the let's say 30 centimeter mark so here I am at 30 centimeters and we're getting some readings there was a 30-point o1 in there a lot of it has to do with how steady I hold this thing so they fluctuate a bit but it's very close to 30 let's move it down a little but let's go down to 10 centimeters now here at 10 centimeters which is roughly 4 inches again there's a nine point nine one nine point nine eight it seems a little low but it's close at 20 centimeters then 19.1 619 point five seven whew very close but it seems a little bit off and let's back up to maybe 50 centimeters now and we're a little low but not terrible so it's actually giving a pretty good ballpark range but it can be out it seems by at least a half a centimeter in some particular cases now I'm going to show you some tricks you can use to increase the accuracy of this but before I do I want to show you another way that you can write a sketch for this using a library called new ping libraries are additional functions that you can add to your Arduino and the new ping library has been specifically made to have functions for ultrasonic sensors like the ACS are 0-4 so let's take a look at that now so here's our sketch rewritten to use the new ping library now when you use a library you need to include the library so use an include statement to include new PIN dot H dot H is the extension for a library after that we'll define a few variables we define trigger pin as 10/10 and echo pin is pin 13 again if you've used different pins on your Arduino simply change the ten of 13 to match the ones that you've used and a third variable called max distance now the new pin library is actually usable by a number of different ultrasonic sensors some of which have different capabilities in the HC sr0 for the ACS are 0-4 has a maximum distance of 400 centimeters which is why I've used 400 here but for a different sensor you might want a different value calling new ping is very very simple we just do new pings sonar and then the trigger pin the echo pin and max distance that's all there is to it so it's very simple to call the new pin library then we define a float of distance we don't need duration in this case because new ping is going to handle all of this internally our setup is very simple we just start the serial monitor nothing else and then we go into the loop and the loop is also very very simple our distance simply equals sonar ping underscore cm and this will give us our distance in centimeters new ping can also give you the distance in inches if you wish and then once again we send results of the serial monitor before we send this up to our Arduino let's make sure that it compiles correctly I always like to do that we do that with a little check mark here which will accept for code errors and other things so let's cap it and see what happens and as you can see bad things happened there's a problem with our sketch over here and the problem is we've included the library but this library has not actually been installed in our arduino ide and so we need to do this what we will do is we will go and get the library now there are a couple of sources for the new pin library you can get it off of bitbucket or you can get it off of github I'm going to take this off of github so I have the links by the way at the bottom of the video and also got the links on the article on the dural bot workshop website so you can get the link for that so I'm going to go to github to the new pin and I'm going to download a zip file of the library the download the zip file and in this case is saving in my downloads directory and then I'm going to go back to my Arduino IDE now what I need to do is go under sketch and include library and add if there is adds it file and when going to my downloads and there's new Pig so let's add that the library has been added to my library menu a very little notice here so let's try compiling this again and see what happens and that looks a bit better it seems to compile so let's send this up to our Arduino and see how the new pin library functions ok I've got my reflecting surface I'm going to give the new ping a try now let's first of all start off at let's say 30 centimeters and looking on the 3rd and the serial monitor I've got 30 centimeters right on the nose isn't that great let's move down to 20 centimeters gained 20 centimeters but you might be noticing something here let's go up to 40 centimeters and great we're getting a reading of 40 oh no we're getting rid of 39 oh no it's 40 can you see what I'm talking about the new ping library is only resolving down to a one centimeter resolution and as it turns out this is by design the developer of the library felt that since most of these sensors have an accuracy of plus or minus half a centimeter that there is no reason to include fractional centimeters however this may or may not be true in your application in some applications like a robot that you're trying to keep from driving into a wall a centimeter resolution is probably fine if you're building a measurement device though it's completely out of the question also keep in mind that new pin can be run in imperial mode and Imperial mode the resolution will be within an inch and an inch is quite a variance so you might think this new pin library itself is not really that useful if you want to do something accurate but there actually is a way that we can get more accuracy from the new pin library so here's our new ping sketch rewritten to calculate duration instead of distance what we'll do is we'll use a new pin library to obtain the duration of the signal and then use the same mathematics we did before to calculate the distance so this is really just an amalgamation of the first and second sketches that we looked at today the first thing we do is we start off the same as the last sketch we include the new pin library and we set up the same variables trigger panicle 10 and maximum distance then we'll call new pin in the same fashion by passing it to trigger connect open and maximum distance now we define two floats as we did in the original sketch because once again we are going to use duration to calculate distance in the setup routine will start the serial monitor and then we get into the loop and this is where we see the one difference in the new pin command you remember in the last sketch that sonar dot ping underscore cm returned the value of the distance and sent to me but if you sonar dot ping instead you will get the duration back and then from then it's simply the same thing that we did in our first sketch we've used the duration divided it by two and multiplied it by the speed of sound and then we send the results out to the serial monitor so let's see how this has improved our new ping sketch okay I've got my reflecting surface and I'm ready to test our new ping sketch using duration instead of distance so we'll start off and I'm going to place this at the twenty centimeter mark and 19.7 for nineteen point eight one it's close 40 centimeter mark it really depends on hold this because I'm probably not holding at that steady but it looks like I'm plus or minus a centimeter on that one there ten centimeters nine point eight six nine point seven nine nine point nine three so we're quite close there and let's go all the way back to sixty centimeters over here and 59 point two five etc so as you can see we brought back the decimal points but we still haven't got complete accuracy now as I promised earlier I will show you a way that you can improve upon that accuracy but before I do there are some other functions in a new pin library that I'd like to take a look at so the first one is called iterations so here is yet another version of our new ping demonstration in this particular case we're going to use iterations to iterate means to go over something more than once and that's exactly what the new pin library will do when you use the iteration function it'll actually go and take measurements more than once by default it'll do it five times but you can actually ask it to do it as many times as you want it'll take those readings it will throw away any invalid readings and then take the average of the other readings and this will improve the accuracy slightly and I'll also take care of situations like we have with my board which is a bit shaky and all that it should smooth out the readings a bit so the sketch is identical to the last sketch with one particular exception and that is we have an interpreter that I defined as the iterations and I've defined it as five in iterations which is the default anyway you can change this an experiment if you want to see the results and then the duration is sonar ping median and then we Moulton in here so this is really the only difference between this sketch and the last sketch and then once again of course we calculate the distance based on the duration and display it on the serial monitor so let's take a peek at this sketch and see if it's an improvement upon the last one okay let's go and test our iteration sketch and see if it has any improvement over the last one now we're going to put this down at about the 20 centimeter mark and I'm getting readings like nineteen point seven for nineteen point three three one thing I do notice is that they're not jumping around as much while I'm shaking the board so the iteration seem to be smoothing things out a bit let's go over to forty thirty nine point eight four thirty nine point three six seven seven it's close but not exact let's go a little closer down at ten perhaps ten on the button over here that's nice nine point eight six pretty darn close and finally maybe up at about fifty forty nine point two four forty nine point eight nine we're close now as you can see our readings are close but they're not completely accurate and there are a couple of reasons for that some of them of course being the way that I'm just holding my board but there's another source of inaccuracy in our circuit and it has nothing to do with the HC SR 0 4 it actually has to do with the speed of sound okay so what's wrong with the speed of sound well the thing is sound travels at different speeds in different mediums it also travels at different speeds at different ten Cheers for an example and this is really going back into my youth over here when I was a teenager I went to a Pink Floyd concert at a very large stadium and I was sitting at the back of the stadium now every time Roger Waters the bassist for Pink Floyd would pluck his bass guitar our seats would vibrate and then we'd hear the sound it was rather unnerving to say the least and that's because sound travels faster in cement than it does in air as it turns out sound actually travels at different speeds in air as well and it depends upon things like humidity air pressure and especially temperature at zero degrees Celsius and zero percent humidity sound travels at 331 meters per second it travels faster as a temperature in the humidity rise now here's the formula we're going to use C represents a speed of sound to use the temperature and eight is humidity and so here's our whopping large formula so as you can see temperature factors in much more than humidity now let's just keep our equation up here and calculate what the speed of sound would be at 25 degrees Celsius and 50% humidity when we do the calculation we come up with 347 point one seven liters per second which is faster than the 343 m/s I've been using so by incorporating the speed of sound into our equation at 343 m/s we're inducing an inaccuracy because that's really the speed of sound at about 20 degrees Celsius with low humidity so the way to actually link our sensors more accurate is to measure the temperature and the humidity and factor that into the speed of sound and so we're going to do exactly that now in order to factor in temperature and humidity into the equation I've added another sensor to our circuit now this is a dht22 temperature and humidity sensor you may be familiar with this device or with one of its thousands like the dht11 the difference between the dht22 and the dht11 other than the fact that the 22 is a bit more expensive is that the 22 is more accurate it's accurate down to point one of a degrees Celsius where the dht11 is accurate down to one degree Celsius it also spans a wider temperature range although in our application that's really not that important because we're indoors if you don't have a dht22 though you can apply a dht11 and get similar results so let's take a look at how I've wired the dht22 into our circuit now we're going to start off by keeping the HC SR 0 4 wired up but we'll ignore the wiring for that right now and concentrate on the dht22 I've connected the 5 volts from the Arduino to the VCC pin on the dht22 next I've connected pin 7 of the Arduino to the data pin on the dht22 now you could use a different pin if you wish so just have to change the sketch accordingly the third pin of the dht22 is actually not connected to anything and is left alone and finally the ground connection is connected to the Arduino ground so now that we've connected a dht22 up to our circuit let's take a look at the sketch we're going to need to run before we do though I want to run a quick sketch just to make sure our dht22 is actually functioning let's take a look at the code that we'll be using to test or dht22 temperature and humidity sensor now we are going to need to install a couple of libraries to use this code although we'll only be calling one library from our code that library in turn is dependent upon another one so both need to be installed now unlike the new pin library we are going to install these libraries directly within the arduino ide and we will do that by using the library manager so go up into sketch and go into include library and pick manage libraries and this will bring up your library manager now this is a list of libraries available to your arduino ide but not necessarily installed and there are two particular ones where after the first is the Adafruit am two three one five library so filter the search by typing in am two three one five and this brings up the Adafruit am two three one five library click the more info link and it will open up a button on this side click the install button and the library is installed very simple but as I said this library is dependent upon another library called the Adafruit unified sensor library so let's filter by a unified sensor and this brings up the list of libraries go down to the bottom and right near the bottom second-to-last on mine is the Adafruit unified Central Library again click the more info and click install and this will install that library now let's look at the code so the first thing we're going to do is we're going to call that first library that we installed so we include dih dih remember this library in turn will call the other library so we don't need to declare it in the code then after that we'll define a couple of constants the first is DHT pin this is where the data pin of the dht22 is connected to in my case i've connected to arduino pin 7 now if you've chosen another arduino digital i/o pin to connect to just change the number accordingly next we need to define the type of sensor because this library is capable of driving a number of temperature and humidity sensors we're using a dht22 so we've defined the constant DHT type with a value of dht22 which by the way is also known as an AME to 302 they are the same part next we have to initialize a sensor and we do that with this line we pass the DHT pin and DHT type to it and we'll define a couple of variables one that stores our humidity value and the next one that stores our temperature value and we're making these floats because we want the decimal value then after that we go into our setup once again we're going to be using our serial monitor to monitor the results so we start the serial monitor at 9600 baud and then we initialize the DHT sensor with a DHT begin and then it's into the loop now the DHT sensors need a little time to stabilize so I've added the time delay at the beginning of two seconds now in actual life two seconds may be a little long and you can experiment by reducing this time delay if you wish then after that we simply read the dht22 and it's quite easy to do DHT dot read humidity will get our humidity value which we've assigned to this variable and read temperature we'll get the temperature value which we've assigned to this variable and then we simply print these out on the serial monitor and repeat the loop so every two seconds we should be getting a temperature and humidity value so let's take a look and see if this is actually working on your tools we will go to our serial monitor and we're getting a temperature and humidity value by the way my serial monitor is a little bug where the first line often doesn't do a carriage return that's just normal for me so as you can see the temperature in the workshop is a little on the warm side but not too bad and it's 51.5% humidity so that will certainly factor in to our speed of sound calculation so here's the sketch we'll be using to integrate temperature and humidity into our speed of sound readings so we can get more accuracy from your HCS are 0-4 and it's really an amalgamation of the sketches that we've seen before so we're going to include the dhp library for the DHT sensor and we're going to include the new pin library as we did earlier and then we're going to define a number of constants again you've seen all of these before the DHT pin is 7 so that's the data pin for the DHT 22 th key typed is dht22 the trigger pin for our ultrasonic answer is connected to ten ten of the Arduino the echo pin to pin 13 and the maximum distance we set the four hundred centimeters so we initialize the ultrasonic sensor in the same fashion we have before and then define a number of variables again most of these you've seen before a variable to store the humidity and the temperature value another one to would store the death duration another one for the distance now this one's new this is the speed of sound in meters per second and this one is the speed of sound in centimeters per microsecond which is actually the value that we find more useful to calculator distance and this is the number of iterations that we're going to be using on the new ping because we're going to use the iteration mode on that to stabilize their readings again we'll initialize a DHT sensor will setup the serial monitor in our setup and initialize the sensor over here okay now we're going to go down to the loop now we've got this time delay again for the dht22 again you could experiment with this you might not have to give it a full two seconds because it's going to delay their readings a bit and then we read the humidity and temperature value from our sensors now here's where we do the calculations so the speed of sound as you recall is this number three thirty one point four multiplied by zero point six oh six times the temperature plus zero point zero one to four times the humidity and that will give us the actual value for the speed of sound at this particular temperature and humidity then we'll convert that to centimeters per microsecond by just dividing everything by 10,000 and now our duration as we did before with the new pin library our duration is just sonar pinging median iterations so we're going to go five iterations and get the average duration now we're going to calculate the distance the same as we have before the distance is the duration which we're dividing by two because we're making a round-trip and multiplied by the speed of sound centimetres per microsecond and finally we just send the results of the serial monitor so let's give this a test and see if it's a bit more accurate now that we factored temperature and humidity into the equation all right so it's time to test out our new and improved sket factoring in temperature and humidity so let's put this down at the 40 centimeter mark to start I'm going to try to get this pretty accurate thirty-nine point nine three thirty nine point eight six forty point three four again some of this is because of course I'm not that steady 39.99 very close okay down at the 50 mark now forty nine nine five fifty point two nine we seem to be within a quarter of a centimeter which is not too bad at all here's the twenty centimeter mark 2002-2009 it does indeed seem to be more accurate now there's one other way that you can make your device even more accurate that I wanted to mention I'm not going to demonstrate it here it's just basically in theory it is quite possible that I may have the head of this HCS are zero for not exactly lined up with the beginning of the ruler I tried to do that and I actually tried to line my ruler up with the outside of the transducer surface rather than the grading on the front of it so it's possible that by moving the sensor back and forth just a little bit I might even get some increased accuracy and you could do that mechanically or if you found out that your readings were always off by let's say a quarter of a centimeter in the same direction simply add that at the end to compensate for any misalignment you have but as you can see by adding temperature and humidity into the speed of sound equation we have greatly improved the accuracy of our ACS are zero for now there's a couple of other tricks with the HC SR 0 for I want to show you and one of them that I want to show you now is how you can actually eliminate one of the wires that we're using to connect it to the Arduino right now we're using both the echo pin and the trigger pin but there are ultrasonic sensors that only have one pin out and the actual pin is used for both an input and an output it's used to accept the trigger pulse and to deliver back the echo pulse and you can run the HC SR 0 4 in 3 wire mode as well and let me show you that right now in order to run the HD SR 0 4 in 3 wire mode I've simply taken both the echo and trigger pins and connected them both to pin 10 of my Arduino again you could use any pin you want now let's take a look at what I need to do in the code in order to account for that change it's very simple when I use the new pin library all I've done is I set both my trigger pin and my echo pin to point the pen 10 the rest of the sketch is absolutely identical ok let's just give this a quick test to make certain it works just as well in 3 wire mode let's put it down at 20 centimeters 20.5 120 point / 3 pretty good let's let's go to 36 centimeters 36 point 1 0 36 point 3 1 it does indeed seem that it is working just as well in 3 wire mode as a dozen for a wire mode and the beauty here of course is that saves one of the i/o pins on my Arduino and this can come in very handy when you're using more than one HC sr0 for sensor which you may indeed want to do in some robotics projects you may want a sensor in the front during the back in fact you can connect the number of sensors up and the new pin library makes this very easy so now let's take a look at a circuit that has more than one ACS are 0-4 so as I already have an H CS are 0-4 attached in 3 wire mode I decided to attach my second sensor in 3 wire mode as well you have to do this of course you could attach them both in the conventional fashion with a separate trigger and echo connection and they will work just fine but here's how I hooked up my sensor I took the 5 volts from the Arduino and attach it to the VCC on the HC SR 0 4 I took both the trigger and the echo pins and tied them together because I'm working in 3 wire mode and then I attach these to pin number 5 of my Arduino now of course you could use a different pin number if you just change the sketch accordingly and then finally I attach the ground to the Arduino is ground now I've also kept the dht22 in my circuit although I don't have it illustrated here because I thought it would just make the diagram a bit too busy now let's take a look at the sketch that I'm going to use to talk to the two different sensors now this is just a modified version of the last sketch that we used now you can see it starts off in exactly the same fashion I include the library for the DHT sensor for the temperature and humidity and of course I include the new pin library then I go and define a number of constants which again are very similar to our last sketch these two are just for the dht22 and these are the constants i've defined for the HC sr 0 for now you'll notice instead of trigger pin an echo pin I've changed it to trigger pin 1 echo pin 1 and trigger pin 2 echo pin 2 to represent the two different HC SR 0 for s since I'm running in 3 wire mode I've got my trigger and echo pins pointing to the same connections and so my trigger pin 1 and echo pin 1 are connected onto pin 10 of the Arduino and trigger pin 2 and echo pin 2 are connected to pin 5 again if you elect to use different pins or use 4 wire mode just change the numbers over here accordingly max distance is going to be shared between both of the sensors I've left it set at 400 centimeters now here is how we actually initialize two different HCS our 0 for s I use 2 new pin commands and on each one of them I create a new object this object I'm calling stone r1 and I just passed the trigger pin the echo pin in the maximum distance and this is sown r2 and it gets also passed its trigger pin echo pin and the maximum distance value you can continue to add more sensors in the same fashion then after this I define a number of variables again these are the same ones that you saw in the last sketch with the exception of the fact that I've taken distance and duration and split them into two independent variables so now I have duration 1 and duration 2 distance 1 and distance to everything else again is the same as the last sketch will initialize the DHT sensor and we'll go through the setup again with the serial monitor and starting the DHT sensor now I've you've noticed over here I've changed this delay slightly I used to have this delayed at 2 seconds I've delayed it down to one second and that's because there are more delays that I'm inducing in this sketch so I didn't want the total delay to be too long a gain I think have been a little too aggressive delaying for a full second so you can experiment by reducing that value and it will probably work just fine this is the same as before where we get the temperature and humidity values and calculate the speed of sound if you will left not to use a dht22 you can just substitute all of this which are standard 343 meter per second value for the speed of sound now we go and measure the duration the duration for the first sensor is measured over here using the iterations command like we did before and then I've added the delay between the sensor readings I've done this for a reason I wanted everything to be able to stabilize before I read the stuff off the second sensor now once again I think I've been far too aggressive by adding a full second delay here and you can probably reduce this by quite a bit and you may indeed want to do this in real life I don't think you'd actually want to let a full second pass between these because on a moving robot it could travel quite a since within a second then I just measure the second sensories duration using the similar type of command sonar to ping median iterations and then it basically is the same as the last sketch to calculate the distance for both using their duration values and I send the results out to the serial monitor all I'm sending out to the monitor this time of the value for distance 1 and for distance - I'm not including all of that temperature humidity and speed of sound information because I thought it would be a bit too busy so now that we've seen the sketch let's see how it works so let's test out our duel HCS our 0 for arrangement now I've mounted my second 8 CSR 0 4 on my solderless breadboard and it's at a right angle to the original one so it's facing back into my workbench over here now I don't think I've necessarily got this ruler lined up correctly it's just to give a rough indication so I'm going to put this roughly around the 10 centimeter mark and we're getting readings 10 point 7 0 centimeters as I bring it a little bit closer here we have 6 point 8 7 centimeters bring it further back a bit twenty-one point nine seven so the second one is definitely making readings and as is the first one I'm going to put this around the 20 centimeter mark and we're getting 19.53 19.8 on the first one move of that 240 again I'm getting a good reading down in around 10 and another good reading over here so both sensors are functioning now you'll notice a slight delay in the readings and that of course is because delays I induced in the code as I said earlier you can experiment by reducing those delay times in order to make your sensors respond quicker well that about covers it for our video on the ACS are 0-4 I hope it's given you a better idea of how you can use this versatile little component in some of your own designs be they robotic projects or otherwise now if you'd like to get a copy of the sketch that I've used in this video the easiest way to do that is to visit the accompanying article in the drone bot workshop comm website you will find a link to that article in the description of this video one thing I've added to the website on all of the articles including this one is a resource box which has some handy links plus links to zip files that include all of the code that I've covered in the articles so I think you'll find that very useful now if you have any questions about what I've covered here today please put them down in the comments I love getting comments from you guys and I try to respond to them as quickly as possible and if you haven't subscribe to the channel please do it would really mean a lot to me so until next time take care of yourselves I hope to see you soon again in the workshop bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 390,457
Rating: 4.941186 out of 5
Keywords: HC-SR04, Ultrasonic Sensor, Arduino, Arduino tutorial, obstacle avoidance, beginners Arduino project, ultrasonic range finder, Arduino Library, NewPing, DHT22, Distance Sensor, HCSR04
Id: 6F1B_N6LuKw
Channel Id: undefined
Length: 48min 3sec (2883 seconds)
Published: Sat Jul 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.