arduino 3 wire temperature sensor
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 04 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. Ms 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 1010 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 ‘! 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 ms I’ve, been using so by incorporating the speed of sound into our equation. At 343, ms 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 io 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 10000 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, ‘.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 io 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.
arduino 3 wire temperature sensor Video
arduino 3 wire temperature sensor news
PT100 Resistance Temperature Detector (RTD) Probes Support Extreme Temperature Ranges – CNX Software
Posted on Monday September 02, 2019PT100 Resistance Temperature Detector (RTD) Probes Support Extreme Temperature Ranges CNX Software … Continue Reading »
Posted on Sunday July 21, 2019An Epic Tale Of Thermistors: Tricks For Much Better Temperature Sensing Hackaday … Continue Reading »
Posted on Thursday June 28, 2018Move Aside Mercury: Measuring Temperature Accurately With An RTD Hackaday … Continue Reading »
arduino 3 wire temperature sensor Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr
Originally posted 2016-08-12 12:21:41.