How To - 1-Wire Interface with Arduino!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys so this video is part of the series of videos I'm doing on building a smart home consisting of wireless sensor nodes and one of the things I want to measure at each of these nodes is the ambient temperature now temperature sensors there's a ton of options available you could use a thermistor you could use an LM 35 you could use you know just a ton of different sensors and the one I chose for this is the one wire temperature sensor by maksim called the D s 18 B 20 so it was originally designed by Dallas semiconductor but then they were acquired by maksim so now it's considered the maxim one wire interface and this same interface is used by all kinds of devices like memory devices i/o devices just a whole bunch of different types of parts they all can work on the same bus so think of it like I squared C or spy so we're going to talk about the one wire interface in this video in insane detail I'm going to cover everything you need to know about how to talk to devices using this interface so I've got one device on this interface and I've got an example where we have 10 devices using this single one wire interface so we're talking about reading out the temperature we're talking about searching the entire bus out for all the devices reading that unique 64 bit address out of each of the devices everything you will even talk about alarm modes for each of the devices or even better yet we'll talk about the CRC generation of the the one where interface so anyway a lot of details a lot of stuff to cover I don't even know if I'm going to do it all in one video but here we go ok so before we start working through the code I just want to talk about the hardware here so basically I have a standalone Arduino set up on the breadboard here so this is the same circuit as you know the Arduino Uno just everything's on the breadboard we have a little USB to serial converter that programs it and also you know is used for debug to print out the the temperature on a serial monitor window and to work with these devices it's really a piece of cake you just give them you know your five bolts ground and then this the one wire interface the only thing you have to do is add a 4.7 km pull-up resistor on that data line but in my case I just used five K so I put two 10k ohm resistors in parallel with each other to get that 5k so when you when you look at these devices here this is their in a teo 92 package so if you hold it with the flat side facing up and you with the pins facing you the pin all the way to the left is your ground and the neck the middle pin is your data pin and then all the way to the right is where you put the 5 volts and that's pretty much it so you can connect this you only need one 4.7 K or 5 K ohm resistor per the entire bus and what I have set up it set up here is nine devices all connected to digital pin 8 and one device connected to digital pin 7 so I can demonstrate you know if you have only one device versus you know multiple devices and since each one of these has a 64 bit unique code you can connect you know hundreds of these things up all on the same all up on the same 1-wire bus so it's pretty cool stuff a lot of guys use that 64 bit unique code as like a MAC address for the device and I I might do that I don't know because you know the the goal of this whole thing is to send this temperature information wirelessly from a sensor node to the host base station and I might use that 64 bit address as in is a MAC address for each of the sensor nodes so anyhow that's pretty much all there is to it as far as the hardware goes yeah I think that's think that's it so you can use any digital pin for this too so let's jump over to ok so let's talk about the code which is kind of the heart of the whole one wire interface really there's nothing going on on the hard where everything you need to know is really going on here in the code so but before I get started I just want to let you know that you know there's nothing new about the one wire interface so you know if you just want to get up and running quickly I would recommend just going and downloading the one wire library for the arduino i haven't actually tested it out but i know it exists so download that and that will get you up and running in no time or you could stick around and watch this crazy long and detailed video that i'm about to put you through so this is more for the people who really want to know how this interface works and everything i show you here relates to the DSA teen b20 but it really works for all of the 1-wire devices so so you can carry what you learned here over to those devices as well okay so let's let's get into this code right here we have all of our global variables here that are used throughout the whole the whole program and you can pull these out and use them for various things so like for example number of devices this will always contain the number of devices found on the bus when you do a search function but I'm not going to really get into that right now we'll save that for later I just want to show you right now the easiest thing or what most of you actually want to do which is just read the temperature of one device on the bus so there's nothing going on right really right here it's a very variable setup in the setup loop nothing occurs here that you actually need you could actually comment all this out and it would work except the serial that begin you do need that in the loop the only function you need to call a my code to get the temperature is this DSP get temp of one device seven comma one and this is basically just saying you know on digital pin that's what where my one wire device is connected and the one means print so do a serial dot print of the temperature so here it is and it's printing off the temperature in Celsius and in Fahrenheit if I had this set to a zero nothing would print off but it would update this variable here this temp C decimal and tenth off decimals so if I wanted to do something with that you know within the code I could or the these are the actual raw bytes read out of the device so I could send these over wirelessly to another to another Arduino where it could then process the those bytes there and do the temperature calculation okay so let's get into what's going on in this function here so if I go down to get temp one right here okay so here's the function we pass the pin and the printer value zero or one so we got a bunch of variables here no big deal the first thing we do though we're going to start talking to the to the single device on the bus and we do a reset okay so which is another function in here so I'll just go up and we do this everywhere in the code so where is it here it is the reset of the pin so basically what we do is we drive the we drive the bus low for 500 microseconds and then we change it back to an input whenever you whenever you drive the bus is an input like when you set that pin as an input the pull-up resistors on the bus yank the bus high okay to five volts so to the device it's seeing the five volts there and then you go you you hold off on the bus there as an input for 500 microseconds and that's all it takes to do a reset so I have the datasheet here and we'll just show you that real quick so here is a reset and you can see it's just showing you you know your your low for about five hundred microseconds and then right here you let the bus yank up high and then the device pulls the bus low okay that's a presence pulse and you could read that like if I did you know if I delayed for 250 and then I did a digital read of the bus I should see it low so that'll let me know that hey somebody's out there pulling the bus low and we got we have some devices out there but I don't really care so I just ignore that I just weighed out the whole 500 microseconds all right so now we get back here now we do some byte rights so now we're going to actually do a bite right and we write a hexadecimal cc first and if I go up here you'll see I think I got to go down just a little bit here there it is skip skip ROM here so since there's only one device what you normally have to do is give now you would match ROM so you would say hey I want to talk to this specific device at this address and then you would give it the address but since I only have one device connected I can skip the rum so I'll just do a CC and then once I do that then I'm good to go so I'll be talking to that one device on the bus and to do that so here is the the byte I want to write I want to write a cc so I'll go up to DSP by right which is right here and we're passing in a byte and then I I call it command and on the pin and there's eight bits within the byte so I have to read in each bit of that byte and that will that will determine what I do to the bus so I need to write zeros and ones to the bus and it's that it's on a time I think they call it a time slot in here if you actually go and look at it somewhere in here they call it yeah these read and write time slots okay so if I get a one if my bit is a 1 basically what I do is I Drive the bus slow I chill out for a microsecond and then I let the bus get pulled up high for a duration of 60 microseconds and then the device will read that as a 1 so here is a master write of a 1 you can see you know we drive the bus low or we're telling you we're starting a timeslot hangout and then we let the bus get yanked up and then the device will read this out as a 1 okay otherwise what we do here if it's a 0 we write the bus low we still have to read it low but this time we do is we keep it load the entire time the entire 60 microseconds of the time slot so you can see here when you're doing it right of a zero keep it low the whole time okay and that's all there is to it that will write an entire byte to the bus okay so it's kind of cool the way it works all right let's go now back to where we were get temp 1 so we write a cc then we write a 4 4 and the 4 let me pull this back over here and the 4 4 is a convert so now the device will convert the the temperature it will grab the analog temperature and digitize it so then it's ready this is the analog to digital conversion on the device okay so you do that now depending on the resolution you have set for the device you know I think I showed you it could be 9 10 11 12 bits that will determine how long you have to wait until you go and grab the temperature out of the device so you do this then you got to chill out and wait and then you can go back in and then get it so you can see now we do another reset then we do a bite right of the CC this is a skip ROM and then we do a scratch read of the pin and this is a scratch pad read and you can see here that it's a B II so in that function if I go to the scratch read let me go find that real quick here here it is this is this is kind of a crazy function here you can see what we do we start off with is a bite right of BBE so now we're telling the device we want to read out those 9 bytes in the scratchpad memory and you can see in the datasheet they show you these 9 bytes here okay so here's the temperature least significant byte most significant byte the alarm temp hi the templo alarm the configuration register a bunch of stuff we can't do anything with and then the CRC of the previous 8 bits or 8 bytes sorry ok so what we do here is get out we store these 9 bytes in data in now what I do is a CRC checker so I want to check the data in like I was saying we have long cable lengths or you know I've got a sensor that's outside and it's like negative 20 out or something crazy and the bus is kind of acting goofy I want to make sure that my temperature reads in makes sense so what I do here is the CRC checker there's eight bytes with eight bits each so I have to go through every single bit in the in the entire eight bytes I read in and then I'm going to compare those eight those those eight bytes in the CRC of those eight bytes to the actual CRC that I read in so that's how I do the whole CRC check and they have a little thing here about the CRC right here you can see this little picture here and it's basically showing you that you know you have this is your CRC this is your input input bytes or your input bits and as they're coming in you need to X or them with the least significant bit of your CRC as you're creating it and then constantly reloading and shifting this so let me show you how that all works all right so we create this CRC XOR and that's how it all works it's this XOR and it's of the the least significant bit in according to I and J here it's you know these are constantly ratcheting up and then we use the caret symbol here for an XOR and we bit read CRC which is a byte I just created in here and I initialize it at 0 and I check that an IX or that and I store that as CRC XOR and then what I do is I bit right to the CRC at position 3 and 4 here the CRC XOR back against whatever was in CRC 3 and 4 so basically if you look at this I I do this XOR now I have the XOR bit here and then I do this against this value and then against this value and then I store them back into these I use and then I'll show you the next step then I shift everybody over to the right once so I just stored the the XOR of whatever was in here back in there and there then I shift everybody to the right once and then I take this this XOR bid I just did and write it to the last position of the CRC so basically this is yeah this is kind of crazy but let me see if I can get it all in screen here so I just I just stored both of the the XOR of this back into that for both of these positions shifted the whole thing over to the right once so the zero position just got dropped off completely and then I stored that value that I just XOR dand here into this position and I do that for all 64 bits and it just keeps going and going and going and it goes through that and it creates this CRC okay and this is a way to check you know for data integrity and then basically I just compare it if CRC is equal equal to the data innovate whatever I actually read in because the device does give me some CRC in the scratchpad memory here they better match and if they do that means that I store you know everything I did read in into the variables that I want that stuff to go into otherwise just set everybody to zero and set the CRC flag to one so you know for future use I can use that to check for errors okay so now I've got the data and the data is good now I need to check it or convert it into an actual usable temperature and this is kind of a pain in the butt so if I go here now to worth it actually shows me the temperature what it looks like I think I passed it up okay right here okay so here's the temperature format so this thing measures you know it's not just whole numbers it's not just integers you know you know I've got a fractional part to these to this temperature measurement and it comes in is two bytes LSB and MSB you'll see your two to the negative one negative two negative three negative four those are all the bits corresponding to the fractional part and then the actual integer part of the whole number part is you know bits four to seven of LSB and then the first three bits of MSB here and then you see these s's that tells me if there is a negative if the temperature is negative or not okay and then there's a few examples down here and this gets a little complicated and I'll explain why but basically here I'm checking the signed bit okay I'm checking you know this value here and if it's a zero that means it's positive okay which is pretty easy conversion so that I go through the first four bits of LSB and I set up that fractional decimal part this is kind of you may have not have seen this before but this is kind of interesting so like two to the negative one so right here this it would be a value of one you know and as you keep going you go one two four eight and that makes up you know your number but when you go the other way it's the other way around it goes you know 0.5 0.25 0.125 point and then the last one there's 0.0625 and that makes up the fractional part so basically what I needed to do is go through those first four bits and set up this fractional part and since I started zero you know I'm going your temp C decimal plus the power so two to the negative four plus I so that'd be zero so at negative four I'm grabbing the 0.0625 so I'm working straight through those since I started zero that is two to the negative four and I work my way right through there all the way until two to the negative one okay yeah it's kind of a crazy way of doing it but it does work and I've got some some notes here that uh that help explain that too okay so now I have that fractional part keep going along here now I want to combine that fractional part with the whole number part and the way I do that is you know I have those top four bits here that are all good so I just take those ship them to the right four times and then I add the MSB to that but I shift this over one two three four times so that it's on top of you know the LSB so I'm combining everybody together okay I get back over here so everybody's back together all right and that's exactly what I do oh but I also end it with this value here 1 1 1 and then the rest zeros and that's basically to just chop off all this crap you know whatever was there and I just did that for good measure just that you know make sure that whatever was there is a 0 in the MSB that is here okay temper decibel this is just simple conversion times 9 divided by 5 plus 32 okay that's it that sets up my temp C decimal my top F decimal and we're done everything's good now I this is where things get a little tricky but what if it's negative well you would think that these sign bits would just be 1 and then this would be a some number and we would do the same exact thing and that's exactly what I did and I put the board in the freezer and then as soon as it went negative I got like negative 1000 something and I was like what the hell is going on here well if you look at this closely it says that the negative temperature sensor is stored as a somewhere in here as a 16-bit sign extended two's complement number so it's set as two's complement okay and you can see here that when it flips negative everybody goes to is basically it's like an inversion of what it was positive so yeah you get these signed bits up at the top but you also invert everybody else okay and you may have actually seen two's complement you know accidentally in your Arduino program like when you multiply two numbers together that are really big that's a way of representing negative numbers and that's probably for another video but basically this device does use two's complement so what I do here is the first thing I do is bring another variable into the picture here and basically this is a word so I'm combining two bytes together and I just want to combine the MSB with the LSB so now I have a 16-bit word and I'd shift you know the MSB on top of the LSB and then I do an XOR of that against all ones and what that does is it flips everybody it inverts every bit so whatever was a one becomes a zero and whatever was a zero becomes a one and then I add one and this is how you reverse a two's complement number so I'm trying to get the that two's complement number back to normal like it was up here before so I do that you know this is kind of a crazy way of doing it and then I pull MSB and LSB back out of it and the reason I did that was because well I redid the math up here that I knew how to do so I just wanted to copy and paste it so that does that and it pulls it back out and then now you're going to go to do these same exact math as before okay and with the addition of a little negative one added on to the number over there okay so that's it yeah it's a little crazy it takes a while to figure out and get worked but that is the scratch read ok so I don't even remember where I was but let me go back up here I think I was doing a temperature a single temp read let's see if I can find that yeah nope right you're get temp 1 ok so does that scratch read all right and then if you want to print it if printer was equal equal 1 you go and print it off okay as a float with with four decimal places okay so that is how to read the temperature out of the device okay so that's that's a little complicated not too bad though right okay so I took a little bit of a break there felt like that was kind of a long video so if you're watching this it might have been tacked on to the existing video or it might be a part two but anyhow now we can move on to another function so we just talked about get the temperature of one device when you have only one device on the bus now I'm just going to show you how you can get the address of a single device so this is good if say you want if you want to know the address of each of your devices and you want to keep them all on the same bus but first you want to read all the addresses out so that you know where each of these devices are going and then when you do a read of an address of the temp of that on that address you know exactly where it was so I just uploaded this code and now it should be there it is so it's just reading out the a byte unique address 64-bit so you can see everything starts at 28 then you have six bytes and then the checksum okay so let me just show you how this works it's pretty simple stuff now that I showed you how to do a byte right to be byte read you know everything is pretty much the same so if I do good I'll just show you quickly though get temp address here we go no not get temp address get address one see it's such simple code that it's only a few lines so we do the reset then we do is 33 that's a single that's a read ROM address command and then read out basically eight bytes so we do this byte byte read of eight bytes and oh did I I don't think I talked about byte read yet I talked about scratch read up let me show you that real quick so this is how to read the bytes out of a device so it's pretty much like writing the bytes except in Reverse so you've got 8 bits for 8 bytes you know 8 bytes sorry 8 bits in a byte so you got to do this 8 times to read out the 8 bits and you'll notice that this function starts a byte that's because it returns a byte so it's kind of like when I do a digital read in the Arduino it's returning a 0 or a 1 so when I do like if digital read is equal you go to 1 well you can do that with this so if DSB byte read is equal to blah blah blah blah blah it's that sort of thing this is actually equal to a byte in the code and that'll make sense here in a sec okay so when I do a byte reading I go through the 8 bits set it up as an output drive it l'hotel the tell the device that I'm ready - I'm ready to start talking we set it back up as an input now we hang out for about 10 microseconds let the device pull the line low or just leave it floating and if it leaves it floating or high that means it's a 1 so if I read it in high that means set that bit otherwise clear it and do this 8 times and work through it until you have all 8 bits set okay and that's pretty much it so this at this equals about 60 micro seconds or so about 59 what I have it set here for okay so that is a byte read and then you can see here we do a return of byte in okay so if I get back here to get address 1 you see that I read it in I read it in 2 ROM 0 so right this is a and a right here so the array is set up as in in ROM 0 is equal to 8 bytes okay and then if you want to print it out you can so I was printing it out and that's it for that that's pretty easy let's see what else we got here now oh this is kind of cool so I'm going to get that address and now I'm going to get the temp on an address so I just showed you that this is storing it at ROM 0 so if I do a DSP I got changes to a seven so if I do a DSP get temp address on pin seven of address zero because I know that when I have only one device I can I know it's at address zero of ROM and then I can print that out so if I did this I'm going to upload that real quick and I should it should show me the address I should have actually changed this to a zero but yeah well okay let that upload real quick and you should see the address with the temperature here in C and F so that's kind of cool too and this is a good function because especially when you've got like hundreds of devices they're all indexed you know zero one two three four or five six seven eight and they're all index within this memory here so this is I have it set right now 400 addresses so we stored that first one in zero and they would just keep going zero one two three four you know wherever you want to put them and in fact right here if you have your custom address set and I have an example here if you have it set then you can do this little for loop and in your setup to set it to whatever address you want so if I want this to be like five or whatever you know you want you could set that up there and it'll read out what you put in here and put it into the ROM so that when you do this call you just set it for whatever address you put into rum okay so let me go and show you that real quick how to get add an address here so here it is you got the pin you got the address and whether you now you want to print it you do the reset but now you do a 55 command which is a match the rum so now you actually have to give it an address that you want to go talk to and you know it's eight bytes so you got to do this for loop and then you've read out in least significant byte out to the device and you can just see it's just grabbing out you know whatever you have in as the address coming in and out to the seven so it's just doing byte rights of the a bytes okay then you do a convert so you have to do that kind of that a 2d conversion and then you have to wait your 750 milliseconds and you can't change that and I'll show you how it's like then you go back you do the reset do the match and then you're ready and open and then you have to write out the the unique address and then you do a skip a scratch read so just like before and then of course you do that you can do the print so that's kind of cool okay let me show you a scratch right here real quick so up here if you did a search and now you've got all these devices you know you've got multiple devices so you've got ten devices or whatever you can do this scratch right and that's how I actually have it set up right now or you can just do a scratch right of you know one device so here it is pin the address on the high temperature in Celsius of the alarm the low temperature alarm and the configuration so I have the configuration set for all ones but you can set it for you can go to right here where's that somewhere here it shows you the configuration bit right here and this shows you you know the resolution in bits so once you start you know if you set it all four zeros then you'd be at nine resolution nine bits of resolution but the trade-off there is that now you get more speed so now you don't have to wait for the 750 milliseconds you only have to wait like 100 milliseconds so that's nice but this will send that out to scratch but what this also does is it well why don't we go and find out let me go down to DSP scratch here scratch right get temp convert all scratch right here we are so what this does is it does the mat it does the the ROM match it sends out the 8 bytes and then it does a scratch write command for e and then you can see here that it's sending out the high the low and the config okay that's the three things you can send out to this thing the high alarm low alarm the configuration byte now it's in the scratchpad memory but you can then copy that into the EEPROM of the device so when you remove power you don't have to keep doing that scratch right so what this does here then ROM right or ROM match here fifty-five same thing but now we do a 48 so what you just wrote here to the scratch pad it copies to the EEPROM okay so just storing it you know in hard memory okay now I'm just going to go back up here and I think we're just about ready to show you the search all now this is going to be a pretty crazy long part of this video but if I show you this search all here I'm going to do a search all on my pin 8 ok so I didn't want to do have to do this but I think it's I think it's easier to really visualize how the search algorithm works by just drawing it out and showing you an example of it but you kind of have to visualize it so that's why I dragged out the white board here so with the one wire interface you could have multiple devices connected to the same one wire but the only real way to talk to each of these devices is by their individual address so the search algorithm allows you to search the entire bus for each of the addresses so you're pulling out the addresses of all the devices you know the other way of doing it though of course is to read in each of the devices individually without you know all the devices connected to the bus so like just connect one device to the bus read in its address copy it out paste it into you know a text document or something and then you know like I showed you in the code you could actually just set that and use those addresses to talk to each of the device individually if you didn't do that you could use the search algorithm to just go out and search for all of the addresses and it's kind of cool the way it works so basically you know you do a reset of the bus then you do a search all command and then what you do is issue a bit freed okay so let's just say we had a device out there that had an address like this and we're just going to do four bits for now and we just have one device out there so we do a bit read of the first bit when we do this bit read it will give you the first bit and we'll say that this is the first bit right here we do a bit read so we do the bay read this guy has a zero so it pulls the bus low and we get a zero then we do a second bit read now this is the complement bit read which means that it's going to give you the compliment of this bit so it'll be a one so now that we know that we got a zero one we know that for certain that in this position all devices out there are at a zero for this bit position right so what we do then is a right so then we write a zero out to the bus this guy sees the zero and says yep that's what I have there I'm going to keep playing this game so then you do a bit read and this will return a zero and then you do another bit read and it will give you the compliment of this and you get the one then you do a right so then you write your zero so it's this reread right cycle and it sees that it says yep that's what I got and it'll keep playing the game and then it'll give you a 1 and then the complement of that one which is a zero so it's all in these pairs here and then you write a 1 and you said yep that's what I got and you write the 1 and then it will give you the 0 and then the 1 now that's how you do it just with one device and this is easy but it gets a little complicated when you've got multiple devices out here so let's just keep working with that device address but let's say now you've got a second device here that has something like this or even better has this so now we'll do the same thing we do the read read write and we read in both of these devices we'll give you the zero they'll both pull the bus low you'll read into zero okay then when you when you do the second read it will give you the compliment of this both of these devices will let the bus yank up high and you get the compliment at one so you see this and you say okay all the vices out there have a zero in bit position zero so you write the zero both devices say yep that's what I got and they'll keep playing the game now what happens is you do a read this guy will pull the bus low with its zero and this guy will leave the bus high with its one but since it's through pull-up resistors it has no way of driving the bus high it'll just leave it floating and this guy we win by pulling it low so it pulls it low and you get a zero now you'll get the compliment of that signal or of this bit position and this guy will let the bus float high and this guy will give you its compliment which will be zero so it'll pull the bus low while this guy is leaving it high so you get a zero this is a discrepancy you'll see the zero zero and this means that you have both zeros and ones out on the bus and you could have a bunch of devices doing this so we'll have this this major discrepancy so what do you do well what I did was I write a zero and when you write a zero this guy says yep that's what I got I'll keep playing the game this guy says nope that's not what I got and I'm done and playing the game and it's done so even though now you keep going with this zero here what will give you next is it's 1 then it's 0 and then you say yep got a 1 it writes the 1 and then it keeps going but when you come to this discrepancy right here it doesn't matter because this one's done playing so it will stay out of the game and this guy continues right on through ok so that's pretty easy so if you get 2 devices eventually you'll have the discrepancy and then you can kill out so what I do though is I log this I can see I logged this in memory so that the next time I go back through I know I have a second device now I go back through but instead of writing a zero here like I did I'll write a one there and when I write the one there then this guy is done and this one will keep playing the game okay so it's that back and forth kind of thing now it gets kind of complicated it can get very complicated so let's say you've got a let's say you've got this set up here now where you've got a zero zero zero one something crazy like that where you've got this discrepancy here between these two guys and you choose the zero and you keep going but this guy's cool because it has a zero too but then you come to another discrepancy on the bus so nothing's got two discrepancies so you've got a discrepancy here and then you choose this one so it's like you you were starting here and then you zigzag around so this gets a little complicated so that's what I have in my code to monitor the last discrepancy found so that as it's moving through here as you're logging all these discrepancies this was the last one found on the bus so that's the one I go back and then change out okay so yeah it gets a little complicated and it's you really got to think about how it all works but which is why this is so cool and fun to actually try to solve it's like a puzzle like how are you going to find all these devices you know the rules and the rules are simple but how do you write the code to actually make this happen and I did it by doing a last discrepancy found check so that as it's working through yeah I found all these discrepancies in here I'm going to start with the last one figure those two out and then move my way in until I move all the way into the code or into the first bit position okay I don't know if that's the greatest way of explaining it but let's jump back into the code now okay so now we can talk about the actual code behind the search all function and okay so we have this this DSP search all and it's on the pin and whether or not you want to print them on I think I already showed you this maybe not so found nine and it goes through all of the addresses it found and I mean if it finds these things instantly the only delay you're seeing in there is due to the convert delay the 750 millisecond delay so we found nine devices all on on wire eight and we're returning here the temperature of each so I mean this even works you know I've got I'm going to upload that real quick so I only have one device connected to the tube to digital pin 7 but I should still see that you know something was found right at least just one and there it is found one okay so anyway if we go through this now now now that I've showed you on the whiteboard exactly you know what the rules are we can break this down in code and maybe it'll be easier to explain in code maybe not but I just want to at least give you an outline of what's happening here so here's search all here's search alarm search all search alarm runs the same exact algorithm going differences is that search alarm only searches for devices that are alarming and we'll get into that here in a second but first let's talk about search all and basically the search command is fzero so it passes that along down to the search algorithm which is all the way at the bottom here and it's a giant function here here it is search brings in the the command which is f0 pin and if we want to print off here's a bunch of arrays here and we're using these to basically keep track of where they're where discrepancies are happening where the true checks are the complement checks are so what I was just explained to you the read read is what these are the read one read two okay and it all kicks off right here we start off you know one there's only one device out there we're assuming there's one device right so I equals zero and I was less than them divided to be at least one out there right well actually I've got something in here to Detective nothing's out there okay so here's your reset the command that's zero eight bytes eight bits each we got to go through 64 bits here's your do a read and then I read true check comm check and we basically we're doing a bit right in true check of the bit read and I actually have dedicated functions down your for re byte search read which is just a bit right a bit read so you know we pull the thing low for set for a microsecond and then we wait it out for 10 microseconds go back to an input and then we check the bus to see if it's high or low okay and then we've returned that that's these are boolean z' so we return a 1 or a 0 okay and we're right here so we do that this could be so if it's a you know if there was a 0 in position 0 we get a 0 1 ok now we can actually see these and do the check now look here if we see a 0 and a 0 for the true track contract that means the discrepancy was found now we have the rules and what we're going to do here with this if the disk check of that position discrepancy check of that position is equal to 0 then you do this if not you do this down here so basically what this is is is what am I going to write to the bus what am I going to write to memory and what am I going to write to the bus on default this would be 0 so let's work through that first thing we do is clear out the last discrepancy found the reason we do that is because next we're going to set the last discrepancy found to that bit position so if I'm working through and I find a discrepancy boom I set this mmm but then all the sudden I find another one clear it out and then set it okay this is important to do because now I know where the last discrepancy was found in the whole bit string okay then we do a bit right of the address we're actually writing to and we write it to 0 that's our default that's what we'll do first and then we do a right zero a search right of zero and this is writing to the bus so a search right is just simply down here it's alright to actually cut off the bottom here but this is nothing more than bringing the bus low for you know 51:59 microseconds if the values is zero if it's if it's a 1 we just keep the bus we yank the bus high with our our input here and this is telling the device we're writing either a 0 or a 1 to that bit position okay so right here at 0 okay so we do that on default we won't do this so we'll come back to that in a second and then else if we there was no discrepancy found and true check was equal to 1 and comp check was equal to 1 that means we read a 1 1 that means that there's neither a 0 or a 1 written to position 1 that means that nobody pulled the line low that means that there's no devices hence we said number of devices equal to 0 that means like broken wire or you know there's just you've got the wrong pin or you know who knows something's wrong with your device maybe you forgot a pull-up know the pull-up wouldn't wouldn't do that because you know you'd be if you had no pull-up then you just kind of read you might read in once I don't know have to try that okay well anyway if so if there was knows if there was no zero zeros and there was no one ones that means you got a 0 1 or a 1 0 that means that you can you can confidently write to that address whatever you read in first the true check and then you can write the bus to that true check so you do that for all 64 bits just burn right through it now you're on to your next address and you come into this little guy here and this is going to check out your discrepancies so what did you find out as you're writing through there did you find some discrepancies and that's basically what it's doing it's starting at 7 because it wants to find the last discrepancy especially in disk jack here and the reason it's doing this from 0 - from 7 to 0 is that it wants to also clear out the discrepancy check back on going the other way you don't want to start at 0 and clear out your discrepancies because some of those need to be stored in for your next check ok so as I go through this you know 8 8 bytes 8 bits each go through and we want to make sure that my last so if I read in a 1 as my last disk check and my current district is equal to equal to 0 meaning hey I just I there's nothing there I didn't just do this okay I found a discrepancy but I didn't handle it that's what this is basically doing here go in here and now set your dis check for 1 so like I showed you before what our action was right here was to write a 0 because this check was equal to 0 but now since I found that disk check and it's equal to a 0 right here we didn't just deal with it we can now go in here and do this else which is our y1 now in a1 right so it's pretty cool stuff and now we can increment the number of devices and then get out of this loop right away as soon as you find this get out of the loop so it'll do that now if it goes through and you found a disk track and it dealt with it you got a 1 in there you know but it didn't line up with this go ahead and clear that this check out so you want to work your way backwards and you want to get rid of all your discrepancy checks so it can move through it so that's how that works and then it goes back up and does the whole thing over and over and over again and that's it that's the search algorithm it's not tube it's not too bad in code but it's really it can really mess with your head trying to figure out how this whole thing worked and I think the easiest way to do it is write up some examples of different scenarios that would give you different outcomes here especially ok and then of course if you want to print off the thing your printer would be a 1 and you could found found in a number of devices and then go through and then print off the addresses and then you also can go the temperature at those addresses and print that off okay so that's pretty cool and I showed you an example of that now I think what might be need to do is where are we at here we're right here we did a scratch right about 27 how about we set this for 25 and I'm going to talk here just for a quick second here we did a search all I'm going to change this back to 1/8 and I want to set this for a high temp of 25 and I want to do a convert all I'll explain this in a second and I want to do a search of alarm and I think I'm good with that and I want to do is search all again ok so what this will do is convert all basically this convert all if you want to search for the alarmed devices you still need to give them a conversion you have to still do that hexadecimal 44 to them all and that's all this does I'm not even going to get into it it just goes through and converts every device it found on that bus and then you can do a search alarm and then this just does the same search algorithm I just showed you but with a different command so it'll only reveal devices that are alarming and then it also stores the number of devices found as the alarm devices so why don't I upload that real quick here and then i refresh it here as a search all just so I can read since it uses the same algorithm you got to keep running this search all because you know if you'd run this it might only find one device but that's not the total number of devices you've got out there so you gotta have to refresh it with search all ok so here we go okay so it's going to do the first search all here but it's actually printing it okay found zero zero zero zero zero okay I'm going to probably make some noise here but I'm going to take my lamp here and I'm going to bring the lamp down close to the devices okay we've got the the lamp touching pretty much the devices and let me bring this up here a little bit so you can see it work up found two found four and you can see now they're starting they'd hit the 25 fresh threshold you're starting to see them come in found five found nine now all nine devices are above 25 degrees C okay so kind of cool stuff and if we threw these in the the freezer if we threw this board in the freezer we would see the same thing but on the low side so kind of cool and the whole point of this is you see this delay you know you see this huge delay in the conversion well I can go in and blast through all these and do the convert all and not wait that 750 milliseconds and then I can do a search so it saves a lot of time you know especially if you've got like you know a thousand of these freaking things on the bus you know you want to you're going to want to just convert all blasts and convert them all and then do a search and then just continuously search and you can see you know the search command is fast it's really fast there's so it's kind of cool to actually watch this this bad boy work alright and that's pretty much the code so I think I got everything on this kind of a complicated little interface but you know once you understand how it all works it's pretty cool and again here's the alarm devices if I just print it off alarm devices you would watch it go you know nine eight blah blah blah blah blah in real time let's see how this guys doing I took the lamp so now found one okay cool so anyway that is a crazy long video on how the probably one wired device one wire interface works with the DS 18 B 20 thanks for watching
Info
Channel: Kevin Darrah
Views: 50,375
Rating: undefined out of 5
Keywords: ds18b20, 1 wire, interface, MAXIM, Dallas Semiconductor, arduino, temperature sensor, search algorithm
Id: ZKNQhzPwH0s
Channel Id: undefined
Length: 54min 40sec (3280 seconds)
Published: Thu Apr 10 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.