LMT01 - My Favorite Accurate Temperature Sensor - Full Tutorial Arduino and ESP8266

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys so recently I was working on a temperature sensing project and while I was doing my research for that I discovered the LM t01 and this was really the perfect sensor for this application in fact I loved the sensor so much that I've been putting it into all of my other projects so you can see it's very accurate point five degrees C only two pins it's digital so I don't have to introduce more inaccuracies by measuring anything on one of my analog inputs you just have to measure the digital pulses out of it and we'll get into how all that works here in a little bit it's also great for battery-powered applications because it's not only low current you can see 34 micro amps and we'll get into that again later on but it's also pretty fast so the time it needs to measure the temperature and output those pulses is pretty fast so I am I decided to make a whole video dedicated just to this sensor and we'll get on we'll get into how to work with it on an Arduino as well as the esp8266 so here I've got one hooked up to one of my little trig boards here and I've had this outside posting to a slack channel once an hour so check this out it's kind of cool here it is so I've got a slack channel called log and once an hour the board wakes up from a sub 1 micro amp sleep state grabs the temperature and posts it up here so out here on the East Coast you can see it is pretty darn cold we hit minus 3 degrees Fahrenheit yikes and then here we are indoors at 71 point two so not bad so anyway let's go ahead and get into how this sensor works ok so if we take a quick peek at the datasheet here I'm just gonna scroll down a little bit and you know by the way the accuracy even though they state point 5 degrees C it's probably going to be much better than that if you look at this chart that's just the worst case but anyway if you could scroll down here what I'm looking for is to show you how it actually outputs those pulses it only has two pins so you've got a VP pin and a V end pin and this is the hardware setup so you power it up through the VP pin at I'm using 3.3 volts of course it can run at five volts as well and what it actually does here is change its current okay so in other words its source current changes and you're measuring that so it kind of got a complicated but if you look here at the timing diagram scroll down you see that a high pulse here is a hundred twenty five micro amps of source current to the device a low is thirty-four micro amps so if we feed that down through a shunt resistor of 10k ohms so if your current from the three three there goes down here we're gonna generate a voltage right across this 10k ohm resistor so again if it were high we're gonna be at 125 micro amps that times the 10k will give us a voltage right here of of 1.25 volts the low part of that which is actually what the part actually needs to run is 34 micro amps so that times the 10k is 0.34 volts so we're gonna have these two voltages on that 10k ohm resistor and we could basically feed that right into an analog pin if we wanted to we could if we could sample it fast enough we could feed it into a comparator input if the if the microcontroller supports that the Arduino 80 mega processor does have comparator inputs so you could do that or you could just add a simple NPN transistor so we're using the 2n 3904 it will turn on at the high level 1.25 so it'll actually turn on and it pulls this pin here low and when it's off at the point 3 4 volts the 10k ohm resistor right there snaps it up high to 3.3 volts and that's all there really is to the circuit and I have that exact thing set up right here so you see we've got three three and ground going over here and the three three is feeding the VP pin here directly it also acts as a pull-up for the 10k over here of course we have ground going over there and then here is the digital pin right here and this has to be an interrupt pin on the 18 mega so and that's the way I'm doing it now I did not use the comparator on the IET mega and the reason for that is because I wanted to use my same code for this everything exactly for the esp8266 because I'm already on this board here using the analog input for the battery voltage monitoring okay so anyway that is pretty much all there is to it and then once you get the pulses if we scroll down here you see the little formula here so pulse count and that's exactly what we have in the code so we we plug in the pulse count here and we get the temperature output in Celsius so over here in my Arduino code it's running right now here's my raw pulses I'm actually looking at degrees C converted to Fahrenheit and then keeping track of the high and low showing the differential there and you know that's mostly just test code and of course I'll make all of this available for download we'll go through this code too by the way so before we do that though I do want to hook up a logic analyzer and just show you kind of how this whole thing is actually working ok so I've got the Sayle hooked up here and I just wanted to quickly show you what to expect out of that circuit feeding the digital pin of the Arduino so looking over here I've already sampled that and I'm powering the LM t01 directly from the three three so it's going to continuously output that pulse train over and over and over again you could actually drive it from another digital pin off of your micro if you wanted to conserve power so let's say you wanted to deep sleep the micro controller then you may want to kill the temperature sensor completely and drive that pin low so that is something you could do in the case of the trig board over here I actually killed the entire three three rail when it sleeps so doesn't really matter and I actually had to do that to keep the sub 1 micro amp sleep currents but anyway so just looking at that you can kind of see here that in a continuous conversion mode it takes is somewhere between well I think actually it's in the datasheet but right here running 81 milliseconds there between conversions and if we zoom in on one of these you can see that the the 10k ohm resistor keeps that P and high at 3.3 volts and then as soon as we get a pulse we pull that transistor low and that's what you see so in the Arduino we're actually measuring these falling edges and we could actually just verify that the readings are accurate and I had to actually do this let me zoom out a little bit there and then let's see we'll add a measurement and look at falling edges and then we can look straight across this entire thing and you can see 1153 and when I sampled this it probably was 11:53 so and of course that's all been verified so anyway just wanted to show you that and we may actually poke around it some they're things once we walk through the code so let's go ahead and do that now okay so here we've got the sample code that you just saw running there and it's pretty straightforward I kept it all in one single tab here just so that it's a little bit easier to download all you have to do to roll this into your application is just hang on to the functions the variables here and if we scroll down a little bit we've got the get temperature function and the interrupt handler for the the pulse counter but anyway we'll get into all of that here so let's just go right through this so right up here at the top the only pin we're going to define is the output pin from that circuit you saw feeding the digital input and on the Arduino we're using digital pin 2 because that is one of the interrupts we can use it is d5 on the esp8266 that's what I used and I'll test that at the end and maybe get into some other stuff I tend to ramble so I'm just gonna get right to the point here with the code and then afterwards I'll just take a deep dive into some of the other cool stuff going on here anyway going on here the the variables we're using the pulse count and that's what keeps track of the pulses every time the interrupt fires we increment that pulse it's got to be volatile because we are changing that variable in an interrupt so we've got to tell the compiler that we're that could just change randomly in the code anytime that edge is goes low so then of course we've got the temperature C temperature F temp high temp love you don't have to use any of those so if you don't want to functions like I said before we've got the pulse detect interrupt function and then the main function that we call to actually get the temperature it returns a float temperature in C we're using the serial monitor for debugging just printing out what the temperature is at 115 - I'm gonna make that pulse pin an input and then right here in the Leon you might be wondering well why where are you going to attach it to interrupt you'll see so in the loop here the only thing you really need to look at is this temperature see is equal to get temperature because this is going to return the temperature in Celsius then all of this stuff here is just you know to print out the high to low the converted temperature differential and the raw pulse count so you can really just ditch all that if you don't want it you might be wondering well what's this check right here well if anything goes wrong in here it will return 1000 so if the temperature is a thousand degrees C you've got bigger problems so anyway that's what I used as a fail so let's just jump right in to get temperature down here first thing we do is attach the interrupts using this digital pin to interrupt so this will set up digital pin 2 on the Arduino it's actually interrupts 0 I think so this just takes the two I and convert it over to a zero that's a new thing that I discovered built into the Arduino IDE which by the way I'm using one point eight point three and we're gonna call this function pulse detect function down there on the falling edge so down here pulse detect right here and again the only thing we're doing in there is incrementing the pulse count by one conversion time we use this all over the place probably not the best name for that variable and it's just going to grab the current millisecond count and then we've got a couple while loops in here so the first thing I wanted to do is because this could get called at any time so let's say it gets called them and zoom out a little bit here let's say it gets called right here and you well you don't want to start counting the pulses here because you're gonna get less than the total pulses out so what that does right here the first is just to make sure that we're not in the middle of an existing pulse train so it goes ahead and actually just keeps track and it's looking to make sure that the pulse count stays at zero for at least 10 milliseconds so if we look here you've got the if the pulse ever is incremented set it back to zero and reset the timer so it actually just stays stuck here until it gets 10 milliseconds of silence on the pulse counter I got this yield added in here and that's for the esp8266 and to be honest with you some of the ESP stuff I'm still kind of figuring out but it is my understanding that since we've got a full-blown Wi-Fi stack going on it has to keep the Wi-Fi radio happy if you use if you just stick it in a while loop where it can't get out you could crash it so I've got the yield in there to let it take care of its housekeeping for the Wi-Fi so once we get 10 milliseconds of silence we go ahead and reset the pulse count back to zero and we jump into another while loop so while that Millis - conversion time is less than 150 milliseconds we wait for a single pulse so we're gonna sit here and wait for the first pulse out of that pulse train again we've got the yield here and as soon as we get a pulse go ahead and break out of that if we never got a pulse we return a thousand so that was a fail then we reset the conversion time again so we've got our first pulse then we're going to keep track of the old pulse council I've got a new variable here I'll explain that in a second - and we also have pulse time I'll also explain that so for now just we jump into another while loop here and this one stays stuck in here for 60 milliseconds so let me go back here to the logic analyzer so we let's say we want to grab our measurement right here we wait for 10 milliseconds we're quiet then we wait for 150 until we see our first pulse and as soon as we see that first pulse we're gonna wait it out 60 milliseconds for the entire pulse train to come through in fact all of this too by the way if we go in the datasheet here they've got some of these timings and somewhere in here actually that was probably at this T data and T conversion yeah they're up here so you can see temperature conversion time on powerup would be 50 milliseconds the T date data transmission time is 47 milliseconds and I think that's about it I think there's one more in here somewhere yeah oh here it is duh okay so right here so you've got 140 milliseconds max between total measurements and then you'll have 50 milliseconds for an entire pulse train so that's where some of those timings came from and I'm giving some safety margin in each of them but it really doesn't matter because I added some cool stuff in here to take care of that so in here basically we could just stay stuck and measure the total pulses so we've already got the first pulse okay and then in here we're going to continuously get hit with pulses for 60 milliseconds and that's fine and and I thought that was cool but the problem is is that I'll stay in there for 60 milliseconds but look just that measure time alone was only 14 milliseconds so if I've got a battery powered application all of the that 60 let me show you what that looks like I'm gonna put a time right there and then right here so 60 milliseconds would be all the way out here so this time here is just wasted battery so basically what I wanted to do was keep an eye on that pulse count time and if it goes idle go ahead and break out we're done we've got all the pulses out of the sensor and that's what this takes care of so every time we have a new pulse we check it we see oh it's not equal to the old pulse count remember this variable here which grabs the existing pulse count we've got a new pulse just came in set the old pulse count equal to the new pulse count and then grabbed a time stamp of that for the pulse time and then if we get stuck in here and we haven't had a pulse count for a while if that time ever is greater than 5 milliseconds then we know for sure that were idle because if you take a look here we'll zoom in a bit and see if we can look here and I'll just throw down the timing here between just one period here you know that's only eleven point two microseconds so yeah scape out of that there we go so if we look at what five milliseconds would be go here okay way we gotta go to zoom way out actually yeah five milliseconds would be all the way out here so I mean that's still quite a bit of dead time but it's only five milliseconds so guarantees that it's the pin is gone idle and we can break out so that's what that does so we're we're stuck in here counting pulses keeping track of the time basically in between those pulses and if we don't get a pulse and our time has elapsed five milliseconds then break out and that's what that does right there if our pulse count is ever less than five we know something went wrong there so returned a thousand in fact every once in a blue moon on the esp8266 I think it was I would get a little bit of No and that little bit of noise on the line was enough to trigger the first pulse so I would have like a pulse of like 1 or 2 which obviously isn't right so that's when I decided to put this little test in here and go ahead and return a thousand bail out then I just detach the interrupt because we're done with it in this function and then converge convert the pulse count into a temperature and this is straight out of the datasheet a seven point three point two in the data sheet and return that temperature in degrees C and that's how the code actually so now let me show you a couple cool things okay so basically what i'm doing here is kind of an interesting test because i've got digital pin 3 here set up as an output now and that's just gonna go to the SE li for debugging just the show you know what the arduino is sort of doing because right now we're just looking at the temperature sensor sensor that's just freerunning so we'll write we'll drive that pin high when we go to get a temperature and then low when we're complete and this is just running as fast as it possibly can there's no delay in there and all i there is there's a random delay I forgot I actually headed that so cool okay so when it gets up reading I forgot to mention this in the code actually so when it gets a reading it actually does a for loop of a random delay here between 50 and 200 milliseconds so the reason for that is just this I wanted to show you this so this is kind of cool this is the sample this is that digital pin 3 below this is the output fret straight from the sensor and if we zoom in here this looks crazy because look it doesn't take that long to get a measurement but because we're going so fast and we require the 10 milliseconds you can see here that when we went to go request it this time here and this would have been the 10 millisecond point right here so 10 milliseconds is kind of a lifetime because I could have reduce that down to like one or two milliseconds actually but since I'm waiting 10 milliseconds oh I crashed into the pulse train so it wasn't long enough so it waited for the next one and then right here is when it actually started measuring and we can look at that over here I'm just going to pop that down right there and then look at the first one so you can see that this is actually where it actually grabbed the pulse train and then when it stopped we had about six or so milliseconds after so that wasn't really on that line so let me pull that over a bit still pretty close to six so and that's where that should have been five milliseconds the five millisecond timeout so that's kind of cool actually and that's what I wanted to show you here is how that works so these skinny ones in here that's when I was able to fully get a 10 millisecond duration of silence let's see it's pretty close 10 right there so I got my 10 milliseconds and then I went right in and got my pulse train and then got out so that's sort of the ideal measurement right there but either way it does work and it is still pretty fast so I mean even in some of these I had to wait 187 milliseconds for the oh I'm looking at never mind don't look at that see right there - right there so about a hundred and ten or 111 no what is that 116 milliseconds I had to wait total for that measurement and and that could be optimized so anyway I wanted to show you that let's get into the the esp8266 real quick okay now I have the same setup with the esp8266 everything is the same and if we look here at the trig board I'm using GPIO five hooked over to it and that's to the output of that circuit to the digital pin GPIO five I needed ground as well and then three three is on the other side and this is just sort of air wired over and again I I sort of hacked all of the the resistors and the NPN transistor down here on the board and I was a little bit a little bit cleaner than with all the wires like you see here but anyway so looking over at the code the differences for the esp8266 are pretty straightforward you've got the pin obviously you have to change to five but also and this is kind of interesting is you have to add this to your interrupts function and when I did not have that originally I was getting random crashes all of a sudden I would crash and it would not recover so I did a little bit of research and found on some forum somewhere that you should place this in in on all of your interrupts functions and I believe it has something and again I'm still kind of getting up to speed with some of the ESP stuff but it has to do with the way the instructions are executed and on the ESP you know you've got the spy ship on there and it's actually running out of that spy chip so I guess when the interrupt calls this allows you to put this instruction or this function into its onboard memory the instruction cache Ram I think ok so now it's just a little faster I guess a little easier for the ESP to get to instead of running out of the spy flash again that could be totally wrong but either way this is needed to run this and it will run without it but again it did occasionally crash on me without it so that's pretty much it for the LM t01 kind of a cool sensor I've been using in it on several projects I like it better than the DSA team b20 right now may not be the perfect replacement for that part you can't run it on as long as a cable you can only have one sensor on a cable with the D s 18 B 20 you can have several not because it's more of a 1-wire bus and there's some other advantages disadvantages but right now it's kind of a cool sensor so I thought I'd make a video about it anyway that's the video thanks for watching
Info
Channel: Kevin Darrah
Views: 25,664
Rating: undefined out of 5
Keywords: arduino, temperature, accurate, lmt01, ds18b20, tmp36, sensor, esp8266, esp-12, trigboard
Id: Jf8fhW68vYs
Channel Id: undefined
Length: 27min 33sec (1653 seconds)
Published: Fri Jan 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.